Match Awk entire record using regular expression

Using Awk, I want to match the entire entry using regex. By default, regex matching refers to portions of the record.

Ideal solution:

  • Shared across all fields, regardless of the field separator used.
  • Don't treat all input as one field and parse it manually using string functions.
  • Work mostly and don't be specific to gawk for example.

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

For 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'm looking for:

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

      

+1


source to share


2 answers


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



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

      

+4


source


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

      



+2


source







All Articles