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

前端 未结 4 1450
野性不改
野性不改 2020-12-02 20:31

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

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 21:09

    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
    

提交回复
热议问题