0

I am writing a script wherein rather than writing over the existing data, I would like to write to the next empty line in the csv file.

Current Output

texta,2
textb,
filed1,field2,field3
1,abc,123
2,xyz,124

Expected Output

texta,2
textb,
filed1,field2,field3
1,abc,123
2,xyz,124
1,jkl,547

The current code which writes over the existing data in the file

#! /bin/bash
echo "Enter file name"
read name
if [ condition ]
then
     #commands
else
     echo "Enter alphabets"
     read alpha
     echo "Enter number"
     read number
     echo "1,$alpha,$number" >> ${name}.csv
fi

Example input here aplha = jkl and number = 547

This will write over the entire data but I would like to write only to the next empty line each time the if condition fails. Any help is appreciated.

3
  • 2
    Your code looks ok, since > truncate and >> append, maybe the file has carriage returns? Commented Sep 17, 2021 at 8:42
  • @Jetchisel True, my bad . While actually writing in bash i used > and over here >> It does work Commented Sep 17, 2021 at 9:03
  • 1
    Your code lacks a loop to iterate the input lines. See here for nested read. Commented Sep 17, 2021 at 10:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.