Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
How can I match lines with exactly three numbers?

How can I use grep to match 3 numbers in a file? My file looks like this:

123  
122    
222  
333443  
fdsfs5454353  
dsfsfjsk4654641

Note that some of the lines contain trailing spaces. I want to only match three digit numbers. I tried:

grep -E [0-9]{3} test.txt
grep -E '\<[0-9]{3}\>' test.txt
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

grep '^[0-9][0-9]*' test|awk '{if(length($0) == 3) print $0}' 

or if you have whitespace:

sed 's/[ \t]*$//' test|grep   '^[0-9][0-9]*'|awk '{if(length($0) == 3) print $0}'

(thanks @shellter)

over 4 years ago · Santiago Trujillo Denunciar

0

Use Extended Regular Expressions with Bounds

I asked if you meant numbers with exactly three digits, or each three-digit match in a string. You replied that you wanted only lines that contained exactly three digits.

Extended grep provides an easy solution for this. Consider the following:

$ egrep '^\d{3}\b' /tmp/corpus 
123  
122    
222

This uses a bound (also known as a range) to look for exactly three digits at the start of each line, followed by a word boundary. The word boundary will match trailing space or the end of line, ensuring that you get the proper match in either case.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda