删除每 4 行末尾的逗号 [英] Remove the comma on at the end of every 4th line

查看:49
本文介绍了删除每 4 行末尾的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

她是输出的一个例子.我需要删除每四行末尾的逗号.

Her is a example of the output. I need to remove the comma at the end of every fourth line.

"tester1",
"SERVICE_TICKET_CREATED",
"Thu Mar 19 23:27:57 UTC 2015",
"111.111.11.1"
"tester1",
"SERVICE_TICKET_CREATED",
"Fri Mar 20 00:31:59 UTC 2015",
"111.111.11.1"

我想做的是

"tester1",
"SERVICE_TICKET_CREATED",
"Thu Mar 19 23:27:57 UTC 2015",
"111.111.11.1"
"tester1",
"SERVICE_TICKET_CREATED",
"Fri Mar 20 00:31:59 UTC 2015",
"111.111.11.1"

推荐答案

使用 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

这篇关于删除每 4 行末尾的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆