I have a file structured as record list, where field separator is \t.
I want to extract only records where the second field is a number from 1 to 9, but my awk script doesn’t work.
The awk script is
cat file |awk -v FS="\t" '$2 ~ /[0-9]{1}/ {print $0;}'
or this
cat file |awk -v FS="\t" '$2 ~ /.{1}/ {print $0;}' #because the second fields of my file have all second fields as number
Why these sscript don’t work? Isn’t regex a good regex?
Update
Even with the interval
{1}, you are still going to match a field like23because the2matches a single number. What you really want to use are anchors and forget about intervals:The problem is the use of intervals
{1}.awkless than version 4 doesn’t support intervals.gawkon the other hand will if you add the following flag:--re-intervalTry this:
Some other things to note:
FScan be assigned at the end without the need for-vprintrather thanprint $0as that is its default behaviorcat.awkcan take a file as an argument, use that instead