Add text at the end of each line

前端 未结 7 2032
死守一世寂寞
死守一世寂寞 2020-11-28 20:24

I\'m on Linux command line and I have file with

127.0.0.1
128.0.0.0
121.121.33.111

I want

127.0.0.1:80
128.0.0.0:80
121.121         


        
相关标签:
7条回答
  • 2020-11-28 20:56

    You could try using something like:

    sed -n 's/$/:80/' ips.txt > new-ips.txt
    

    Provided that your file format is just as you have described in your question.

    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.)


    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
    

    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
    
    0 讨论(0)
提交回复
热议问题