using a wildcard in awk

Viewed 9372

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?

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, ~41, matches the regular expression^22_. If we were to use+in place of~`, we would be instead asking awk to check for an exact match.

Related