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

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


There are many ways:

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

$ sed 's/$/ | COUNTRY/' fileindia | COUNTRYsudan | COUNTRYjapan | COUNTRYfrance | COUNTRY

awk: print the line plus the given text.

$ awk '{print $0, "| COUNTRY"}' fileindia | COUNTRYsudan | COUNTRYjapan | COUNTRYfrance | COUNTRY

Finally, in pure bash: read line by line and print it together with the given text. Note this is discouraged as explained in Why is using a shell loop to process text considered bad practice?

$ while IFS= read -r line; do echo "$line | COUNTRY"; done < fileindia | COUNTRYsudan | COUNTRYjapan | COUNTRYfrance | COUNTRY


Another awk

awk '$0=$0" | COUNTRY"' file


For a more obfuscated approach:

yes '| COUNTRY' | sed $(wc -l < file)q | paste -d ' ' file -