从最后一行删除逗号 [英] remove comma from last line

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

问题描述

如何从文件的最后一行删除逗号? 这是文件:

How do I remove the comma from the last line of the file? Here is the file:

# cat ox_data_archive_r_20120727.json
{"name": "secondary_ua","type":"STRING"},
{"name": "request_ip","type":"STRING"},
{"name": "cb","type":"STRING"},

以下将删除所有3行中的逗号.

The following will remove the comma from all 3 lines.

# sed 's/,$/\ /' ox_data_archive_r_20120727.json
{"name": "secondary_ua","type":"STRING"}
{"name": "request_ip","type":"STRING"}
{"name": "cb","type":"STRING"}

我只需要删除最后一个逗号.所以输出应该看起来像这样……

I need to remove the last comma only. So the output should look something like this...

# cat newfile.json
{"name": "secondary_ua","type":"STRING"},
{"name": "request_ip","type":"STRING"},
{"name": "cb","type":"STRING"}

推荐答案

$ cat input.txt
{"name": "secondary_ua","type":"STRING"},
{"name": "request_ip","type":"STRING"},
{"name": "cb","type":"STRING"},
$ sed '$s/,$//' < input.txt
{"name": "secondary_ua","type":"STRING"},
{"name": "request_ip","type":"STRING"},
{"name": "cb","type":"STRING"}

摘录自 GNU sed 的文档:

$:此地址与输入的最后一个文件的最后一行或指定了-i-s选项的每个文件的最后一行匹配.

$: This address matches the last line of the last file of input, or the last line of each file when the -i or -s options are specified.

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

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