Tuesday, October 31, 2017

A greedy grep


While using the grep for pattern matching keep in mind that grep is greedy on matching.
grep gives the results when the part of the string matches.

Your pattern string should be  made to match the lines uniquely.
In the below example instead of hcptest it can be matched with ',hcptest1,'.

A good QA will includes the test cases of below kind when grep command is seen.

$ cat /tmp/test.txt
02:03:01:1a:00:73,set:10_31_0_31,10.31.0.31,dhcptest1,infinite
02:03:01:1a:00:74,set:10_31_0_14,10.31.0.14,dhcptest2,infinite
02:03:01:1a:00:74,set:10_31_0_14,10.31.0.14,hcptest1,infinite

$cat /tmp/test.txt | grep -i 'hcptest1' --color
02:03:01:1a:00:73,set:10_31_0_31,10.31.0.31,dhcptest1,infinite
02:03:01:1a:00:74,set:10_31_0_14,10.31.0.14,hcptest1,infinite

No comments:

Post a Comment