How to select several lines before search line in Linux?

Sometimes you need to look for something (i.e. grep) in file, but you want to see several lines BEFORE and AFTER the line you’re looking for. So, how can you do this in Linux?

Try:

grep -B 3 -A 3 "Deploying" catalina.out

It looks for text “Deploying” in file catalina.out and returns 3 lines before (-B switch) and after (-A switch) the line with text “Deploying”.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *