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

369
Visualizações
how to exclude certain pattern when searching in bash

Let's say I have

 abc-def1-xxx
 abc-def2-yy-vv
 abc-def3
 abc-def4

I want to output abc-def3 and abc-def4

If I use the pattern abc-def* then it outputs everything. If I search for abc-def*-* then it out puts the first two entries How do I get the last two entries?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can make the pattern more specific matching lowercase chars with a hyphen and matching 1 or more digits at the end

  • ^ Start of string
  • [a-z]\+ Match 1+ chars a-z
  • - Match literally
  • [a-z]\+ Match 1+ chars a-z
  • [0-9]\+ Match 1+ digits
  • $ End of string

For example

echo "abc-def1-xxx
abc-def2-yy-vv
abc-def3
abc-def4" | grep '^[a-z]\+-[a-z]\+[0-9]\+$'

Output

abc-def3
abc-def4

You could also match for example abc-def and then 1 or more digits:

^abc-def[0-9]\+$
over 4 years ago · Santiago Trujillo Relatório

0

Let's say the data in put in a file, data.txt

abc-def1-xxx
abc-def2-yy-vv
abc-def3
abc-def4

The command to get the last two lines is:

grep -E '*[0-9]$' data.txt

Explanation:

  • -E, the pattern is interpreted as an extended regular expression.
  • *: any character
  • [0-9]: one digit
  • $: ends with the character just before the $. So here, it indicates that the string must end with a digit.

So it looks at the lines in data.txt, and outputs the lines where the last character is a digit, from 0 to 9.

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