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

107
Views
Using file1 as an Index to search file2 when file1 contains extra informations

as you can read in the title Im dealing with two files. Her is the example how the look like.

file1:

Name (additional info separated by a tab from the name)

Peter Schwarzer<tab>Best friend of mine

file2:

Name (followed by a float separated by a tab from the name)

Peter Schwarzer<tab>1456

So what i want to do is use file1 one as an index for searching file2. If the Names match it should be written in file3 which should contain the Name followed by the float from file2 followed by the additional info from file1. So file3 should look like:

Peter Schwarzer<tab>1456<tab>Best friend of mine 

(everything separated by tab)

I tried grep -f to read a pattern from a file and without the additional information it works. So is there any way to get the desired result with grep or is AWK the answer?

Thanks in advance, Julian

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

give this line a try, I didn't test, but should go:

awk -F'\t' -v OFS="\t" 'NR==FNR{n[$1]=$2;next}$1 in n{print $0,n[$1]}' file1 file2 > file3
over 4 years ago · Santiago Trujillo Report

0

Try this awk one liner!

awk -v FS="\t" -v OFS="\t" 'FNR==NR{ A[$1]=$2; next}$1 in A{print $0,A[$1];}' file1.txt  file2.txt > file3.txt
over 4 years ago · Santiago Trujillo Report

0

To me this looks like a job for join:

join -t '\t' file1 file2

This assumes file1 and file2 are sorted. If not, sort them first:

sort -o file1 file1
sort -o file2 file2
join -t '\t' file1 file2

If you can't modify file1 and file2 (if you need to leave them in their original, unsorted state), use a temporary file:

tmpfile=/tmp/tf$$
sort file1 > $tmpfile
sort file2 | join -t '\t' $tmpfile -

If join says "illegal tab character specification" you'll have to use join -t ' ' where you type an actual tab between the single quotes (and depending on your shell, you may have to use control-V before that tab).

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!