tags:

views:

603

answers:

2

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:

  • Be general for all fields, regardless of the field separator used.
  • Not treat the entire input as a single field and parse it manually using string functions.
  • Work in a general way and not be specific to gawk for example.

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_
+5  A: 

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

/^[01]*_[01]*$/ { print $0 }
Paul Tomblin
+3  A: 
$ gawk '/^[01]*_[01]*$/' indata.txt
1010_
J.F. Sebastian
I know your answer is equal to Paul's. And I can't tell who was written first. I picked Paul's answer as my accepted by letting chance decide.
DeletedAccount
@Kent: Paul was the first to answer.
J.F. Sebastian
But it's not an identical answer. This one is much more awk-ish.
PEZ