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.

So, when you use inverse in grep (egrep -v), this literally means the output is shown upside down (flipped over)?

I think I was more confused when I saw the following examples

$ egrep -v '\ ' blah.sh
$ egrep -v '(\ )|^$' blah.sh

I'm confused about what '\ ' does, and what happens if you put -v. Also what is the difference between '\ ' and '(\ )' command if we are excluding |^$ command?

share|improve this question

2 Answers 2

As vonbrand said, -v means print lines that do not match the pattern. This is best explained by example. Say I have a file called foo.txt whose contents are:

aaa
bbb
aabb

So, lets grep for some strings in that file:

$ egrep 'a' foo.txt ## print lines that contain 'a'
aaa
aabb
$ egrep -v 'a' foo.txt  ## print lines that do not contain 'a'
bbb

Now, the specific regexes you have posted don't really make much sense by themselves. Where did you find them? Anyway, in many regular expression flavors, \ escapes the next character, '\ ' is a way of typing a space in many shells. This is not necessary when using GNU grep, but '\ ' means look for spaces. For example:

$ cat foo.txt
aaa
bb cc
$ grep '\ ' foo.txt
bb cc
$ grep ' ' foo.txt # same as the previous one, at least for GNU grep
bb cc
$ grep -v '\ ' foo.txt # print lines that do not contain spaces
aaa

| means logical OR, ^ matches the start of a string and $ matches the end.. So, \ |^$ will match any line that contains at least one space, or an empty line:

$ cat foo.txt
aaa

bbb
cc dd
$ egrep '\ |^$' foo.txt # print lines that are either empty, or have spaces

dd ee
$ egrep -v '\ |^$' foo.txt # print lines that are neither empty, nor have spaces
aaa 
bbb

The parentheses, allow you to capture a pattern:

   Back References and Subexpressions
       The back-reference \n, where n is a single digit, matches  the
       substring   previously   matched   by  the  nth  parenthesized
       subexpression of the regular expression.

They are not doing anything useful in the regular expression you have posted. An example of their usage would be:

$ cat foo.txt
aaaa
aa bb

aabbccbb
$ egrep '(bb)..\1' foo.txt  
aabbccbb

The regular expression (bb)..\1 means match the string bb, any two characters (..) and then the string bb again (\1).

share|improve this answer

grep pattern file(s) shows the lines that match the pattern; grep -v pattern file(s) shows the lines that do not match the pattern. If you merge the outputs of both in the correct order, you get the file(s) back.

share|improve this answer
    
I think, also need to add -E or egrep . –  Rahul Patil Apr 7 '13 at 3:17
    
@RahulPatil, the difference between grep and egrep (or grep -E) is the exact regular expression language handled, nothing else. –  vonbrand Apr 7 '13 at 3:23
    
I knew it, if he just use as you mention in answer grep pattern file eg. grep -v '^#|^$' file , then it will not work , he have to use -E (or egrep ) option to grep. –  Rahul Patil Apr 7 '13 at 3:30
    
@RahulPatil vonbrand did not give any pattern, he just explained grep's -v flag which has exactly the same effect whether you are using grep -E or not. -E enables extended regular expressions without which the regex you give won't work, however, this works perfectly well grep -v '^#\|^$'. –  terdon Apr 7 '13 at 3:34
    
@RahulPatil, it will work... just differently ;-) –  vonbrand Apr 7 '13 at 3:35

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.