Unix – How to append text to every line of a file except for the header line/first line

sed

I have a file say abc.txt with following data (real data has many records,say around 200)

Sno | Name
1 | Jack
2 | Jill
3 | June

Now how do I append text to make my file look like this,

Sno | Name | Place
1 | Jack | Paris
2 | Jill | Paris
3 | June | Paris

I tried replacing globally ,but first line should be appended with different text.So please help me out guys.

Best Answer

Here are three options:

  • awk and its variants (gawk, mawk etc.):

    awk '{if(NR==1){print $0,"| Place"} else{print $0,"| Paris"}}' file.txt
    
  • Perl:

    perl -lne '$.==1 ? print "$_ | Place" : print "$_ | Paris"' file.txt
    
  • sed

    sed '1 s/$/ | Place/; 1! s/$/ | Paris/' file.txt 
    
Related Question