awk科学符号输入 [英] awk scientific notation input

查看:90
本文介绍了awk科学符号输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让awk从文件中读取科学计数法有点麻烦,这是一些示例输入:

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

我正在使用awk来格式化该文件,以便使用gnuplot使用以下命令进行绘图:

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

但是该算法无法正常工作,经过几次测试,我意识到awk似乎无法正确解释输入格式,我如何指定awk使用的格式?

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?

推荐答案

如果您无权安装其他awk,则可以定义和 使用函数来解析工程符号

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
% 

这篇关于awk科学符号输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆