Awk Regular Expression Commands and Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/awk-regular-expression-commands-examples/
PINTEREST
PINTEREST
INSTAGRAM

Awk is very popular and useful tool for text and string manipulation. Regex or regular expression is generic language used to define text and string structures. Awk can

Syntax

Regular expression for awk can be used with the following syntax.

  • REGULAR_EXPRESSION is the regular expression we want to run . The regular expression is surrounded with / and /

Example Data

We will use follow file named data.txt for example scripts.

Any Single Character

The first and simplest example with regular expression is expressing single character in regular expression. In this we will filter words starts with is and ends with ail but contain single character between then where character is not important.

Any Single Character

Any Single Character

Start Of Line

We may need to specify the start of the line. We will use ^ sig to specify start of the line. In this example we will look the lines starts with a .

Start Of Line

Start Of Line

End Of Line

Like previous example we can also specify the end of the line with the $ sign. In this example we will look lines those ends with le .

End Of Line

End Of Line

Match Character Set

We can specify multiple characters to hope match one of them. We will put those characters into [...] . In this example we will match lines those have sm or hm .

LEARN MORE  Vim Copy, Cut and Paste Commands and Operations

Match Character Set

Match Character Set

Exclusive Set

Exclusive set is the reverse operation of character set. The provided set matched lines will be removed from output. We will express exclusive set as [^...] . In this example we want to list lines those do not have sm or hm .

OR or Multiple Words

We can specify multiple words for a single search operation. This will match all lines those have one of the provided words. In this example we will match lines ahmet or ismail

OR or Multiple Words

OR or Multiple Words

Match Zero or One Occurrence

We can specify to expect given character zero or one occurrence. We will use ? sign. In the example we will look i character zero or one occurrence.

Match Zero or One Occurrence

Match Zero or One Occurrence

Match Zero or More Occurrence

We can specify zero or more multiple occurrence with * sign. In this example we will look for word male , malee , maleee , …

Match Zero or More Occurrence

Match Zero or More Occurrence

Match One or More Occurrence

We can also expect single or more occurrence for given character or word. We will use + for this. In this example we will look male , mmale , mmmale .

Match One or More Occurrence

Match One or More Occurrence

Match Groups

Up to now we have generally worked with single characters but in real world we may need multiple characters or words to match. We will use (...) . In this example we will look one or more occurrence of male .

LEARN MORE  Awk If, If Else , Else Statement or Conditional Statements

Match Groups

Match Groups

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/awk-regular-expression-commands-examples/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)