0

I wrote a line like this diff fileA.txt fileB.txt | grep -v "^[11,12d9]" . Without the grep part, the output would look like this:

1d0  
< loop  
11,12d9  
< polo  
< pool  

But with grep, the output would look like this:

< loop  
< polo  
< pool  

I think that grep -v is kind of like reverse search. But I don't understand the ^[11,12d9] part, what does it do?

2
  • 2
    What inspired you to write that particular line? What did you actually want to achieve?
    – Kusalananda
    Feb 6 at 11:26
  • -v is not reverse search (it does not read the file/stream backwards). It is a inverted filter (find those that don't match). Feb 6 at 11:44
5

grep -v "^[11,12d9]"

yes, as you found out -v is used for invert match and will return lines that do not match the given pattern ^[11,12d9].

and what ^[11,12d9] is doing is that saying match characters in [...] and if they were appeared at the begging of a line (^ is the beginning of the line anchor) then ignore those lines (since of used -v invert match);

so basically lines that started with one of 1, ,, 2, d or9 characters will be ignored (repeated characters also will be ignored within character-class).

but if you would like to ignore the line starting with string 11,12d9, you need grep -v '^11,12d9'.


*Not relevant to the question but to keep notes about use of ], ^ and - characters within character class:

if ^ character: it can be placed anywhere in [] but not the first character; if it was first character, it acts as negation on the characters within that class.
if ] character: it should be the first character.
if - character: it should be the last character.

0

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.