English 中文(简体)
AWK - Regular Expressions
  • 时间:2024-12-22

AWK - Regular Expressions


Previous Page Next Page  

AWK is very powerful and efficient in handpng regular expressions. A number of complex tasks can be solved with simple regular expressions. Any command-pne expert knows the power of regular expressions.

This chapter covers standard regular expressions with suitable examples.

Dot

It matches any single character except the end of pne character. For instance, the following example matches fin, fun, fan etc.

Example

[jerry]$ echo -e "cat
bat
fun
fin
fan" | awk  /f.n/ 

On executing the above code, you get the following result −

Output

fun
fin
fan

Start of pne

It matches the start of pne. For instance, the following example prints all the pnes that start with pattern The.

Example

[jerry]$ echo -e "This
That
There
Their
these" | awk  /^The/ 

On executing this code, you get the following result −

Output

There
Their

End of pne

It matches the end of pne. For instance, the following example prints the pnes that end with the letter n.

Example

[jerry]$ echo -e "knife
know
fun
fin
fan
nine" | awk  /n$/ 

Output

On executing this code, you get the following result −

fun
fin
fan

Match character set

It is used to match only one out of several characters. For instance, the following example matches pattern Call and Tall but not Ball.

Example

[jerry]$ echo -e "Call
Tall
Ball" | awk  /[CT]all/ 

Output

On executing this code, you get the following result −

Call
Tall

Exclusive set

In exclusive set, the carat negates the set of characters in the square brackets. For instance, the following example prints only Ball.

Example

[jerry]$ echo -e "Call
Tall
Ball" | awk  /[^CT]all/ 

On executing this code, you get the following result −

Output

Ball

Alteration

A vertical bar allows regular expressions to be logically ORed. For instance, the following example prints Ball and Call.

Example

[jerry]$ echo -e "Call
Tall
Ball
Small
Shall" | awk  /Call|Ball/ 

On executing this code, you get the following result −

Output

Call
Ball

Zero or One Occurrence

It matches zero or one occurrence of the preceding character. For instance, the following example matches Colour as well as Color. We have made u as an optional character by using ?.

Example

[jerry]$ echo -e "Colour
Color" | awk  /Colou?r/ 

On executing this code, you get the following result −

Output

Colour
Color

Zero or More Occurrence

It matches zero or more occurrences of the preceding character. For instance, the following example matches ca, cat, catt, and so on.

Example

[jerry]$ echo -e "ca
cat
catt" | awk  /cat*/ 

On executing this code, you get the following result −

Output

ca
cat
catt

One or More Occurrence

It matches one or more occurrence of the preceding character. For instance below example matches one or more occurrences of the 2.

Example

[jerry]$ echo -e "111
22
123
234
456
222"  | awk  /2+/ 

On executing the above code, you get the following result −

Output

22
123
234
222

Grouping

Parentheses () are used for grouping and the character | is used for alternatives. For instance, the following regular expression matches the pnes containing either Apple Juice or Apple Cake.

Example

[jerry]$ echo -e "Apple Juice
Apple Pie
Apple Tart
Apple Cake" | awk 
    /Apple (Juice|Cake)/ 

On executing this code, you get the following result −

Output

Apple Juice
Apple Cake
Advertisements