Nico Carabelli
Nico Carabelli

Reputation: 1

Using a wildcard in awk with references to file

Using awk, I want to print all lines that have a string in the first column that starts with /dev/vda

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

awk '$1=="/dev/vda/*" {print $3}' 

Is this possible in awk?

Upvotes: 0

Views: 765

Answers (2)

user14473238
user14473238

Reputation:

For string matching there is the index() function:

awk 'index($1, "/dev/vda") == 1'

The function returns the starting position of a substring, which must be 1 if it's at the beginning.

* does not work as a wildcard

This is true – awk does not use shell-like patterns, although * may appear as a regex metacharacter (where it would match zero or more repetitions of the the previous character).

Upvotes: 2

tink
tink

Reputation: 15247

awk '$1~/^\/dev\/vda\// {print $3}'

awk has wildcards in regular expressions, not in string equality checks.

Technically the * is not a wildcard in a regex, it's a quantifier. The regex equivalent of the wildcard * of any number of any character would be .*.

Upvotes: 1

Related Questions