[英]How do I extract the values from this data using bash and awk?
我抓了这些,如何提取值?
... cavity_2mgl_wt_strip57001.out: Total cavity volume (A3) : ( 1.240E+01) cavity_2mgl_wt_strip58001.out: Total cavity volume (A3) : ( 2.408E+00) cavity_2mgl_wt_strip60001.out: Total cavity volume (A3) : ( 4.935E+00) cavity_2mgl_wt_strip61001.out: Total cavity volume (A3) : ( 1.319E+00) cavity_2mgl_wt_strip63001.out: Total cavity volume (A3) : ( 1.532E-01) cavity_2mgl_wt_strip64001.out: Total cavity volume (A3) : ( 1.137E+01) ...
我需要以粗体显示文件名中的索引#:
cavity_2mgl_wt_strip76001.out: Total cavity volume (A3) : ( 1.276E+01)
我需要括号中的数字:
cavity_2mgl_wt_strip76001.out: Total cavity volume (A3) : ( 1.276E+01)
$ ..<commands>.. | awk -F"[:)(]" '{gsub(".*strip|.out","",$1);print $1,$(NF-1)}'
57001 1.240E+01
58001 2.408E+00
60001 4.935E+00
61001 1.319E+00
63001 1.532E-01
64001 1.137E+01
或者如果您的grepped值已经存在于文件中
$ awk -F"[:)(]" '{gsub(".*strip|.out","",$1);print $1,$(NF-1)}' file
sed 's/.*strip\(.*\).out.*(\([^:].*\))/\1 \2/' file
当然perl比awk短得多。 我不知道你的格式有多灵活; 为了以防万一,我放了一些通配符:
perl -ne 'if ($_ =~ /cavity_[0-9]+mgl_wt_strip([0-9]+)\.out:[^:]+: *\( *([0-9]+\.[0-9]+E[+-][0-9]+)\)/) {print "$1 $2\n"}' in.txt > out.txt
用sed怎么样?
sed -e 's/.*strip\([^.]*\).*( *\([^ )]*\) *).*/\1 \2/' infile > outfile
第一次捕获是“条带”和下一个点之间的线的一部分。 然后跳过直到最后一个开始括号的行。 第二次捕获是最后一对括号之间的数字(删除了任何前导和尾随空格)。
纯粹的bash:
while read line; do
tmp="${line#*strip}"; index="${tmp%.out*}"
tmp="${line##*(}"; value="${tmp%)*}"
printf "%s:%s\n" "$index" "$value"
done < file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.