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 /^.../
.
For printing lines that match a given regexp grep
is the first tool to grab:
grep -Eo '^[0-9]{4}[^0-9]*\s' file
I cannot see the problem.
awk --posix '{ if ($1 ~ /^[0-9]{4}/) print $1}'<<EOT
1234 qwer
234 asdf
34456 qwe
EOT
gets
1234
34456
As expected...
If You need match exactly four digits, then You could use:
awk --posix '$1~/^[0-9]{4}$/{print $1}'<<EOT
1234 qwer
234 asdf
34456 qwe
EOT
Output:
1234
But actually You do not need to run awk
if you are in bash
:
while read f rest; do
[[ $f =~ ^[[:digit:]]{4}$ ]] && echo $f
done <<EOT
1234 qwer
234 asdf
34456 qwe
EOT
Output:
1234