bash sed 命令只为 unix 中的最后一行添加文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26298405/
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
sed command to add text for only last line in unix
提问by Mars
I want sed command which will add comma to only last line e.g i have text file which contains
我想要 sed 命令,它只会在最后一行添加逗号,例如我有包含
"872709"
"872700"
"145"
"872808B"
"8729029921"
"879B"
"87290"
"AirHo9"
"Ait22"
"DVDSept22"
"Gr929"
want to add comma at last line
想在最后一行添加逗号
"872709"
"872700"
"145"
"872808B"
"8729029921"
"879B"
"87290"
"AirHo9"
"Ait22"
"DVDSept22"
"Gr929",
回答by anubhava
You can use sed
:
您可以使用sed
:
sed '$s/$/,/' file
"872709"
"872700"
"145"
"872808B"
"8729029921"
"879B"
"87290"
"AirHo9"
"Ait22"
"DVDSept22"
"Gr929",
To make it save changes inline use:
要使其保存内联更改,请使用:
sed -i.bak '$s/$/,/' file
回答by nu11p01n73R
sed -r '$ s/([a-zA-Z0-9"]*)/,/' inputfile
The $
mathces the last line in the file
该$
文件在符合项目的最后一行
([a-zA-Z0-9"]*
matches charactes numbers or " and the matched patter is saved in \1
back reference.
([a-zA-Z0-9"]*
匹配字符数字或 " 并且匹配的模式保存在\1
反向引用中。
\1,
is the mathced pattern + commaa wich is the substituion pattern
\1,
是数学模式 + 逗号,这是替代模式
test
测试
"872709"
"872700"
"145"
"872808B"
"8729029921"
"879B"
"87290"
"AirHo9"
"Ait22"
"DVDSept22"
"Gr929",
回答by Casimir et Hippolyte
No need to use sed:
无需使用 sed:
echo -n ',' >> file