Reputation: 31
I have a comma delimited .csv file looking like this:
header1,header2,header3
value10,value20,value30
value11,value21,value31
,,
,,
,,
How do i delete the "empty lines" at the end of the csv? The number of empty line is not always the same but can be any number.
And how to save the modified csv in a new file?
I have a comma delimited .csv file looking like this:
header1,header2,header3
value10,value20,value30
value11,value21,value31
[empty line]
[empty line]
[empty line]
How do i delete the "empty lines" at the end of the csv? The number of empty line is not always the same but can be any number.
And how to save the modified csv in a new file?
Upvotes: 0
Views: 329
Reputation: 37404
It kind of depends on your definition of an empty line. If it really is empty as in there is nothing but a newline, using awk you could:
$ awk '/./' file
or /^$/
ie, if there's anything but just a newline (default RS
in awk), print it. If you need the outout to another file:
$ awk '/./' file > file2
If your definition of empty can tolerate space in the record along with the newline:
$ awk '/^[^ ]+$/' file
Update: A-ha, the definition of emptiness boiled down to all commas. OP mentions in the comments that the "empty lines" is always placed at the end so once we run into first empty line (ie. nothing but commas in the record = ^,+
= !/[^,]/
- sorry about the double negative), exit.
$ awk '!/[^,]/{exit}1' file
header1,header2,header3
value10,value20,value30
value11,value21,value31
Upvotes: 1
Reputation: 203674
It's not clear from your question but it sounds like all you need is:
grep -v '[^,]' file1 > file2
Upvotes: 0
Reputation: 3147
Use below -
sed -i '/^$/d' file
Explanation :
^$ : To search line which doesn't contain anything from start(^) to last($)
d : To delete that searched line
i : to make the changes permanent so that you don't need to redirect to another file and then rename it again.
Upvotes: 0
Reputation: 7517
A quick and dirty (but efficient) way of doing it is to find on your keyboard a character that is not in your file, for instance µ
. Then just type:
tr '\n' 'µ' < myfile.csv | sed -e 's/[,µ]*$//' | tr 'µ' '\n' > out.csv
Not tried, but you can adapt this idea to your own need. Maybe you will have to also add the space character (or the tab, etc.) in the bracket expression.
The idea is to replace the 'end of line' character by a (temporary) µ
in order to get a (temporary) single line file; then use a very basic regular expression for deleting what you want; and finally restore the 'end of line' characters.
Upvotes: 0