This question already has an answer here:

I have a big sql dump with long lines and I would like to use grep to find a certain text string.

grep text_string filename.sql

returns the result lines which are huge and difficult to see where my text_string appears. What I want to achieve is to get a result that contains lets say 50 characters to the left and 50 to the right of the search term (text_string). Is this possible?

marked as duplicate by meuh, Anthon, Jeff Schaller, jimmij, roaima Apr 19 '16 at 11:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Use grep with -o to get the desired portion only:

grep -Eo '.{,50}text_string.{,50}' filename.sql
  • 1
    That only works on lines that DO have 50+ characters on both sides of text_string. Should be grep -Eo '.{0,50}text_string.{0,50}' filename.sql instead. – cas Apr 19 '16 at 11:18
  • @cas oopss..missed that..fixed.. – heemayl Apr 19 '16 at 11:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.