Insert string or text at the end of the line.

Insert string or text at the end of the line.

Some time we need to insert text or string at the end of the line in a file. This can be done using two methods “awk” and “sed” command. Below are the examples:

Insertion with sed at the end of the line

If you want to insert the text during the execution of the command use -i option.

 sed -i 's/$/.pdf/'  my_input_filename

In the above example $ represents the end of the line and will add “.pdf” at the end of every line of input file.

If you want to redirect the output to another file use the redirect option.

sed 's/$/.pdf/' my_input_filename > newfilename

This will insert “.pdf” at the end of every line and redirect its to a new specified file.

Insert at the end of the line conatining a pattern

 sed '/pattern/ s/$/ .pdf/' test > test.txt

The first part is a pattern to find and the second part is an ordinary sed’s substitution using $ for the end of a line.

Example with awk:

The following example will add “.pdf” at the end of each line in a file.

awk ‘{ print $0  “.pdf” }’ my_input_filename  > newfilename

Sed and awk are most useful text manipulation tools in Linux.