I want to find the last line of text in a file, and delete the comma at the end of it. I asked about this already, but, after I got an answer I realized my question was not specific enough.
This sed
command will go to the last line of a file and take action on it. In my case, I want to remove the trailing comma:
sed -i '$ s/",/"/g' file.txt
So this:
blah blah blah,
blah blah blah,
blah blah blah,
... becomes this:
blah blah blah,
blah blah blah,
blah blah blah
However, this won't work if there are blank lines after the last line of text in the file.
I've been searching for ways to get the last line of text but haven't come up with anything that I can understand and apply. I've also looked for ways to remove all trailing blank lines, and found this command:
sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' *.txt
But it doesn't work for me (it just seems to output the contents of my files on the command line). In any case, it's inelegant. I'd rather not delete the trailing blank lines, it would be much better to just identify the last line with text in it and act on that.
How do I remove the comma from the last line of text in multiple files in a directory?
sed
, specifically? Wouldawk
orperl
be acceptable? – Reed Kraft-Murphy Feb 21 '13 at 04:26sed
. As long as what I use is standard on any default installation of Ubuntu. – Questioner Feb 21 '13 at 06:19