Removing Dashes in Grep -A

Removing Dashes in Grep -A

I am using grep -A command to get a specific pattern. It gives -- at the end of every block, how I can remove these dashes?

1

2 Answers

AFAIK it's not mentioned in the man page, but in the Context Line Control section of info grep you will find the option

‘--no-group-separator’
     When ‘-A’, ‘-B’ or ‘-C’ are in use, do not print a separator
     between groups of lines.

You have at least three alternatives:

  1. With -v option (works on any version of grep):

    ... | grep -A1 "pattern" | grep -v -- "^--$"
    

    Also, | sed '/^--$/d'.

  2. With undocumented --group-separator

    ... | grep -A1 "pattern" --group-separator "" 
    
  3. With --no-group-separator (as mentioned steeldriver).

    ... | grep -A1 "pattern" --no-group-separator
    

Your Answer

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

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.