Bash script compare values from 2 files and print output values from one file

2.1k Views Asked by At

I have two files like this;

File1
114.4.21.198,cl_id=1J3W7P7H0S3L6g85900g736h6_101ps 114.4.21.205,cl_id=1O3M7A7Q0S3C6h85902g7b3h7_101pf 114.4.21.205,cl_id=1W3C7Z7W0U3J6795197g177j9_117p1 114.4.21.213,cl_id=1I3A7J7N0M3W6e950i7g2g2i0_1020h

File2
cl_id=1B3O7M6C8T4O1b559i2g930m0_1165d
cl_id=1X3J7M6J0W5S9535180h90302_101p5
cl_id=1G3D7X6V6A7R81356e3g527m9_101nl
cl_id=1L3J7R7O0F0L74954h2g495h8_117qk
cl_id=1L3J7R7O0F0L74954h2g495h8_117qk
cl_id=1J3W7P7H0S3L6g85900g736h6_101ps
cl_id=1W3C7Z7W0U3J6795197g177j9_117p1
cl_id=1I3A7J7N0M3W6e950i7g2g2i0_1020h
cl_id=1Q3Y7Q7J0M3E62953e5g3g5k0_117p6

I want to compare cl_id values that exist on file1 but not exist on file2 and print out the first values from file1 (IP Address).

it should be like this

114.4.21.198
114.4.21.205
114.4.21.205
114.4.21.213
114.4.23.70
114.4.21.201
114.4.21.211 120.172.168.36

I have tried awk,grep diff, comm. but nothing come close. Please tell the correct command to do this.

thanks

4

There are 4 best solutions below

0
On

I do not see how you get your output. Where does 120.172.168.36 come from. Here is one solution to compare

awk -F, 'NR==FNR {a[$0]++;next} !a[$1] {print $1}' file2 file1
114.4.21.198
114.4.21.205
114.4.21.205
114.4.21.213
0
On

This seems like what you want to do and might work, efficiently:

grep -Ff file2.txt file1.txt | cut -f1 -d,

First the grep takes the lines from file2.txt to use as patterns, and finds the matching lines in file1.txt. The -F is to use the patterns as literal strings rather then regular expressions, though it doesn't really matter with your sample.

Finally the cut takes the first column from the output, using , as the column delimiter, resulting in a list of IP addresses.

The output is not exactly the same as your sample, but the sample didn't make sense anyway, as it contains text that was not in any of the input files. Not sure if this is what you wanted or something more.

0
On

Feed both files into AWK or perl with field separator=",". If there are two fields, add the fields to a dictionary/map/two arrays/whatever ("file1Lines"). If there is just one field (this is file 2), add it to a set/list/array/whatever ("file2Lines"). After reading all input:

Loop over the file1Lines. For each element, check whether the key part is present in file2Lines. If not, print the value part.

2
On

One proper way to that is this:

grep -vFf file2 file1 | sed 's|,cl_id.*$||'