Terminus by Warp
Grep Show Lines Before and After

Grep Show Lines Before and After

Razvan Ludosanu
Razvan Ludosanu
Founder, learnbackend.dev

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:

 $ grep -C <number> <pattern> <file>

For example, to show 3 lines:

$ grep -C 3 "pattern" file.txt

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:

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]

$ grep -B  <number>  <pattern>  <file>

For example, to show 3 lines:

 $ grep -B 3 "pattern" file.txt

[#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:

 $ grep -A <number> <pattern> <file>

For example, to show 3 lines:

 $ grep -A 3 "pattern" file.txt

[#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:

$ grep -B 1 -A 2 "pattern" file.txt

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:

$ head -n $(( $(grep -m <number> -n <pattern> <file> | tail
-n 1 | cut -d ':' -f 1) - 1 )) <file>

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:

$ cat lorem.txt
sed ut perspiciatis
unde omnis iste natus
ut labore et dolore
sed quia consequuntur
qui ratione voluptatem
neque porro quisquam
sed quia non numquam

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.


$ head -n $(( $(grep -m 2 -n "sed" lorem.txt |
  tail -n 1 | cut -d ':' -f 1) - 1 )) lorem.txt

Which will output:


sed ut perspiciatis
unde omnis iste natus
ut labore et dolore

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.

head: illegal line count -- 0

Also note that, if the searched pattern doesn’t exist in the specified file, this command will output the following error message.

head: illegal line count -- -1

[#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:


$ tail -n $(( $(wc -l < < file> | tr -d ' ') - $(grep -m <number> -n <pattern> <file> |
  tail -n 1 | cut -d ':' -f 1)  )) <file>

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:

 
$ cat lorem.txt
sed ut perspiciatis
unde omnis iste natus
ut labore et dolore
sed quia consequuntur
qui ratione voluptatem
neque porro quisquam
sed quia non numquam

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.

  
$ tail -n $(( $(wc -l < lorem.txt | tr -d ' ') - $(grep -m 2 -n "sed" lorem.txt |
  tail -n 1 | cut -d ':' -f 1)  )) lorem.txt

Will output:

  
qui ratione voluptatem
neque porro quisquam
sed quia non numquam

Common pitfalls

Note that, if the searched pattern doesn’t exist in the specified file, this command will output the following error message.

  
-bash: 7 -   : syntax error: operand expected (error token is " ")

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.