问题
I'm having a bit of trouble with getting awk to read scientific notation from a file, here is some example input:
#Plane Turn Real Imaginary
HOR 1 0.0000e+00 -2.1885e-07
HOR 1 4.8481e-08 -8.1221e-08
HOR 1 4.0934e-08 -6.0784e-08
HOR 1 3.5707e-08 -5.1223e-08
HOR 1 3.1664e-08 -4.5608e-08
HOR 1 2.8268e-08 -4.1964e-08
HOR 1 2.5242e-08 -3.9469e-08
HOR 1 2.2429e-08 -3.7707e-08
HOR 1 1.9731e-08 -3.6430e-08
HOR 1 1.7082e-08 -3.5479e-08
I'm using awk to format this file for plotting with gnuplot using the following command:
gnuplot "awk '{if($2==1){printf "%.4e\n",sqrt($3^2+$4^2)}}' bathtub_values.dat" w l
However the arithmetic is not working correctly and after a few tests I realized that awk doesn't seem to correctly interpret the input format, how can I specify to awk the format to use?
回答1:
If you're not entitled to install a different awk, you can define and use a function to parse the engineering notation
% awk 'function parse_en(eng_num) {
split(eng_num,en,/[eEdD]/); return en[1]*10**en[2]; }
$1 !~ /#/ {
printf "%.4e\n",sqrt(parse_en($3)^2+parse_en($4)^2)}
' bathtub_values.dat
2.1885e-07
9.4590e-08
7.3282e-08
6.2440e-08
5.5522e-08
5.0597e-08
4.6850e-08
4.3873e-08
4.1430e-08
3.9377e-08
%
来源:https://stackoverflow.com/questions/24517880/awk-scientific-notation-input