Hi All,
I have a file where I am converting newlines to comma separated values but I would like to append zero if the output is empty
Here is the command I am using
sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}' test1.txt
test1.txt will have comma seperated values but sometimes this file can be empty so I wanted to append 0 to this file how could I do this.
I appreciate your help.
Regards,
raj
I am not clear what you are asking, but maybe this will be helpful to you:
bash has -s test for non-empty files,
if [ ! -s empty_file ]; then echo "EMPTY"; else echo "Not Empty"; fi
So you could do
echo "0" > empty_file
if the file is empty.
I wanted to do like this echo "0" >> sed command please let me know if this works and output file should have 0 in it. how do i do this approach
Regards,
raj
PikK45
4
What @Migurus told is the easiest way!
Yoda
5
Another approach using awk:
awk 'NF{s = s ? s OFS $0 : $0; f = 1 }END{ if (f) print s; else print "0" }' OFS=, test1.txt
Hi Yoda,
I tried using awk command but its just showing as 0 at unix command prompt but not appended to the file
awk 'NF{s = s ? s OFS $0 : $0; f = 1 }END{ if (f) print s; else print "0" }' OFS=, test1.txt
Please let me know if I am wrong somewhere
Thanks
Raj
RudiC
7
Me too, I'm not clear what you want to achieve. Do you want the original file to contain a single "0" char after the operation if before it is empty?