Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a wildcard in awk

Tags:

awk

wildcard

Using awk, I want to print all lines that have a string in the first column that starts with 22_

I tried the following, but obviously * does not work as a wildcard in awk:

awk '$1=="22_*" {print $0}' input > output

Is this possible in awk?

like image 752
Abdel Avatar asked Sep 12 '25 15:09

Abdel


1 Answers

Let's start with a test file:

$ cat >file
22_something keep
23_other omit

To keep only lines that start with 22_:

$ awk '/^22_/' file
22_something keep

Alternatively, if you prefer to reference the first field explicitly, we could use:

$ awk '$1 ~ /^22_/' file
22_something keep

Note that we don't have to write {print $0} after the condition because that is exactly the default action that awk associates with a condition.

At the start of a regular expressions, ^ matches the beginning of a line. Thus, if you want 22_ to occur at the start of a line or the start of a field, you want to write ^22_.

In the condition $1 ~ /^22_/, note that the operator is ~. That operator tells awk to check if the preceding string, $1, matches the regular expression ^22_.

like image 162
John1024 Avatar answered Sep 15 '25 13:09

John1024