如何在unix中每行末尾添加文本 [英] How to add text at the end of each line in unix

查看:38
本文介绍了如何在unix中每行末尾添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做某些文本处理操作,最后能够得到一个这样的文件

I am doing certain text processing operations and finally able to get a file something like this

india
sudan
japan
france

现在我想在上面的文件中添加一个注释,就像在最终文件中一样,它应该是

now I want to add a comment in the above file like in the final file it should be something like

india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

喜欢整个文件中的相同评论.我该怎么做?

like a same comment across the whole file. How do I do this?

推荐答案

方法很多:

sed:用给定的文本替换$(行尾).

sed: replace $ (end of line) with the given text.

$ sed 's/$/ | COUNTRY/' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

awk:打印一行加上给定的文本.

awk: print the line plus the given text.

$ awk '{print $0, "| COUNTRY"}' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

最后,在纯 bash 中:逐行读取并将其与给定的文本一起打印.请注意,如 为什么使用 shell 循环处理文本被认为是不好的做法?

Finally, in pure bash: read line by line and print it together with the given text. Note this is discouraged as explained in Why is using a shell loop to process text considered bad practice?

$ while IFS= read -r line; do echo "$line | COUNTRY"; done < file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

这篇关于如何在unix中每行末尾添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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