Wildcard in AWK

Wildcard in AWK

In my previous article “Grep Based on Column” I discussed the usage of AWK in order to grep a string from a column, instead of the entire output.

The following is the syntax that we use to find a string in AWK:

awk ‘$NUM == “STRING”‘

However, the limitation of above syntax is that it tries to match the exact regex and does not allow the use of * (wildcard).

So let’s take an example of the following output:

[[email protected] ~]# ls -lah
.
.
-rw-------  1 devs       devs       442M Feb 17  2012 project_inprogress.pdf
-rw-------  1 devs-stage devs-stage 4.3K Mar  4  2012 jmeter.log
-rw-------  1 gagan      gagan      5.4K Jul 19  2012 June-2012.pdf
-rw-------  1 root       root       5.4K Jul 19  2012 June-2012-gagan.pdf
-rw-------  1 gagan      gagan      5.4K Jul 19  2012 May-2012.pdf
.
.

So in the above output, I need to find the files which are owned by the users devs*. So if use the above AWK syntax I will not be able to get those files.

Wildcard in AWK

So in this case we will not use the ==, instead we will use the match pattern sign ~ which will act as wildcard in AWK. So the same syntax above will be changed to something like below:

awk ‘$NUM ~ “STRING”‘

To find the files which are user owned by devs* we will use the following command:

[[email protected] ~]# ls -lah | awk ‘$3 ~ “devs”‘

Output:

-rw-------  1 devs       devs       442M Feb 17  2012 project_inprogress.pdf
-rw-------  1 devs-stage devs-stage 4.3K Mar  4  2012 jmeter.log

Wildcard in AWK

No Comments

Post a Comment

Time limit is exhausted. Please reload CAPTCHA.