Removing last comma using sed
I 开发者_JS百科have a text file and I want to remove the last comma , from the second last line if exist.
a, b, c, d,
m, n, p,
x, y, z,
);
The comma after z needs be removed only.
Give this a try:
sed 'N;$s/,\n/\n/;P;D' inputfile
It removes the comma from the end of the next-to-last line. This assumes that there is nothing after the last line you show.
If you need to key on the contents of that line instead, then this should work:
sed 'N;/)\;/s/,\n/\n/;P;D' inputfile
You could also do this with awk:
awk 'l2{print l2} {l2=l1; l1=$0} END{sub(/,$/,"",l2); print l2 "\n" l1}' file
精彩评论