[英]Extract specific values from a file using shell script
如何使用 shell 脚本(例如grep
、 awk
或sed
)从文件中提取值。 我有以下结构(如下所述),我想获得一个只有文件第二列的输出文件。 我尝试使用 grep: grep -oP '\\s*U238\\s*\\*\\s+[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?' file >U238_out
grep -oP '\\s*U238\\s*\\*\\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 *
提前致谢。
您可以使用:
awk '$2=="U238" {print $4}' file > U238_out
或者
awk '$2~/^U238$/ {print $4} file > U238_out
如果 U238 在第二个字段中,则打印第四个字段:
1.779265E+03
你的意思是这样的?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.