Monday, November 1, 2021

[SOLVED] grep match only lines in a specified range

Issue

Is it possible to use grep to match only lines with numbers in a pre-specified range? For instance I want to list all lines with numbers in the range [1024, 2048] of a log that contain the word 'error'.

I would like to keep the '-n' functionality i.e. have the number of the matched line in the file.


Solution

sed -n '1024,2048{/error/{=;p}}' | paste - -

Here /error/ is a pattern to match and = prints the line number.



Answered By - Prince John Wesley