Grep Show Lines Before and After
The short answer
To show N lines before and after the [.inline-code]grep[.inline-code] results, including the lines containing the matched search pattern, you can use the [.inline-code]-C[.inline-code] flag (short for context) as follows:
For example, to show 3 lines:
Read our article on how to [.inline-code]grep[.inline-code] across multiple lines if you are trying to provide a multi-line [.inline-code]grep[.inline-code] input.
[#easily-recall-with-ai] Easily retrieve this command using Warp’s AI Command Search [#easily-recall-with-ai]
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:
![](https://cdn.prod.website-files.com/64b7506ad75bbfcf43a51e90/64c96f2942bf257afa06823b_64babf02c126aa7810fa34da_IEaiS9J2hs-HBngD_0PRFuzH0s7uDQCMQ2HfvrxW2H1iiv4YFE-B9YenYzKYwYUVT5BvofcLLNxfdwiGfGRaMN9IAJQZVRSM_61YpfCT57jkVNxSowx8JF690hL0o8hrazoCGAiMjuKFVZSxb1ztIKU.gif)
Entering [.inline-code]grep show context lines[.inline-code] in the AI Command Search will prompt a [.inline-code]grep[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].
[#show-n-lines-before]Showing N lines before the [.inline-code]grep[.inline-code] result [#show-n-lines-before]
For example, to show 3 lines:
[#show-n-lines-after]Showing N lines after the[.inline-code]grep[.inline-code] result[#show-n-lines-after]
To show N lines after the [.inline-code]grep[.inline-code] results, including the lines containing the matched search pattern, you can use the [.inline-code]-A[.inline-code] flag (short for after) as follows:
For example, to show 3 lines:
[#show-all-lines-before]Selectively showing N lines before and after the [.inline-code]grep[.inline-code] result [#show-all-lines-before]
To selectively show N lines before and M lines after the [.inline-code]grep[.inline-code] results, including the matched search pattern, you can use a combination of the aforementioned [.inline-code]-B[.inline-code] and [.inline-code]-A[.inline-code] flags.
For example, to show 1 line before and 2 lines after:
Showing all lines before the [.inline-code]grep[.inline-code] result
To show all lines before the [.inline-code]grep[.inline-code] results, not including the search pattern, you can use a combination of the following commands:
Where:
- [.inline-code]head -n[.inline-code] displays the first N lines of a file.
- [.inline-code]grep -m <number> -n <pattern> <file>[.inline-code] searches for the [.inline-code]pattern[.inline-code] in the [.inline-code]file[.inline-code] and returns the line number and corresponding lines where the [.inline-code]pattern[.inline-code] is found. The [.inline-code]-m[.inline-code] option flag tells [.inline-code]grep[.inline-code] to stop after finding [.inline-code]number[.inline-code] matches—leave it out if you want to find all matches.
- [.inline-code]tail -n 1[.inline-code] displays the last line of the matches returned by [.inline-code]grep[.inline-code]
- [.inline-code]cut -d ':' -f 1[.inline-code] returns the line number of the pattern matched by [.inline-code]grep[.inline-code].
- [.inline-code]$( … )[.inline-code] is used to substitute a command by the result of its execution.
- [.inline-code]$(( … ))[.inline-code] is an expansion used to perform arithmetic calculations.
For example, considering the following file:
The following command will attempt to display all the lines of the [.inline-code]lorem.txt[.inline-code] file before the one containing the second matched [.inline-code]sed[.inline-code] pattern.
Which will output:
Common pitfalls
Note that when using the [.inline-code]-m 1[.inline-code] flag, if the pattern matched by [.inline-code]grep[.inline-code] is present in the first line of the file, this command will output the following error indicating that the [.inline-code]head[.inline-code] command cannot print the first 0 lines of the specified file.
Also note that, if the searched pattern doesn’t exist in the specified file, this command will output the following error message.
[#show-all-lines-after]Showing all lines after the [.inline-code]grep[.inline-code] result[#show-all-lines-after]
To show all lines after the [.inline-code]grep[.inline-code] result, not including the search pattern, you can use a combination of the following commands:
Where:
- [.inline-code]tail -n[.inline-code] displays the last N lines of a file.
- [.inline-code]wc -l[.inline-code] returns the number of lines of the specified file.
- [.inline-code]tr -d ' '[.inline-code] removes space characters from the string returned by [.inline-code]wc[.inline-code].
- [.inline-code]cut -d ':' -f 1[.inline-code] returns the line number of the pattern matched by [.inline-code]grep[.inline-code].
- [.inline-code]$( … )[.inline-code] is used to substitute a command by the result of its execution.
- [.inline-code]$(( … ))[.inline-code] is an expansion used to perform arithmetic calculations.
For example, considering the following file:
The following command will attempt to display all the lines of the [.inline-code]lorem.txt[.inline-code] file after the one containing the second matched [.inline-code]sed[.inline-code] pattern.
Will output:
Common pitfalls
Note that, if the searched pattern doesn’t exist in the specified file, this command will output the following error message.