I have a text file with multiple datasets. I have written a code that searches for certain values (top left 4-letter code) in a text file but, now I need to be able to have the script copy out data from a set number of lines AFTER the match (“PHHI, etc”) is found. The data values are all formatted the same.
Data:
PHHI GFS MOS GUIDANCE 7/13/2015 0000 UTC DT /JULY 13 /JULY 14 /JULY 15 / HR 06 09 12 15 18 21 00 03 06 09 12 15 18 21 00 03 06 09 12 18 00 X/N 83 71 84 70 84 TMP 77 76 76 76 76 79 82 81 76 76 74 73 76 80 82 81 76 75 73 76 83 DPT 72 72 72 72 72 72 70 68 70 70 69 69 69 70 70 69 69 69 69 69 69 CLD BK OV BK OV OV BK FW FW CL FW BK FW SC SC FW FW FW CL FW SC FW WDR 10 09 06 04 07 09 07 08 07 06 03 03 07 08 07 08 07 07 04 07 08 WSP 04 03 03 03 06 11 16 11 06 08 05 03 04 09 12 09 05 03 03 03 10 P06 5 27 16 0 4 2 4 15 5 6 1 P12 39 21 8 19 7 Q06 0 1 0 0 0 0 0 0 0 0 0 Q12 1 1 0 1 0 CIG 5 4 4 4 4 4 5 8 6 6 6 7 6 6 7 8 7 8 8 8 8 VIS 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 OBV N N N N N N N N N N N N N N N N N N N N N
I am interested in pulling the data values located on the “X/N” line.
Advertisement
Answer
To get a specified number of line of data after the match:
grep pattern -A num file
where,
pattern
is the pattern to be matched
num
is the number of line of data to be taken after the match
For example, to get 3 lines after the PHHI
:
grep "^PHHI" -A 3 file
Output:
PHHI GFS MOS GUIDANCE 7/13/2015 0000 UTC DT /JULY 13 /JULY 14 /JULY 15 / HR 06 09 12 15 18 21 00 03 06 09 12 15 18 21 00 03 06 09 12 18 00 X/N 83 71 84 70 84
To get only the X/N
line:
grep "^X/N.*" file
Output:
X/N 83 71 84 70 84