CZR
CZR

Reputation: 5

How to filter for only the lines that all its fields satisfy a regex condition

I am trying to use a condition in awk in a way that filters for lines that all its fields satisfy a regex condition, other than looping through the fields one by one. For example, I want the lines that have only (yes or ok) in them

example input:

yes yes yes no no ok nok
no yes yes whatever
yes yes ok
ok yes

example output:

yes yes ok
ok yes

Upvotes: 0

Views: 68

Answers (1)

Tyl
Tyl

Reputation: 5262

Depends on your delimiter, let's say it's space or tab, then you can do this:

awk '$0~/^((yes|ok)\s+)*(yes|ok)$/{print}' file

ps: \s is GNU awk's feature, you might need to change it to [ \t] for other version.

And the $0~ and {print} parts are actually implicit, we can drop them, simply use this:

awk '/^((yes|ok)\s+)*(yes|ok)$/' file

Upvotes: 3

Related Questions