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?
像整个文件中的相同评论。我该怎么做呢?
4 个解决方案
#1
50
There are many ways:
有很多方法:
sed
: replace $
(end of line) with the given text.
sed:用给定的文本替换$(行尾)。
$ sed 's/$/ | COUNTRY/' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY
awk
: print the line plus the given text.
awk:打印行加上给定的文本。
$ awk '{print $0, "| COUNTRY"}' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY
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?
最后,在纯粹的bash中:逐行读取并将其与给定文本一起打印。请注意,这是不鼓励的,如为什么使用shell循环来处理被视为不良做法的文本?
$ while IFS= read -r line; do echo "$line | COUNTRY"; done < file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY
#2
4
For a more obfuscated approach:
对于更加混淆的方法:
yes '| COUNTRY' | sed $(wc -l < file)q | paste -d ' ' file -
#3
3
Another awk
另一个awk
awk '$0=$0" | COUNTRY"' file
#4
1
You can also use xargs
with echo
for this:
你也可以使用带有echo的xargs:
xargs -d "\n" -rI % echo '% | COUNTRY'
This will make xargs
take each line of input and pass it one at a time† to the specified echo
command, replacing the %
(or whatever character you choose) with the input line.
这将使xargs获取每行输入并将其一次传递给指定的echo命令,用输入行替换%(或您选择的任何字符)。
† By default xargs
will pass multiple lines of input to a single command, appending them all to its argument list. But specifying -I %
makes xargs
put the input at the specified place in the command, and only put one line there at a time, running echo
as many times as required.
†默认情况下,xargs会将多行输入传递给单个命令,并将它们全部附加到其参数列表中。但是指定-I%会使xargs将输入放在命令中的指定位置,并且一次只放置一行,根据需要多次运行echo。
#1
50
There are many ways:
有很多方法:
sed
: replace $
(end of line) with the given text.
sed:用给定的文本替换$(行尾)。
$ sed 's/$/ | COUNTRY/' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY
awk
: print the line plus the given text.
awk:打印行加上给定的文本。
$ awk '{print $0, "| COUNTRY"}' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY
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?
最后,在纯粹的bash中:逐行读取并将其与给定文本一起打印。请注意,这是不鼓励的,如为什么使用shell循环来处理被视为不良做法的文本?
$ while IFS= read -r line; do echo "$line | COUNTRY"; done < file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY
#2
4
For a more obfuscated approach:
对于更加混淆的方法:
yes '| COUNTRY' | sed $(wc -l < file)q | paste -d ' ' file -
#3
3
Another awk
另一个awk
awk '$0=$0" | COUNTRY"' file
#4
1
You can also use xargs
with echo
for this:
你也可以使用带有echo的xargs:
xargs -d "\n" -rI % echo '% | COUNTRY'
This will make xargs
take each line of input and pass it one at a time† to the specified echo
command, replacing the %
(or whatever character you choose) with the input line.
这将使xargs获取每行输入并将其一次传递给指定的echo命令,用输入行替换%(或您选择的任何字符)。
† By default xargs
will pass multiple lines of input to a single command, appending them all to its argument list. But specifying -I %
makes xargs
put the input at the specified place in the command, and only put one line there at a time, running echo
as many times as required.
†默认情况下,xargs会将多行输入传递给单个命令,并将它们全部附加到其参数列表中。但是指定-I%会使xargs将输入放在命令中的指定位置,并且一次只放置一行,根据需要多次运行echo。