Calculate 2nd Column Based on 1st Column

Dear All,

I have input file like this.

input.txt

CE2_12-15          3950.00  589221.0 9849709.0   768.0   CE2_12_2012
CE2_12-15          3949.00  589199.0 9849721.0   768.0   CE2_12_2012
CE2_12-15          3948.00  589178.0 9849734.0   768.0   CE2_12_2012
CE2_12-52          1157.00  570361.0 9845167.0   850.9   CE2_12_2012
CE2_12-52          1158.00  570376.0 9845187.0   855.1   CE2_12_2012
CE2_12-52          1159.00  570391.0 9845207.0   859.3   CE2_12_2012
CE2_12-54          2396.00  579290.0 9854101.0   853.0   CE2_12_2012
CE2_12-54          2397.00  579305.0 9854121.0   855.0   CE2_12_2012
CE2_12-54          2398.00  579321.0 9854140.0   857.5   CE2_12_2012
....
....
....

I need to calculate 2nd column of input with different function, based on 1st column.
if colum 1st contain CE2_12-15 do (4086-$1)*2
if colum 1st contain CE2_12-52 do ($1-836)*2
if colum 1st contain CE2_12-54 do ($1-1088)*2
...
...

desired output:

output.txt

CE2_12-15           272.00  589221.0 9849709.0   768.0   CE2_12_2012
CE2_12-15           274.00  589199.0 9849721.0   768.0   CE2_12_2012
CE2_12-15           276.00  589178.0 9849734.0   768.0   CE2_12_2012
CE2_12-52           642.00  570361.0 9845167.0   850.9   CE2_12_2012
CE2_12-52           644.00  570376.0 9845187.0   855.1   CE2_12_2012
CE2_12-52           646.00  570391.0 9845207.0   859.3   CE2_12_2012
CE2_12-54          2616.00  579290.0 9854101.0   853.0   CE2_12_2012
CE2_12-54          2618.00  579305.0 9854121.0   855.0   CE2_12_2012
CE2_12-54          2620.00  579321.0 9854140.0   857.5   CE2_12_2012
....
....
....

I tried coding like this, but unsucesfull :

awk '{
if ( $1 =="CE2_12-15")
        print ((4086-$1)*2)
else if ($1 =="CE2_12-52")
    print (($1-846)*2)
else if ($1 =="CE2_12-54")
    print (($1-1088)*2)
}' input.txt > output.txt

Thanks for advance,

Attila

Try

awk '$1=="CE2_12-15"{$2=(4086-$2)*2}
$1=="CE2_12-52"{$2=($2-836)*2}
$1=="CE2_12-54"{$2=($2-1088)*2}1' file
1 Like

Hi,

can you try with $2 instead of $1 in all the print functions-- print ((4086-$2)*2)

Thanks pamu & msathees,

Solved