簡體   English   中英

如何使用bash和awk從這些數據中提取值?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM