Thanos
Thanos

Reputation: 586

awk: Print between regexps which are in different columns

I am trying print the contents of a column in a file between two regular expressions which are in different columns though. A sample file looks like this

Run Summary Paddle Status = 613  (hex 265) Cd+Bi paddle
   Run     To      Time    Current    C mon       Ufis      Charge
                   (sec)    (uA)                             (uC)
  23626   35950   1797.5     99.0   5.641e+05   2.289e+06   1.779e+05
  23630   34587   1729.3    104.9   5.439e+05   2.234e+06   1.815e+05
  23634   34982   1749.1     92.8   5.526e+05   2.100e+06   1.623e+05
  23638   35950   1797.5     95.0   5.698e+05   2.204e+06   1.707e+05
  23642   36135   1806.8     99.6   5.710e+05   2.317e+06   1.800e+05
  23646   36130   1806.5    100.0   5.747e+05   2.322e+06   1.807e+05
  23650   36121   1806.0    100.2   5.733e+05   2.324e+06   1.810e+05
  23654   36123   1806.2    100.2   5.752e+05   2.323e+06   1.809e+05
   Sum  2418821 120941.1            3.772e+07   1.513e+08   1.193e+07

Run Summary Paddle Status = 357  (hex 165) Cd paddle
   Run     To      Time    Current    C mon       Ufis      Charge
                   (sec)    (uA)                             (uC)
  23625   36001   1800.0    100.8   3.342e+05   2.329e+06   1.814e+05
  23629   36132   1806.6    100.8   3.307e+05   2.343e+06   1.821e+05
  23633   35882   1794.1     97.6   3.565e+05   2.257e+06   1.751e+05
  23637   36139   1807.0     99.3   3.545e+05   2.307e+06   1.795e+05
  23641   36133   1806.7     99.3   3.598e+05   2.307e+06   1.794e+05
  23645   36144   1807.2     99.7   3.635e+05   2.315e+06   1.802e+05
  23649   36132   1806.6    100.1   3.648e+05   2.321e+06   1.808e+05
  23653   36123   1806.2    100.1   3.695e+05   2.318e+06   1.808e+05
  23657   36152   1807.6     99.9   3.684e+05   2.318e+06   1.805e+05
   Sum  2404062 120203.1            2.186e+07   1.505e+08   1.193e+07

What I need to do is print the 1st column that occurs between two rows after the regular expression Cd+Bi and the first occurrence of Sum not included. Then the same will be repeated for the regexp Cd. My output will be then two files (although this is not strict) that will have the following contents

file with Cd+Bi

23626   
23630 
23634  
23638  
23642   
23646  
23650  

file with Cd

23625
23629
23633
23637
23641
23645
23649
23653
23657

I tried using the following oneliners, but I can't reproduce what I want.

  1. This doesn't print anything awk '$9 ~ /Cd+Bi/, $1 ~ /Sum/' file
  2. This prints all lines that contain Sum awk '$9 ~ /Cd+Bi/{flag=1;next}$1 ~ /Sum/' file

Any idea on how to produce the desired output?

Upvotes: 0

Views: 53

Answers (2)

anubhava
anubhava

Reputation: 786289

Here is another awk solution:

With Cd+Bi:

awk -v s='Cd+Bi' '$9==s{p=1} $1=="Sum"{p=0} p && $1+0 == $1{print $1}' file

23626
23630
23634
23638
23642
23646
23650
23654

and with Cd:

awk -v s='Cd' '$9==s{p=1} $1=="Sum"{p=0} p && $1+0 == $1{print $1}' file

23625
23629
23633
23637
23641
23645
23649
23653
23657

Upvotes: 1

Freddy
Freddy

Reputation: 4718

$ awk '$9=="Cd+Bi",$1=="Sum"{ if ($1 ~ /^[0-9]+$/) print $1 }' file
23626
23630
23634
23638
23642
23646
23650
23654

Note that the last line contains 23654 which was not included in your example.

And for Cd:

$ awk '$9=="Cd",$1=="Sum"{ if ($1 ~ /^[0-9]+$/) print $1 }' file
23625
23629
23633
23637
23641
23645
23649
23653
23657

Upvotes: 2

Related Questions