[英]Linux: Extract a specified number of lines from text file based on match
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.
我已经编写了一个代码,用于在文本文件中搜索某些值(左上角的4个字母的代码),但是现在,我需要能够让脚本在匹配后从设定的行数中复制数据(“ PHHI,等”)。 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. 我有兴趣提取位于“ X / N”行上的数据值。
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 pattern
是要匹配的模式
num
is the number of line of data to be taken after the match num
是匹配后要采取的数据行数
For example, to get 3 lines after the PHHI
: 例如,要在
PHHI
之后获得3行:
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: 仅获得
X/N
行:
grep "^X/N.*" file
Output: 输出:
X/N 83 71 84 70 84
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.