2

I want to search Terminal output from a command for a string and display only the lines that include that string.

How can I do it?

I'm interested in searching the Terminal output, not a file. I have tried

ls --help | grep '--l '

but that gave me error.

7
  • Is the output in STDOUT, or already redirected to a file? Or are you asking how to redirect output to a file? – user535733 Nov 23 '19 at 19:35
  • 1
    command | grep 'string' – FedonKadifeli Nov 23 '19 at 20:02
  • @user535733: Output is in STDOUT and I want to stay it that way. I want to "filter" only certain lines. – BlueSkies Nov 23 '19 at 20:10
  • @FedonKadifeli: ls --help | grep '-l ' is giving me error – BlueSkies Nov 23 '19 at 20:27
  • 1
    Possible duplicate of Using GREP command – kingmanas Nov 23 '19 at 20:42
2
command-name | grep 'STRING'

If STRING starts with '-' or '--', it's necessary to utilize one of the following formats:

command-name | grep \\'STRING'
command-name | grep -e 'STRING'
command-name | grep -- 'STRING'
4
  • 2
    ... or use -e 'STRING' to make it explicit that STRING is a pattern not an option; or use -- to signal end of options – steeldriver Nov 23 '19 at 22:28
  • 1
    Being more explicit about the last suggestion: grep -- --l – glenn jackman Nov 23 '19 at 22:34
  • 1
    @BlueSkies you can do it, click edit at share edit flag below your answer. – Hannu Nov 23 '19 at 23:10
  • 2
    They provided the comments so that you could edit and create a complete answer. – Hannu Nov 23 '19 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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