Sunday, September 9, 2007

invert string match using grep

Man grep
A regular expression is a pattern that describes a set of strings. Regular expressions are constructed analogously to arithmetic expressions, by using various operators to combine smaller expressions.

Grep understands two different versions of regular expression syntax: “basic” and “extended.” In GNU grep, there is no difference in available functionality using either syntax. In other implementations, basic regular expressions are less powerful. The following description applies to extended regular expressions; differences for basic regular expressions are summarized afterwards.



Here's a simple way to invert matches from string using grep.

You will noticed that this command was being used here for quite sometime now and was never been explained why this parameter had been used from samples covered from this site.

To invert string match, here are several usage how to invert string match or string expression.

To invert a regular string match using grep

# cat testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~
abc
bcd
efg
ab
#abc
#bcd
efg

xyz
~~~~~~~~~~~~~~~~~~~~~~~~~~

# grep -v abc testfile.txt
Results:
~~~~~~~~~~~~~~~~~~~~~~~~~~
bcd
efg
ab
#bcd
efg

xyz
~~~~~~~~~~~~~~~~~~~~~~~~~~

How to grep all string that does not starts with # character? This is handy specially when filtering conf that contains strings that start with # (comment character) and you wish to extract those lines that does not start with # character only

# grep -v '#' testfile.txt
~~~~~~~~~~~~~~~
abc
bcd
efg
ab
efg

xyz
~~~~~~~~~~~~~~~

Another usage for multinested and multiple string searchi using grep command can be done like so

# grep -v ab testfile.txt | grep -v bcd

Alteratively

# grep -v 'ab\|bcd' testfile.txt

Now, how to count the occurences of inverted string matches using grep command would be like so

# grep -cv 'ab\|bcd' testfile.txt

The above command can be useful of counting string matches occurrences from a text file for further string manipulation from bash script. This can be applied on parsing multiple system log files looking for multiple matches of string.

Linux input/output redirection of this grep command can also be applied.

Let us say your current linux box had blocked an IP address from thousand IP address lines from /etc/hosts.deny and you wish to remove it from the list, using linux I/O redirection with inverted string matching using grep can be hand with this situation. Here is an example on how to apply the inverse string matches using grep

# grep -v '123.123.123.123' /etc/hosts.deny > newhosts.deny
# mv newhosts.deny > /etc/hosts.deny

This two commands had removed a single blocked IP address from thousand lines of /etc/hosts.deny without editing it interactively or via string manipulation using sed.

Simple tip using reversed or inverted string matches using grep.

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy