Parsing an input file through awk I ran into an issue with anchors in awk.
Given the following file:
2015
2015
test
test
Output with awk
$ awk '$1 ~ /^[0-9]/' file
2015
2015
Output with sed
$ sed -n '/^[0-9]/p' file
2015
Can somebody explain the behaviour I'm seeing in awk?
Seen with
You will understand the difference with this awk command:
awk '/^[0-9]/' file
2015
Now awk is operating on full line like sed not just the first field.
$1 ~ /^[0-9]/
only compares first field and since whitespace is default field separator in awk therefore first field is 2015
in both the lines irrespective of spaces before it.
The problem is you are picking the first field.
You should be doing awk '/^[0-9]/' file
which matches the whole line.
To be more precise:
awk '$0 ~ /^[0-9]/' file
Is what you want, as $0
is the whole line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With