user14070
user14070

Reputation:

Awk matching of entire record using regular expression

Using Awk I want to match the entire record using a regular expression. By default the regular expression matching is for parts of a record.

The ideal solution would:

However any and all solutions are of interest as long as they use Awk without calls to external programs.

An example, I have:

$ ls
indata.txt  t1.awk
$ cat indata.txt 
a1010_
1010_
1010_b
$ cat t1.awk 
/[01]*_[01]*/ { print $0 }

I get:

$ awk -f t1.awk indata.txt
a1010_
1010_
1010_b

This is the result I am seeking:

$ awk -f t1.awk indata.txt
1010_

Upvotes: 1

Views: 1249

Answers (2)

jfs
jfs

Reputation: 414235

$ gawk '/^[01]*_[01]*$/' indata.txt
1010_

Upvotes: 2

Paul Tomblin
Paul Tomblin

Reputation: 182782

You just need to add a beginning and end anchor to your regex:

/^[01]*_[01]*$/ { print $0 }

Upvotes: 4

Related Questions