Using awk to search for strings containing multiple digits

Here is the file1

200
201
202
203
204
205
2001
2002
2003
2004
2005

Is there an awk oneliner that finds only strings with three digits in the first field?

+5
source share
7 answers

If we can assume that the first field contains only numbers:

awk 'length($1) == 3' file1

If not, go to one of the regex solutions.


Alternative solution:

awk '$1 >= 100 && $1 <= 999' file1

print the entire line where the numerical value of the first field is in the range (100,999). This decision has two reservations:

  • 100aapconverted to 100and printed.
  • 005Converts to 5and does not print.
+10
source
awk '$1 ~ /^[0-9][0-9][0-9]$/' file1

($1) ( , ^ $). ($0). {print $0}, .

{} , gawk --posix:

gawk --posix '$1 ~ /^[0-9]{3}$/' file1
+10

:

awk '$1 ~ /^[[:digit:]]{3}$/' file1

, POSIX:

awk '$1 ~ /^[0-9]{3}$/' file1
+4
awk '/^[0-9][0-9][0-9]([^0-9]|$)/ {print $0}' file

, 3 :

awk '/^[0-9][0-9][0-9]$/ {print $0}' file
+2

, -

[jaypal~/Temp]$ cat text7
200
201
202
203
204
205
2001
2002
2003
2004
2005
[jaypal~/Temp]$ awk 'BEGIN{FS="";} NF<4{print}' text7
200
201
202
203
204
205
+1
awk '{ if ($1 ~ /^[0-9][0-9][0-9]$/) print $0}' file

Note that we are using reg-ex, which defines 3 char classes (anything inside [..]), for a total of 0-9. The first field of the file is indicated as $ 1. "^" And "$" indicate the beginning and end of the field. If we don’t have them, fields with 4 or more digits will also coincide.

Hope this helps.

0
source
awk '{num=$1/1; if (num == $1) if (length($1) == 3) print $0}' file

Must work with leading zero (s)

0
source

All Articles