I have two files, below. I need to apply the ID numbers from file1 to file2 matching their corresponding hostnames. Desired output also below. I'd prefer to do this with a bash script if possible but am open to alternatives.


ID: 12345, Name: foo1.bar.com
ID: 12346, Name: foo2.bar.com
ID: 12347, Name: foo3.bar.com
ID: 12348, Name: foo4.bar.com
ID: 12349, Name: foo5.bar.com



desired output -

12347 foo3.bar.com
12348 foo4.bar.com
12345 foo1.bar.com
12349 foo5.bar.com
12346 foo2.bar.com

Any ideas on the best way to tackle this?

  • This is the perfect use case for importing into a SQL table and running a query. – ajeh Apr 19 '18 at 15:16

Create a lookup table / hash / associative array from the first file, then use the contents of the second file to key into it:

awk -F'[ ,]+' 'NR==FNR {a[$NF] = $2; next} $1 in a {print a[$1], $1}' file1 file2
12347 foo3.bar.com
12348 foo4.bar.com
12345 foo1.bar.com
12349 foo5.bar.com
12346 foo2.bar.com

Based on the example file, steeldriver’s answer (using a lookup table / associative array in awk) is probably the best solution.  While it is generally inadvisable, you can do the same thing purely in bash:

declare -A id
while IFS=" ," read x idnum xx name
done < file1
while read name
        printf '%s %s\n' "${id[$name]}" "$name"
done < file2

The logic is the same:

  1. A first pass, which creates an array, indexed by name values, containing ID values.
  2. A second pass, which maps names to IDs and outputs them side by side.

My answer works as desired (i.e., as specified) for the sample data in the question.  (Since it uses an array — specifically, an associative array — it requires bash (or ksh, zsh or yash); arrays are not specified by the POSIX specification for the shell, and are not available in all shells that are used in Unix/Linux.)  Because of the way bash’s read command works, my answer also handles multi-word names (i.e., names with spaces in them) as one might intuitively expect:

        file1                             file2                    output

ID: 42, Name: fat cat                   fat cat                  42 fat cat
ID: 95, Name: Superman                  under dog                83 under dog
ID: 83, Name: under dog                 spider man      ⟹       17 spider man
ID:  9, Name: cat woman                 spider pig               60 spider pig
ID: 17, Name: spider man                Superman                 95 Superman
ID: 60, Name: spider pig                cat woman                9 cat woman

Note that both answers are case-sensitive; e.g., foo1.bar.com would not match Foo1.bar.com.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.