16

How can I grep for line containing pipe character | or for character >:

files content:
|this is test
where is >
this is none

now what I need using grep command is

grep -iE "<some expression>" file_name

Output:

|this is test
where is >

4 Answers 4

13

With standard grep syntax:

grep '[>|]'

or

grep -e '>' -e '|'

or

grep '>
|'

or

grep -E '>|\|'
6

If you are using GNU grep you can do this with the or operator (|), which should be escaped (preceded by a backslash \). So to find lines containing either pipe or a greater-than sign, included them literally with the or operator:

grep '|\|>' infile

Output:

|this is test
where is >
3
  • 4
    \| is not a standard BRE operator, though it works with the GNU grep which is the grep found in most Operating systems built around a Linux kernel. Aug 19, 2013 at 13:51
  • @StephaneChazelas: thank you for pointing this out, I was not aware of it. I corrected the text.
    – Thor
    Aug 20, 2013 at 8:39
  • 1
    Yes, the alternation operator is the only real addition of EREs over BREs, the rest (+, ?) being syntactic sugar for \{1,\} and \{0,1\}. (on the other hand, EREs loose back references (\(.\)\1) which are a BRE only feature) Aug 20, 2013 at 8:45
2

Using bracket expression to match either of the wanted characters:

grep "[|>]" infile

Output:

|this is test
where is >
2
  • 4
    No need for either -i or -E here. Aug 19, 2013 at 13:50
  • That's true, acknowledged.
    – zagrimsan
    Aug 20, 2013 at 2:47
0

The correct way to accomplish it is using -e flag which is specified by POSIX. E.g:

grep -e '>\||' infile
2
  • 1
    grep -e '>' -e '|' infile works too.
    – ott--
    Aug 19, 2013 at 11:28
  • 2
    -e is specified by POSIX but is not useful here. \| is not specified by POSIX. Aug 19, 2013 at 14:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .