I am trying to use AWK for manipulating two files. Well I am actually interpolating between the two. Since I am not that much familiar how to process two files simultaneously with AWK I first do a paste
paste file_1 file_2 > mixed_file
each of those files contains lines of the following 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 like 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 is my problem: when I try doing something like
awk ' / EG = / {$1=0.5*($1+$7)} {print $0} ' mixed_file
I get my 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 that the change in $1 is most probably due to mixing floats with characters. It is in principle possible to explicitly define the format and avoid this problem by using "printf", but I guess that more elegant solution exists.
AWK experts I would appreciate your help,
Thank you
Alex
Reassigning back to $0 is what you're after:
awk '/ EG = / {$0 = sprintf(" %7.5E%s", 0.5*($1+$7), substr($0,13))} 1' mixed_file
When you assign to a field awk re-formats the line.
It does that using the value of OFS
(by default a space).
Your input had multiply spaced columns. awk doesn't recreate that.
You can pipe to column -t
to recreate that sort of output if you want.
You can also set OFS
to a tab (\t
) if that gets you the output you want.
I don't know of a way to get awk itself to preserve input formatting though (though I think that the RT
variable in newer versions of gawk might allow that to be done manually).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With