从文本文件中打印特定模式后的列

时间:2013-06-26 14:23:02

标签: regex perl sed awk pattern-matching

我有这些数据。我想打印A,C和D后面的数字。有没有办法打印这些列?

 (((A 0.0400213  B 0.0400213  0.00737467  C 0.047396  0.03466  D 0.082056 ;
 ((C 0.0275027  (A 0.023266  B 0.023266  0.00423667  0.0131187  D 0.0406213 ;
 ((C 0.0310553  (B 0.0210907  A 0.0210907  0.00996467  0.0222647  D 0.05332 ;
 ((C 0.03491  (A 0.020474  B 0.020474  0.014436  0.0221067  D 0.0570167 ;
 ((C 0.0485573  (A 0.00938133  B 0.00938133  0.039176  0.00335133  D 0.0519087 ;

如果我想得到A
之后的数字 我的预期结果将是:

0.0400213,
0.023266,
0.0210907,
0.020474,
0.00938133

2 个答案:

答案 0 :(得分:3)

这应该有效:

awk '{for(i=1;i<=NF;i++) if($i~/A|C|D/) printf $++i FS; print ""}' file

输出:

$ awk '{for(i=1;i<=NF;i++) if($i~/A|C|D/) printf $++i FS; print ""}' file
0.0400213 0.047396 0.082056 
0.0275027 0.023266 0.0406213 
0.0310553 0.0210907 0.05332 
0.03491 0.020474 0.0570167 
0.0485573 0.00938133 0.0519087 

更详细:

$ awk '{for(i=1;i<=NF;i++) if($i~/A|C|D/) { gsub(/[(]/,"",$i) ; printf $i " is " $++i FS }; print ""}' file
A is 0.0400213 C is 0.047396 D is 0.082056 
C is 0.0275027 A is 0.023266 D is 0.0406213 
C is 0.0310553 A is 0.0210907 D is 0.05332 
C is 0.03491 A is 0.020474 D is 0.0570167 
C is 0.0485573 A is 0.00938133 D is 0.0519087 

更新

$ awk '{for(i=1;i<=NF;i++)if($i~/A/){gsub(/[(]/,"",$i);print $++i}}' file
0.0400213
0.023266
0.0210907
0.020474
0.00938133

答案 1 :(得分:3)

GNU代码

$sed -r 's/.*(A\s)([0]\.[0-9]+).*/\2/' file
0.0400213
0.023266
0.0210907
0.020474
0.00938133