Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda