user3595099 user3595099 - 1 year ago 101
Linux Question

Merging and comparing files using Linux command

I have a question about merging 2 data files. The first file contains an IP address for the name of each server. The second contains a data item and the associated IP address.

For output, I'd like to have the input of the second file, but with the mapped server name, looked up in the first file.

File_1: server_1 server_2 server_3



I need to figure out a command for getting the output below:


2 server_3
4 server_1

Answer Source

You can use awk:

awk 'FNR==NR{a[$1]=$2; next} $2 in a{print $0, a[$2]}' file1 file2

2 server_3
4 server_1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download