14

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 >
10

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 >
  • 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. – Stéphane Chazelas Aug 19 '13 at 13:51
  • @StephaneChazelas: thank you for pointing this out, I was not aware of it. I corrected the text. – Thor Aug 20 '13 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) – Stéphane Chazelas Aug 20 '13 at 8:45
1

Using bracket expression to match either of the wanted characters:

grep "[|>]" infile

Output:

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

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

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

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.