Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!