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

80
Views
How to make a strict match with awk

I am querying one file with the other file and have them as following:

File1:

Angela S Darvill| text text text text   
Helen Stanley| text text text text   
Carol Haigh S|text text text text .....

File2:

Carol Haigh  
Helen Stanley  
Angela Darvill

This command:

awk 'NR==FNR{_[$1];next} ($1 in _)' File2.txt File1.txt

returns lines that overlap, BUT doesn’t have a strict match. Having a strict match, only Helen Stanley should have been returned.

How do you restrict awk on a strict overlap?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

With your shown samples please try following. You were on right track, you need to do 2 things, 1st: take whole line as an index in array a while reading file2.txt and set field seapeator to | before awk starts reading file1

awk -F'|' 'NR==FNR{a[$0];next} $1 in a' File2.txt File1.txt

Command above doesn’t work for me (I am on Mac, don’t know whether it matters), but

awk 'NR==FNR{_[$0];next} ($1 in _)' File2.txt. FS="|" File1.txt

worked well

over 4 years ago · Santiago Trujillo Report

0

You can also use grep to match from File2.txt as a list of regexes to make an exact match.

You can use sed to prepare the matches. Here is an example:

sed -E 's/[ \t]*$//; s/^(.*)$/^\1|/' File2.txt
^Carol Haigh|
^Helen Stanley|
^Angela Darvill|
...

Then use process with that sed as an -f argument to grep:

grep -f <(sed -E 's/[ \t]*$//; s/^(.*)$/^\1|/' File2.txt) File1.txt
Helen Stanley| text text text text  

Since your example File2.txt has trailing spaces, the sed has s/[ \t]*$//; as the first substitution. If your actual file does not have those trading spaces, you can do:

grep -f <(sed -E 's/.*/^&|/' File2.txt) File1.txt

Ed Morton brings up a good point that grep will still interpret RE meta-characters in File2.txt. You can use the flag -F so only literal strings are used:

grep -F -f <(sed -E 's/.*/&|/' File2.txt) File1.txt
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!