0

we have two files - primary.txt and secondery.txt

on both files we have the same parameters , but on secondary.txt they are not ordered as the primary.txt

more primary.txt

param.avarge.com 3443
param.no.com 43
param.lol.com 54
param.tis.com 24
param.er.com 254
param.nh.com 13
param.pl.com 456
param.nm.com 534
param.asd.com 236

more secondery.txt

param.no.com 42
param.lol.com 51
param.tis.com 21
param.avarge.com 343
param.er.com 259
param.nh.com 131
param.pl.com 45
param.nm.com 50
param.asd.com 23

we want to merge the secondery.txt values with primary.txt values as the following:

more merge.txt

param.avarge.com 3443 343
param.no.com 43 42
param.lol.com 54 51
param.tis.com 24 21
param.er.com 254 259
param.nh.com 13 131
param.pl.com 456 45
param.nm.com 534 50
param.asd.com 236 23

I will appreciate if we get suggestion for this , so I will update the solution in my bash script

2
  • Rough size of files, in particular will it fit in memory? Commented Dec 31, 2019 at 14:19
  • @icarus' answer is just fine, but if you don't mind having a sorted ouput, then another simpler possibility is: $ join -t " " -j 1 <(sort primary.txt) <(sort secondary.txt). Caveat: sorting input files is necessary for join to work. Commented Jan 6, 2020 at 10:30

1 Answer 1

2

If it will all fit in memory, then

 awk 'NR==FNR { s[$1]=$2 ; next }
      { print $0,s[$1] }' secondary.txt primary.txt

The NR==FNR is a trick which tells you if you are processing the first file (assuming the first file is not empty). If it is the first file then store the value of the second column into an associative array and move on to the next line. If we don't move on then we must be processing the second file, so print out the value from the primary file and the stored value.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.