Thursday, 15 July 2010

linux - AWK: Preserving the format when printing "print $0" -



linux - AWK: Preserving the format when printing "print $0" -

i trying utilize awk manipulating 2 files. interpolating between two. since not much familiar how process 2 files simultaneously awk first paste

paste file_1 file_2 > mixed_file

each of files contains lines of next type

2.02646e+12 err 7.39921e+09 eg = 67 1.82357e+12 err 7.01570e+09 eg = 68 8.65566e+11 err 4.35764e+09 eg = 69

the newly created file 'mixed_file' looks this

2.02646e+12 err 7.39921e+09 eg = 67 2.02646e+12 err 7.39921e+09 eg = 67 1.82357e+12 err 7.01570e+09 eg = 68 1.82357e+12 err 7.01570e+09 eg = 68 8.65566e+11 err 4.35764e+09 eg = 69 8.65566e+11 err 4.35764e+09 eg = 69

and here problem: when seek doing

awk ' / eg = / {$1=0.5*($1+$7)} {print $0} ' mixed_file

i formatting totally wrong

2026460000000 err 7.39921e+09 eg = 67 2.02646e+12 err 7.39921e+09 eg = 67 1823570000000 err 7.01570e+09 eg = 68 1.82357e+12 err 7.01570e+09 eg = 68 865566000000 err 4.35764e+09 eg = 69 8.65566e+11 err 4.35764e+09 eg = 69

i presume alter in $1 due mixing floats characters. in principle possible explicitly define format , avoid problem using "printf", guess more elegant solution exists.

awk experts appreciate help,

thank you

alex

reassigning $0 you're after:

awk '/ eg = / {$0 = sprintf(" %7.5e%s", 0.5*($1+$7), substr($0,13))} 1' mixed_file

linux awk formatting text-files

No comments:

Post a Comment