35

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?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
user2647888
  • 721
  • 1
  • 13
  • 22

4 Answers4

72

There are many ways:

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

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 < file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 5
    All the solutions work, assuming the lines end with a line feed ("\n"). I had a file coming from Windows with carriage return and line feed ("\r\n"), and I got the desired comment at the beginning of the line . I just converted the file with dos2unix and all the solutions work fine. – nephewtom Sep 07 '16 at 10:55
  • 1
    MacOS only uses Carriage Return (`\r`), hence, you have to write `sed 's/\r/ | COUNTRY/' file` there. – Jan Aug 25 '21 at 16:26
5

Another awk

awk '$0=$0" | COUNTRY"' file
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • I thought of this too, this might be the shortest awk solution... +1 – Kent Apr 15 '14 at 09:22
  • 3
    Just for fun, I compared the performance with a 20M lines file. `time awk '{print $0, "| COUNTRY"' a &>/dev/null` takes around 0.003s, while `time awk '$0=$0" | COUNTRY"' a &>/dev/null` around 4.03s. I guess the assignment consumes way more extra time. – fedorqui Apr 15 '14 at 10:06
  • @fedorqui Interesting. Shortness is not always the best :) – Jotne Apr 15 '14 at 10:34
  • @Jotne indeed, I did not expect such result. But of course, it is an edge case and your answer is perfectly valid. – fedorqui Apr 15 '14 at 12:38
  • 2
    I don't get this running properly... I get the | COUNTRY at the beginning of the line... Why is that? – nephewtom Sep 07 '16 at 10:45
  • Are you using a MacOS computer? In that case, you have to use `\r` instead of `$`. At least, it did for me the trick. – Jan Aug 25 '21 at 16:28
4

For a more obfuscated approach:

yes '| COUNTRY' | sed $(wc -l < file)q | paste -d ' ' file -
fedorqui
  • 275,237
  • 103
  • 548
  • 598
William Pursell
  • 204,365
  • 48
  • 270
  • 300
4

You can also use xargs with echo for this:

< file xargs -d "\n" -rI % echo '% | COUNTRY'

This will make xargs take each line of file and pass it one at a time† to the specified echo command, replacing the % (or whatever character you choose) with the input line.

† By default xargs will pass multiple lines of input to a single command, appending them all to its argument list. But specifying -I % makes xargs put the input at the specified place in the command, and only put one line there at a time, running echo as many times as required.

Smylers
  • 1,673
  • 14
  • 18