gpt4 book ai didi

sed - 从最后一行删除逗号

转载 作者:行者123 更新时间:2023-12-04 16:18:01 30 4
gpt4 key购买 nike

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

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

下面将删除所有3行中的逗号。
# sed 's/,$/\ /' ox_data_archive_r_20120727.json
{"name": "secondary_ua","type":"STRING"}
{"name": "request_ip","type":"STRING"}
{"name": "cb","type":"STRING"}

我只需要删除最后一个逗号。所以输出应该看起来像这样...
# 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的文档中:

$: 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.

关于sed - 从最后一行删除逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12390134/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com