0

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.

user3306922
  • 25
  • 1
  • 7
  • 1
    `grep -E "[<>]" filename` – revo Jun 12 '14 at 09:10
  • @revo What's the difference of this in comparison with `grep -E "<|>" filename`? Does it have a different behaviour? – fedorqui Jun 12 '14 at 10:07
  • @fedorqui There is no difference except an additional character in my regex. – revo Jun 12 '14 at 10:22
  • @revo I mean: is the behaviour of `grep -E "<|>"` the same as `grep -E "[<>]"`? To me it looks like it is, but I feel curious after seeing your comment. – fedorqui Jun 12 '14 at 10:23
  • 1
    @fedorqui Both regexes concepts are the same. One is a character class which checks for each character inside individually, and the other has an OR logic. The difference could be made when if OP stated to need more than one < or > in each line. Then a character class can be more efficient. – revo Jun 12 '14 at 11:07

2 Answers2

4

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 <
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Thank you for a clear response and I will use this solution. I am, however, still interested as to why my command does not work; are there too few escape characters somewhere? – user3306922 Jun 12 '14 at 10:41
  • your command works for me. I don't know why it won't work for your's. I think it may because of version of grep installed or operating system you have been running. – Avinash Raj Jun 12 '14 at 10:46
  • we are running AIX 6.1.0.0, but I do not know how to find the version of grep. – user3306922 Jun 12 '14 at 14:24
0

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
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • I also thought it would work - based on another similar grep command - but somehow it does not as it seem between the shell and the grep command, the characters get interpreted. I have tested this with ksh and bash, neither works. – user3306922 Jun 12 '14 at 10:39
  • It would be interesting to know what OS you are working on, as well as the grep version. – fedorqui Jun 12 '14 at 11:32
  • we are running AIX 6.1.0.0, but I do not know how to find the version of grep. How can I find out the grep version? – user3306922 Jun 12 '14 at 14:25
  • @user3306922 `grep --version` should make it. Otherwise, check `man grep`. Also, are you in bash or ksh or...? Pity I don't have any AIX around to test :) – fedorqui Jun 12 '14 at 14:26
  • I am running ksh, but can run other shells if need to. When I run grep --version, grep tells me that it does not recognise the parameters: > grep --version grep: Not a recognized flag: - Usage: grep [-r] [-R] [-H] [-L] [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] -e pattern_list... [-f pattern_file...] [file...] Usage: grep [-r] [-R] [-H] [-L] [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] [-e pattern_list...] -f pattern_file... [file...] Usage: grep [-r] [-R] [-H] [-L] [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] pattern_list [file...] – user3306922 Jun 12 '14 at 14:34
  • there is no version mentioned anywhere in the man page for grep; not at the top or in the middle of the document. The only thing that might be information about the version is at the bottom of the man page: 'File Overview in AIX 5L Version 5.3' I get the feeling that other UNIX versions are more helpful and informative. :-( – user3306922 Jun 12 '14 at 14:43