使用shell脚本从文件中提取特定值

人气:605 发布:2022-10-16 标签: shell grep awk sed

问题描述

如何使用shell脚本(例如grepawksed)从文件中提取值。我有以下结构(如下所述),我希望获得仅包含文件第二列的输出文件。 我尝试使用grep: grep -oP 's*U238s**s+[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)?' file >U238_out从整个文件提取所有U238值并将其存储在输出文件(U238_OUT)中。这样,我将获得以下输出:

 U238     *  1.779265E+03
 U238     *  5.418484E-03
 U238     *  1.777156E+03
         ...

但我想获得此结构:

1.779265E+03
5.418484E-03
1.777156E+03

文件结构:

  PERIODICITY : 0
  SYMMETRY    : 0


  MATERIAL MFUEL   
  MEDIUM   MFUEL    VOLUME  8.308106E+05


  *******************************************************
  * ISOTOPE  *   MASS (KG)   *   CIP    *    EQFMASS    *
  *******************************************************
  * U238     *  1.779265E+03 *   28.125 *  0.000000E+00 *

提前感谢。

推荐答案

您的意思是这样的吗?

sed '/s**/!d;s/s*[*][^*]*[*]s*([-+.E0-9]*).*/1/;/^$/d' file.txt

说明

/s**/!d          # delete line not started with [blank]*
;                  # separator for next sed command
s/                 # substitute
s*                # ignore leading blanks
[*]                # search first *
[^*]*[*]           # ignore everything until the next *
s*                # ignore blanks
([-+.E0-9]*)     # save number into arg1 (1)
.*                 # ignore rest of line
/1/               # print only arg1
;                  # separator for next sed command
/^$/d              # ignore empty lines (first 3)

输出

3.099319E+02
1.274088E+01
1.779265E+03
3.789596E+02
1.760032E+02
5.049642E+01
5.002164E+01
4.777184E+00
2.594883E-19
2.594883E-19

430