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

127
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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