Tuesday, November 17, 2009

Unix - Delete last line of file

For some stuff that I was working on, I needed to remove the last line from a very large data file. At this point, Cygwin + Sed came to my rescue!

sed '$d' source_file.dat > destination_file.dat

However, there was a catch - the last line in the file had a "\n" character at the end. The above code removed the last line but kept the newline character intact!

I verified this behavior with the command "wc -l" and that too ignored this newline character.

So after a lot of struggle, I still could not get rid of the extra newline character ...
If you find a solution for this - please let me know - thanks!

BTW, here is a link to a set of very useful one-line sed scripts on the sed sourceforge site.

No comments:


Related Posts Plugin for WordPress, Blogger...