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
b
c
d
e" | awk '/[^aeiou]/{ print }'

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

b
c
d

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:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Some examples of complemented character classes are given as follows:

Named character class

Matches

[^a-z]

Matches any character except a lowercase letter

[^A-Z]

Matches any character except an uppercase letter

[^a-zA-Z]

Matches any character except an alphabet character

[^a-zA-Z0-9]

Matches any character except an alphanumeric character

[^5-9G-Lr-z]

Matches any single character except among [56789GHIJKLrstuvwxyz]

[^0-9]

Matches any character except any digit

^[^ABC]

Matches any character at the beginning of a string except ABC

^[ABC]

Matches ABC at the beginning of a string

^[^a-z]$

Matches any single-character except a lowercase letter

^[^^]

Matches any character except ^ at the beginning

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.161.225