开发者

Removing last comma using sed

开发者 https://www.devze.com 2023-02-01 08:15 出处:网络
I 开发者_JS百科have a text file and I want to remove the last comma , from the second last line if exist.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号