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

129
Visualizações
How to search exact phrase from a file which consist of set of phrase with hyphen

I have the file, which consists of a couple of phrases as follows. I would like to grep the exact match from out of them.

file.txt

abc
abc-def
xyz
xyz-pqr
pqrs

If I search "abc" I need to return only abc. or if I search "abc-def" i need to return only "abc-def"

preferd output

$grep -w "abc" file.txt
abc

or

$grep -w "abc-def" file.txt
abc-def

the below method is not working for the hyphens

$grep -w abc file.txt 
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

With your given data/file you can use the -x flag.

grep -x abc file.txt

grep -x abc-def file.txt

  • -x, --line-regexp force PATTERN to match only whole lines

  • The -x flag is defined/required by POSIX grep(1)

over 4 years ago · Santiago Trujillo Relatório

0

In order to match an entire line you need to match the start and end of the line:

grep '^abc$' file.txt
grep '^abc-def$' file.txt
over 4 years ago · Santiago Trujillo Relatório

0

You can use awk this way:

awk -v w="abc" '$1==w' file.txt
abc

Or,

awk '$1==w' w="abc" file.txt

With the == operator, it only returns exact string matches. We are setting what to match with w="abc" either with the -v switch or through stdin.

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