在每行末尾添加文本 [英] Add text at the end of each line
问题描述
我在 Linux 命令行上,我有文件
I'm on Linux command line and I have file with
127.0.0.1
128.0.0.0
121.121.33.111
我想要
127.0.0.1:80
128.0.0.0:80
121.121.33.111:80
我记得我的同事为此使用了 sed,但在阅读 sed 手册后仍然不清楚如何在命令行上执行此操作?
I remember my colleagues were using sed for that, but after reading sed manual still not clear how to do it on command line?
推荐答案
您可以尝试使用以下内容:
You could try using something like:
sed -n 's/$/:80/' ips.txt > new-ips.txt
前提是您的文件格式与您在问题中描述的一样.
s///
替换命令匹配(finds)文件中每一行的结尾(使用 $
字符)和然后将 :80
附加(replaces)到每一行的末尾.ips.txt
文件是您的输入文件...而 new-ips.txt
是您新创建的文件(更改的最终结果.)
The s///
substitution command matches (finds) the end of each line in your file (using the $
character) and then appends (replaces) the :80
to the end of each line. The ips.txt
file is your input file... and new-ips.txt
is your newly-created file (the final result of your changes.)
此外,如果您有一个 IP 号码列表,这些 IP 号码碰巧已经附加了端口号,(如 Vlad 和 aragaer 给出的,)你可以尝试使用类似的东西:
Also, if you have a list of IP numbers that happen to have port numbers attached already, (as noted by Vlad and as given by aragaer,) you could try using something like:
sed '/:[0-9]*$/ ! s/$/:80/' ips.txt > new-ips.txt
因此,例如,如果您的输入文件看起来像这样(注意 :80
):
So, for example, if your input file looked something like this (note the :80
):
127.0.0.1
128.0.0.0:80
121.121.33.111
最终的结果应该是这样的:
The final result would look something like this:
127.0.0.1:80
128.0.0.0:80
121.121.33.111:80
这篇关于在每行末尾添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!