I want to print all lines from a file which begin with four digits. I tried this allredy but it does not work:
cat data.txt | awk --posix '{ if ($1 ~ /^[0-9]{4}/) print $1}'
No output is generated
The next line prints all lins which start with a number:
cat data.txt | awk --posix '{ if ($1 ~ /^[0-9]/) print $1}'
With awk:
$ awk '/^[0-9][0-9][0-9][0-9]/ {print $1}' your_file
that is, check for 4 digits starting the line.
Update: 5th character not to be a digit.
$ awk '/^[0-9][0-9][0-9][0-9]([^0-9].*)?$/ {print $1}' your_file
Note it is not necessary to use the { if ($1 ~ /^[0-9]/)
sentence, it is done with just /^.../
.
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