Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add text at the end of each line

People also ask

How do I put text on each line?

Step 2: To add Text at the Start of each line Press Ctrl+F to open Find window, click on the Replace tab, check that you have selected Regular Expression option, now add ^ in the Find textbox and the text you want at the start of each line in the Replace textbox, and click Replace all.

How do I add text before every line?

So press CTRL + H and in Find what , you enter ^. In Replace with, you enter the text you want to add before each new line. At the bottom of the dialog window, you should make sure Regular expression is ticked.


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

Concise version of the sed command:

sed -i s/$/:80/ file.txt

Explanation:

  • sed stream editor
  • -i in-place (edit file in place)
  • s substitution command
  • /replacement_from_reg_exp/replacement_to_text/ statement
  • $ matches the end of line (replacement_from_reg_exp)
  • :80 text you want to add at the end of every line (replacement_to_text)
  • file.txt the file name

How can this be achieved without modifying the original file?

If you want to leave the original file unchanged and have the results in another file, then give up -i option and add the redirection (>) to another file:

sed s/$/:80/ file.txt > another_file.txt

sed 's/.*/&:80/'  abcd.txt >abcde.txt

If you'd like to add text at the end of each line in-place (in the same file), you can use -i parameter, for example:

sed -i'.bak' 's/$/:80/' foo.txt

However -i option is non-standard Unix extension and may not be available on all operating systems.

So you can consider using ex (which is equivalent to vi -e/vim -e):

ex +"%s/$/:80/g" -cwq foo.txt

which will add :80 to each line, but sometimes it can append it to blank lines.

So better method is to check if the line actually contain any number, and then append it, for example:

ex  +"g/[0-9]/s/$/:80/g" -cwq foo.txt

If the file has more complex format, consider using proper regex, instead of [0-9].


  1. You can also achieve this using the backreference technique

    sed -i.bak 's/\(.*\)/\1:80/' foo.txt
    
  2. You can also use with awk like this

    awk '{print $0":80"}' foo.txt > tmp && mv tmp foo.txt
    

Using a text editor, check for ^M (control-M, or carriage return) at the end of each line. You will need to remove them first, then append the additional text at the end of the line.

sed -i 's|^M||g' ips.txt
sed -i 's|$|:80|g' ips.txt