How to have grep print only the matching text plus X surrounding characters
If you know Y up front, then you can do e.g.
grep -o '...mytext...' file
where the ...
is Y characters long. E.g. the above does for Y=3. The '.' character in a regular expression matches any character.
If you want to find, let's say, from 0 to 10 chars before or after your intended search string, mytext
, do this:
grep -rnioE '.{0,10}mytext.{0,10}'
Grep Options Explanation:
- The
-r
says to recursively search down the file and folder tree -n
says to show line numbers-i
says to be case insensitive-o
says to only show the matching parts, not the whole line-E
says to use Extended regular expressions
Regex Explanation:
See: https://regex101.com/r/BUpUdp/2.
.
matches any character except newlines{0,10}
matches 0 to 10 instances of whatever is before it, which is.
, or any character except newlines
Example usage:
I'd like to find any instances of this web page color code (#005cc5
) to figure out if it's being used. But, the CSS is literally 5000 lines all on a single line with no line breaks, so I need to only capture a few of the surrounding chars for context--let's say up to 20 before and after. So, I search for grep -rnioE '.{0,20}#005cc5.{0,20}'
:
$ grep -rnioE '.{0,20}#005cc5.{0,20}'
Test Syntax Highlighting _ GabrielStaples.com home_files/main.css:5:.highlight .l{color:#005cc5}.highlight .n{color
Test Syntax Highlighting _ GabrielStaples.com home_files/main.css:5:.highlight .m{color:#005cc5}.highlight .s{color
Test Syntax Highlighting _ GabrielStaples.com home_files/main.css:5:highlight .mf{color:#005cc5}.highlight .mh{colo
Test Syntax Highlighting _ GabrielStaples.com home_files/main.css:5:r:#005cc5}.highlight .mi{colo
Test Syntax Highlighting _ GabrielStaples.com home_files/main.css:5:r:#005cc5}.highlight .mo{colo
Test Syntax Highlighting _ GabrielStaples.com home_files/main.css:5:r:#005cc5}.highlight .sb{colo
Test Syntax Highlighting _ GabrielStaples.com home_files/main.css:5:highlight .se{color:#005cc5}.highlight .sh{colo
Test Syntax Highlighting _ GabrielStaples.com home_files/main.css:5:highlight .si{color:#005cc5}.highlight .sx{colo
Test Syntax Highlighting _ GabrielStaples.com home_files/main.css:5:highlight .il{color:#005cc5}.gist th,.gist td{b
Test Syntax Highlighting _ GabrielStaples.com home_files/main.css:5:highlight .nb{color:#005cc5}.highlight .nc{colo
Screenshot with coloring:
The second match above shows this color applies to .m
CSS classes, for instance, so I can now search the code for any matches using this "m"
class, which may show up in some *.html
files. (This next search seems to not find everything I want, but you get the idea! The search above works fine.)
grep -rniE 'class="m[\s"]?'