Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk, print lines which start with four digits

Tags:

awk

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}'
like image 601
Lukas Banach Avatar asked Apr 29 '13 14:04

Lukas Banach


1 Answers

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 /^.../.

like image 113
fedorqui 'SO stop harming' Avatar answered Oct 03 '22 08:10

fedorqui 'SO stop harming'