DKangeyan
DKangeyan

Reputation: 543

printing a column after a specific pattern from a text file

I have these lines of data. I want to just print the numbers that follows A, C and D. Is there a way to print those columns?

 (((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 ;

If I want to get the numbers following A
My expected out come will be :

0.0400213,
0.023266,
0.0210907,
0.020474,
0.00938133

Upvotes: 0

Views: 125

Answers (2)

captcha
captcha

Reputation: 3756

Code for GNU :

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

Upvotes: 3

jaypal singh
jaypal singh

Reputation: 77105

This should work:

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

Output:

$ 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 

More verbose:

$ 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 

Update:

$ 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

Upvotes: 3

Related Questions