Text Processing – Print Values After Pattern Match with Awk

awktext processing

I have a data file named File-1. I have to match a pattern DATA_POINTS, and then after skipping one line I want to print 6th column of the following lines.

  • File-1 example:
    here ! some other data exist but all of them are totally different from the below data!
    
    In simple words following data is completely unique.
    
    
    DATA_POINTS
    12
       0.0000000000     0.0000000000     0.0000000000  20   !  A
       0.5000000000     0.5000000000     0.0000000000  20   !  B
       0.7500000000     0.5000000000     0.2500000000  20   !  C
       0.7500000000     0.3750000000     0.3750000000  20   !  D
       0.0000000000     0.0000000000     0.0000000000  20   !  E
       0.5000000000     0.5000000000     0.5000000000  20   !  F
       0.6250000000     0.6250000000     0.2500000000  20   !  U
       0.7500000000     0.5000000000     0.2500000000  20   !  W
       0.5000000000     0.5000000000     0.5000000000  20   !  L
       0.7500000000     0.3750000000     0.3750000000  20   !  K
       0.6250000000     0.6250000000     0.2500000000  20   !  U
       0.5000000000     0.5000000000     0.0000000000  20   !  X
    
  • Desired output
    S1 = A
    S2 = B
    S3 = C
    S4 = D
    S5 = E
    S6 = F
    S7 = U
    S8 = W
    S9 = L
    S10= K
    S11= U
    S12= X
    

The pattern DATA_POINTS does not repeat in the file, and an exact match is desired.

Nearest Solution

I got this command from another qsn. this is working if the 6th column in a same row of pattern

awk '/DATA_POINTS/{i==0 ; i++; getline; print "S"i"=", $6}' File-1

Best Answer

$ awk '/DATA_POINTS/{c=3} c&&!--c{f=1} f{printf "S%d = %s\n", ++s, $6}' file
S1 = A
S2 = B
S3 = C
S4 = D
S5 = E
S6 = F
S7 = U
S8 = W
S9 = L
S10 = K
S11 = U
S12 = X

To start printing from the 27th line from the matching line (inclusive) instead of the 3rd, just change 3 to 27.

See https://stackoverflow.com/questions/17908555/printing-with-sed-or-awk-a-line-following-a-matching-pattern/17914105#17914105 for more information on the above approach and more ways to do something after a match.

Related Question