user2647888 user2647888 - 3 months ago 6
Bash Question

How to add text at the end of each line in unix

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?

Answer

There are many ways:

Pure bash: read line by line and print it together with the given text.

$ while IFS= read -r line; do echo "$line | COUNTRY"; done < file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

sed: replace $ (end of line) with the given text.

$ sed 's/$/ | COUNTRY/' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

awk: print the line plus the given text.

$ awk '{print $0, "| COUNTRY"}' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY