Skip to content
Advertisement

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. The data values are all formatted the same.

Data:

JavaScript

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:

JavaScript

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:

JavaScript

Output:

JavaScript

To get only the X/N line:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement