0

I have a text file with 3 columns of data. However at random times in the various files there is a change in the in the observed unit from ppn to ppb resulting in the need of a conversion factor and multiplication by 1000.

actual data                        needed data look
20101001,01:00,0.3                 20101001,01:00,0.3,300.000
20110103,10:00,212.67              20110103,10:00,212.670,212.670

I have a awk command to print all original and add a fourth column with the conversion.

The only issue is it prints everything everything in the third column by 1000 and print to the fourth column. The command is below....

awk -F ',' '{printf "%s %s %.3f %.3f\n", $1,$2,$3,$3*1000}' temp7.tmp > County001-CO-0012.out

How can I can only values between 2 and -1 in column #3 only get multiplied by 1000 and other wise print the orignal value of column 3 in column 4?

0
2

You can do this in the awk code:

{
    # if $3 is between -1 and 2, multiply by 1000
    converted = $3 * (-1 <= $3 && $3 <= 2 ? 1000 : 1)
    printf "%s %s %.3f %.3f\n", $1, $2, $3, converted
}
0
awk -F "," '{if($NF >=-1 && $NF <=2){$4=$3*1000;print }else{$4=$3;print}}' filename

output

20101001 01:00 0.3 300
20110103 10:00 212.67 212.67

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.