Uncategorized

Grep the line and the line before or after.

Here is a sweet trick I learned to day to grab the line you’re looking for as well as the line after or before. It’s using the -A or -B flag within grep. Glad I found this before writing a stupid script using awk, etc.


grep 'some value in a line' -B 1 /var/log/messages

This will grab the line and one line before. Use -A NUM for after the target.

Here is a sweet trick I learned to day to grab the line you’re looking for as well as the line after or before. It’s using the -A or -B flag within grep. Glad I found this before writing a stupid script using awk, etc.


grep 'some value in a line' -B 1 /var/log/messages

This will grab the line and one line before. Use -A NUM for after the target.