Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 >
share|improve this question
add comment

4 Answers

With standard grep syntax:

grep '[>|]'

grep -e '>' -e '|'

grep '>
|'

grep -E '>|\|'
share|improve this answer
add comment

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 >
share|improve this answer
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. –  Stephane 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) –  Stephane Chazelas Aug 20 '13 at 8:45
add comment

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

grep -e '>\||' infile
share|improve this answer
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. –  Stephane Chazelas Aug 19 '13 at 14:02
add comment

Using bracket expression to match either of the wanted characters:

grep "[|>]" infile

Output:

|this is test
where is >
share|improve this answer
3  
No need for either -i or -E here. –  Stephane Chazelas Aug 19 '13 at 13:50
    
That's true, acknowledged. –  zagrimsan Aug 20 '13 at 2:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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