5

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}'
Lukas Banach
  • 51
  • 1
  • 1
  • 3
  • 6
    Another example of a [useless use of cat](http://partmaps.org/era/unix/award.html). – Dan Fego Apr 29 '13 at 14:47
  • Print all lines that match or print the first field of all line that match? – Chris Seymour Apr 29 '13 at 14:49
  • Awk is fundamentally a pattern/action language; [understanding patterns](http://www.gnu.org/software/gawk/manual/gawk.html#Pattern-Overview) is a fundamental skill. [Regular expressions](http://www.gnu.org/software/gawk/manual/gawk.html#Regexp-Patterns) are one kind of pattern. You should probably use grep instead here, but learn awk's pattern/action stuff anyway. – Mike Sherrill 'Cat Recall' Apr 29 '13 at 15:23
  • @MikeSherrill'Catcall': Actually you do not need `awk` nor `grep`. It can be solved easily in `bash` as well. – TrueY May 22 '13 at 22:31

3 Answers3

7

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

tripleee
  • 175,061
  • 34
  • 275
  • 318
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Thank you this helps allready a bit. I forgot to mention that i needs to start with exactlly four digits. Your solution shows also >4 digits at the begining. – Lukas Banach Apr 29 '13 at 14:51
  • You might still want to consider the corner case of a line containing exactly four digits; `/^[0-9][0-9][0-9][0-9]([^0-9].*)$/...` – tripleee Apr 29 '13 at 15:10
2

For printing lines that match a given regexp grep is the first tool to grab:

grep -Eo '^[0-9]{4}[^0-9]*\s' file
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
0

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
TrueY
  • 7,360
  • 1
  • 41
  • 46