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.

Helium
  • 49
  • 7
  • 2
    Your code looks ok, since `>` truncate and `>>` append, maybe the file has carriage returns? – Jetchisel Sep 17 '21 at 08:42
  • @Jetchisel True, my bad . While actually writing in bash i used `> `and over here `>>` It does work – Helium Sep 17 '21 at 09:03
  • 1
    Your code lacks a loop to iterate the input lines. See [here](https://stackoverflow.com/questions/16308110/does-bash-support-doing-a-read-nested-within-a-read-loop) for nested `read`. – ceving Sep 17 '21 at 10:22

0 Answers0