Compare two files and set a third one using awk or perl

Folks I need your help cuz I've a file with 100,000 records that need to be compared against a passwd file (300) and then create a third one with the data in the first one and the passwd from the second one set in it.

The format of the first file is:

host xxxxxx "" 0,0 Closed control00/ SOLARIS 0x0+0+0 "TCP"
host yyyyyy "" 0,0 Closed control01/ SOLARIS 0x0+0+0 "TCP"
...
host 222222 "" 0,0 Closed control30/ Linux 0x0+0+0 "TCP"

the second one has a key and the passwd as below:

control00 45EF0E4228F59D60521D071AC55E4BAC
...
control30 F93F0F0BC36E597B28D325BD10F99C4B

the third file should looks like:

host 222222 "" 0,0 Closed control30/F93F0F0BC36E597B28D325BD10F99C4B Linux 0x0+0+0 "TCP"

Now you migth think piece of cake :stuck_out_tongue: for you yes, for me no, I know that I need to read one record in the second file and the read all the records in the first one with this passwd in it and then generate the third and so on, but to be honest I don't know how to open, read & control each of them in awk or perl.

I already reviewed the archive with no luck cuz, there're only examples about comparing files.

Can you help me with this?, I'm lost and desperate.

TIA for your help. Raul. :slight_smile:

nawk '
  FNR==NR { f1[$1]=$2;next }
  (s=substr($6,1,length($6)-1)) in f1 {$6=$6 f1;print}
' file2 file1 > file3
1 Like
#!/bin/bash
for i in $(sed 's/\(.*\) .*/\1/' file2)
 do
    x=$(sed -n "/$i/s/.* \(.*\)/\1/p" file2)
    sed -n "/$i/s/\(.*\)\/ \(.*\)/\1\/$x \2/p" file1 >> file3
 done
 more file3
 
host xxxxxx "" 0,0 Closed control00/45EF0E4228F59D60521D071AC55E4BAC SOLARIS 0x0+0+0 "TCP"
host yyyyyy "" 0,0 Closed control01/45EF0E4228F59D60521D071AC55E4BAX SOLARIS 0x0+0+0 "TCP"
host 222222 "" 0,0 Closed control30/F93F0F0BC36E597B28D325BD10F99C4B Linux 0x0+0+0 "TCP"
1 Like
$
$
$ cat file1
host xxxxxx "" 0,0 Closed control00/ SOLARIS 0x0+0+0 "TCP"
host yyyyyy "" 0,0 Closed control01/ SOLARIS 0x0+0+0 "TCP"
host 222222 "" 0,0 Closed control30/ Linux 0x0+0+0 "TCP"
$
$ cat file2
control00 45EF0E4228F59D60521D071AC55E4BAC
control01 98XY1A5643Q78T93728A670BD29C9PJM
control30 F93F0F0BC36E597B28D325BD10F99C4B
$
$ perl -lane 'if ($ARGV eq "file2") {$x{$F[0]}=$F[1]} else {/^(.*) (.*?)(\/)(.*)$/ && print "$1 $2$3$x{$2}$4"}' file2 file1
host xxxxxx "" 0,0 Closed control00/45EF0E4228F59D60521D071AC55E4BAC SOLARIS 0x0+0+0 "TCP"
host yyyyyy "" 0,0 Closed control01/98XY1A5643Q78T93728A670BD29C9PJM SOLARIS 0x0+0+0 "TCP"
host 222222 "" 0,0 Closed control30/F93F0F0BC36E597B28D325BD10F99C4B Linux 0x0+0+0 "TCP"
$
$
$

tyler_durden

1 Like

Folks, thanks a lot for your help all the solutions works great, you saved my soul.

Now I would like to understand the awk code, can you explain to me vgersh99?, I see that you assing one field in an array and you set it later, but I don't understand the rest of the code.

I appreciate too much your help & patience.

Have a nice weekend.