0

For example, I run a command like:

grep -r "MACOSX_PACKAGE_LOCATION" . --include "CMakeLists.txt"

and get:

/qtdoc/examples/demos/photosurface/CMakeLists.txt:           MACOSX_PACKAGE_LOCATION "Resources")
./qtwebengine/src/core/api/CMakeLists.txt:        PROPERTIES MACOSX_PACKAGE_LOCATION Resources/qtwebengine_locales
./qtwebengine/src/core/api/CMakeLists.txt:        PROPERTIES MACOSX_PACKAGE_LOCATION Resources

How to improve this command and make it display not only the line of code containing MACOSX_PACKAGE_LOCATION but a code snipped containing 5 lines before and 5 lines after MACOSX_PACKAGE_LOCATION?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Alexey Starinsky
  • 3,699
  • 3
  • 21
  • 57
  • Which version of `grep` do you use? – Cyrus Oct 22 '22 at 21:58
  • 4
    Have you looked at `man grep`? You should see `-B` and `-A`, to include content before and after a match, if you have a GNU version. – Charles Duffy Oct 22 '22 at 22:00
  • 1
    I've removed the bash tag, as this isn't a bash question: bash has no control over what options your copy of grep has, and it'll be the same options no matter if you're running grep from bash, ash, zsh, Python with `shell=False`, an `exec`-family call from C, etc; it's your OS vendor, in deciding which version of `grep` to include, that makes the relevant decision. – Charles Duffy Oct 22 '22 at 22:00
  • Anyhow -- I'd argue (1) that this question would be more on-topic at [unix.se], as it's not specific to software development; and (2) that it isn't answerable at all, either here or there, unless you specify your OS or otherwise which version of grep you have. – Charles Duffy Oct 22 '22 at 22:05
  • @CharlesDuffy grep version: grep (BSD grep) 2.5.1-FreeBSD – Alexey Starinsky Oct 22 '22 at 22:09
  • 4
    Although not within the current scope of SO, this was [asked and answered in 2008](https://stackoverflow.com/questions/9081/grep-show-lines-surrounding-each-match) as automatically shown in the Related section (at least for me) – dave_thompson_085 Oct 22 '22 at 22:17

0 Answers0