I want a way to search in a given text. For that, I use grep
:
grep -i "my_regex"
That works. But given the data like this:
This is the test data
This is the error data as follows
. . .
. . . .
. . . . . .
. . . . . . . . .
Error data ends
Once I found the word error
(using grep -i error data
), I wish to find the 10 lines that follow the word error
. So my output should be:
. . .
. . . .
. . . . . .
. . . . . . . . .
Error data ends
Are there any way to do it?
You can use grep with -A n option to print N lines after matching lines. Using -B n option you can print N lines before matching lines. Using -C n option you can print N lines before and after matching lines.
To also show you the lines before your matches, you can add -B to your grep. The -B 4 tells grep to also show the 4 lines before the match. Alternatively, to show the log lines that match after the keyword, use the -A parameter. In this example, it will tell grep to also show the 2 lines after the match.
Using the grep Command. If we use the option '-A1', grep will output the matched line and the line after it.
grep's -A 1 option will give you one line after; -B 1 will give you one line before; and -C 1 combines both to give you one line both before and after, -1 does the same.
You can use the -B
and -A
to print lines before and after the match.
grep -i -B 10 'error' data
Will print the 10 lines before the match, including the matching line itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With