0

I can't seem to find the right syntax for this awk command to find the min and max of a column of values and add (-1*min) value to all column values (effectively zeroing my data). Would appreciate any help! Thank you very much!

awk 'NR==1 {max=$2; min=$2} $2>=max {max=$2} $2<=min {min=$2}' <  my_data.dat | awk '{printf("%-5d%+13.5f \n", NR, $2+(-1*min))}'  > my_new_data.dat
1

You can go through a file twice with the same syntax you would use for multiple files and just using the same file twice.

awk 'NR==1 { max=$1 ; min=$1 }
     FNR==NR { if ($1>=max) max=$1 ; $1<=min?min=$1:0 ; next}
     { $2=($1-min)/(max-min) ; print }' file file

Detailed explanation:

NR==1 { max=$1 ; min=$1 } - adopt values of first record, you did this already

FNR==NR - awk with multiple input files will have a NR value for the overall record index number (i.e. as if all files were concatenated) and a FNR value for the record index number of the current file. If these match, we are processing the first file, i.e. this block is executed for the first file only.

if ($1>=max) max=$1 - awk's if statement syntax is if (condition) action ; else if (condition) action ; else action. Obviously if else and else statements are optional.

$1<=min?min=$1:0 - and alternate if statement syntax: condition?yes-action:no-action. This requires and else-action thus the meaningless 0. Just for your interest.

next - skip to the next file, so that the following block is executed for the second file only.

$2=($1-min)/(max-min) ; print - do a normalisation to [0,1] (you have no use for max in your example so I used this instead...) and print data.

  • 1 -79687.72398 2 -80244.28699 3 -80366.94163 4 -78051.28573 5 -75530.96294 6 -75258.26745 7 -79026.36821 8 -79416.81753 9 -79565.89216 10 -79639.14779 – Nadia Nov 21 '16 at 13:00
  • Yes? Take column $2 ( instead of $1 as I did ) and adapt your desired calculation for the output value. – Fiximan Nov 21 '16 at 13:11
  • Thank you so much for the help. It works perfectly now and I learned how to use "next" and the same file twice -- that was the trick! Thank you. – Nadia Nov 21 '16 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.