UNIX shell scripting

February 19, 2009 at 05:57:14
Specs: Windows XP
hi pals,
I have a file called file.csv(coma seperated) like the below
2,elephant,cow,2.3,
2,elephant,cow,2.3,
2,elephant,cow,2.3,
2,elephant,cow,2.3,
,,,
,,,,,
,,,,,,,,,,,
,,,,,,
,,,
How can i remove unwanted commas at the end of the file.My output should be like this

2,elephant,cow,2.3,
2,elephant,cow,2.3,
2,elephant,cow,2.3,
2,elephant,cow,2.3,

it should not have the empty commas at the end. I tried sing sed but can,t get correct combination. Pals thanks in advance


See More: UNIX shell scripting

Report •


#1
February 19, 2009 at 07:41:57
This sed command deletes all lines starting with a comma, but leaves blank lines:

sed 's/^,*//' myfile.txt

This takes care of the blank lines at the end of the file:


sed 's/^,*//' myfile.txt|sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'


Report •

#2
March 15, 2009 at 03:28:52
Thanks friend. It worked fine

Report •

Related Solutions


Ask Question