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

查看:134
本文介绍了如何在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?

推荐答案

有很多种方式:

庆典:逐行读取,并与给定的文本一起打印出来

Pure bash: read line by line and print it together with the given text.

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

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 $0, "| COUNTRY"}' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

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

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