bash 如何在除最后一行之外的每一行末尾添加逗号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35021524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:11:05  来源:igfitidea点击:

How can I add a comma at the end of every line except the last line?

bashsedcommand-line

提问by Joan Triay

I want add a comma at the end of every line of this kind of file except the last line:

我想在这种文件的每一行末尾添加一个逗号,除了最后一行:

I have this now:

我现在有这个:

{...}
{...}
{...}
{...}

I want this:

我要这个:

{...},
{...},
{...},
{...}

How can I do it with the command line? Is it possible with sed command?

我怎样才能用命令行做到这一点?sed 命令可以吗?

回答by tripleee

The following sedscript has an address expression $!which matches lines which are not the last, and a substituton action s/$/,/which adds a comma next to the end of line.

以下sed脚本具有$!匹配非最后一行的地址表达式,以及s/$/,/在行尾添加逗号的替换操作。

sed '$!s/$/,/' file

(The $in the address refers to the last linewhile the $in the regular expression in the substitution refers to the last character positionon every line. They are unrelated, though in some sense similar.)

$地址中的in 是指最后一行,$替换中的正则表达式中的 in 是指每一行的最后一个字符位置。它们无关,但在某种意义上是相似的。)

This prints the modified contents of fileto standard output; redirect to a different file to save them to a file, or use sed -iif your sedsupports that. (Some variants require an empty argument to the -ioption, notably *BSD / OSX sed.)

这会将修改后的内容打印file到标准输出;重定向到不同的文件以将它们保存到文件中,或者sed -i在您sed支持的情况下使用。(某些变体需要该-i选项的空参数,特别是 *BSD / OSX sed。)

If your task is to generate valid JSON from something which is not, I'm skeptical of this approach. Structured formats should be manipulated with tools which understand structured formats, not basic line-oriented tools; but if this helps you transform line-oriented data towards proper JSON, that's probably a valid use.

如果您的任务是从不是的东西生成有效的 JSON,我对这种方法持怀疑态度。应该使用理解结构化格式的工具来操作结构化格式,而不是基本的面向行的工具;但是,如果这有助于您将面向行的数据转换为正确的 JSON,那么这可能是一个有效的用途。

... As an afterthought, maybe you want to wrap the output in a set of square brackets, too. Then it's actually technically valid JSON (assuming each line is a valid JSON fragment on its own).

...事后想来,也许您也想将输出包装在一组方括号中。那么它实际上是技术上有效的 JSON(假设每一行本身就是一个有效的 JSON 片段)。

sed '1s/^/[/;$!s/$/,/;$s/$/]/' file

On the first line (address expression 1) substitute in an opening square bracket at beginning of line (s/^/[/). On lines which are not the last, add a trailing comma, as above. On the line which isthe last (address expression $) add a closing square bracket at the end of line (s/$/]/). If you prefer newlines to semicolons, that's fine; many seddialects also allow you to split this into multiple -earguments.

在第一行(地址表达式1)中,在行首 ( s/^/[/)处用左方括号替换。在不是最后一行的行上,添加一个尾随逗号,如上。在其上线最后一个(地址表达$)在管线的端部(添加右方括号s/$/]/)。如果你更喜欢换行而不是分号,那很好;许多sed方言还允许您将其拆分为多个-e参数。

回答by Ed Morton

Keep it simple, just use awk:

保持简单,只需使用 awk:

$ awk '{printf "%s%s",sep,
vim <file-name>

:%s/$/,/g
; sep=",\n"} END{print ""}' file {...}, {...}, {...}, {...}

回答by Rajat Kumar Nayak

:%s/,//g

To remove

去除

 sed 's/$/,/' file.txt > file_with_added_commas.txt

回答by bhasan

You can use something like this:

你可以使用这样的东西:

##代码##

This will substitute the end of the line with a ',' and write it to file_with_added_commas.txt

这将用 ',' 替换行尾并将其写入 file_with_ added_commas.txt

For more information refer to: add text at the end of each line

有关更多信息,请参阅:在每行末尾添加文本