且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除每 4 行末尾的逗号

更新时间:2023-12-03 21:14:22

使用 GNU sed:

With GNU sed:

sed '4~4 s/,$//' filename

不过,我必须指出,在您的示例输出中,每四行末尾没有逗号.

I have to point out, though, that in your example output there's no comma at the end of every fourth line.

这应该对你有用,因为问题被标记为linux",而 Linux 几乎总是带有 GNU sed.为了完整起见:使用 BSD sed(在 Mac OS X 和 *BSD 上可以找到),4~4 模式不起作用(它是 GNU 扩展).在那里你可以做类似的事情

This should work for you, since the question is tagged "linux" and Linux very nearly always comes with GNU sed. For the sake of completeness: with BSD sed (as found on Mac OS X and *BSD), the 4~4 pattern does not work (it is a GNU extension). There you could do something like

sed 'n;n;n;s/,$//' filename

...每次获取并打印额外的三行并删除第四行末尾的逗号(除非在获取第四行之前到达输入的末尾).

...which fetches and prints three extra lines every time and removes the comma at the end of the fourth (unless the end of the input was reached before a fourth line could be fetched).

或者,你可以使用 awk

Alternatively, with awk you could use

awk 'NR % 4 == 0 { sub(/,$/, "") } 1' filename