The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

SteveS (10) [Avatar] Offline
#1
I think there's some confusion on the use of regular expressions in the grep and awk examples in this section.

First of all, the regex passed to awk is just as incorrect as the one passed to grep in the sense that it's not matching the literal string [Warning], but rather matching any of the characters W, a, r, n, i, or g. Presumably, what makes it seem to the author that awk is working correctly is that, instead of matching against the entire line, as grep does, awk is only matching against the third token of each line, which produces fewer erroneous matches, but they are erroneous, nonetheless.

For example, consider the following input file named error.log:

first second [Warning] fourth fifth
first second [Error] fourth fifth
first second [Note] fourth fifth
first second [Severe] fourth fifth
first second [Critical] fourth fifth
first second [Warning] fourth fifth


Using the example awk command given in the text matches five lines when only two matches are expected:


$ cat error.log | awk '$3 ~/[Warning]/' | wc -l
5


However, escaping the character class braces in the regex yields the correct number of matches:


$ cat error.log | awk '$3 ~/\[Warning\]/' | wc -l
2


Second, the text seems to allude that grep is helpless here because it interprets the bracketed string as a regex character class rather than a literal string. Indeed, it does. However, just as with awk, simply escaping the brackets works just as well:


$ cat error.log | grep [Warning] | wc -l
6
$ cat error.log | grep '\[Warning\]' | wc -l
2
David Clinton (92) [Avatar] Offline
#2
You are absolutely correct. I'm going to have to rework that section.
Thanks,