Manipulate all rows except header, but header should be output as well

Hello There...

I have a sample input file ..

 
number:department:amount
125:Market:125.23
126:Hardware store:434.95
127:Video store:7.45
128:Book store:14.32
129:Gasolline:16.10

I will be doing some manipulations on all the records except the header, but the header should always be at the top in the output. I want the file to be sorted (asc order on 1st field) and add 1000 as bonus to the field 3 (e.g. 125.23 + 1000).

I tried below:

awk 'BEGIN{FS=OFS=":"}{if( NR != 1){$3+=1000}print}' file

How can I sort the file? I know I could use sort but how can I sort all rows except the header and header should appear in sorted output?

I am thinking of doing something like this sort file | awk...

any suggestions?

regards,
juzz4fun

You could print out the header directly and then just work on the rest:

$ head -1 infile; awk 'BEGIN{FS=OFS=":"} NR > 1 {$3+=1000; print}' infile| sort -t: -k3n
number:department:amount
127:Video store:1007.45
128:Book store:1014.32
129:Gasolline:1016.1
125:Market:1125.23
126:Hardware store:1434.95
1 Like

Thanks :slight_smile: