O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Complemented character classes

In a complementary character set, the first character after [ is caret, ^, and it negates the set of characters in the square brackets. For example, to match any character except a vowel, we can use the following regular expression:

$ echo -e "a\nb\nc\nd\ne" | awk '/[^aeiou]/{ print }'

The output on execution of the preceding code is as follows:

bcd

Similarly, in the employee database (emp.dat), if we want to print information for all those employees whose email ID doesn't begin with either 'j' or 'v', that is, [^jv], we can use the following expression:

$ awk '$4 ~ /^[^jv]/{ print $4 }' emp.dat

The output on execution of the given code is as follows:

eva@gmail.comamit@yahoo.comanak@hotmail.comhari@yahoo.com ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required