How to grep for "<|>" ? (either the larger-than or small-than sign)

3.2k views Asked by At

How can I grep for lines that contains either the larger-than sign or the smaller-than sign in them?

The following grep command returns nothing:

grep "<\|>" sdiff.out

Grepping, however, for just one of the signs at a time does return data from the file. The file contains output from the sdiff command.

The purpose of all this is to see only the actual lines that differ between two files.

Thank you for any tips.

2

There are 2 answers

3
Avinash Raj On BEST ANSWER

Use -P or -E option in grep with >< symbols inside character class,

grep -P '[><]' file

OR

grep -E '[><]' file

Example:

$ cat file
chr -   xyz ordered_A01 5480    6144>
chr -   xyz ordered_A01 5480    58001
rm -rf ~/Desktop/-r <

$ grep -P '[><]' file
chr -   xyz ordered_A01 5480    6144>
rm -rf ~/Desktop/-r <

$ grep -E '[><]' file
chr -   xyz ordered_A01 5480    6144>
rm -rf ~/Desktop/-r <
6
fedorqui On

The expression grep "<\|>" sdiff.out should work. Anyway, you can get the same behaviour using -E to define different patterns separated by |:

grep -E '<|>' file

Test

$ cat a
hello < i am
here > and this
is a test
blabla <> iea

$ grep -E '<|>' a
hello < i am
here > and this
blabla <> iea