compare two files for matching in solaris


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare two files for matching in solaris
# 1  
Old 10-21-2008
Bug compare two files for matching in solaris

I am using solaris, need either awk/shell or perl script to compate two files.
both of these file1 and file2 are located in two diffent location path in the same server.
file1 location: /export/db/mna
file2: /etc/dba

The script will compare in file1 first field (e.g. abc00asp)before colon:
and will look for match in file2 second field (e.g. abc00asp)


File1 format:


abc00asp:11.83.0.1
def01asp:11.83.0.2
efg00asy:11.83.0.5
rkl01atm:11.83.0.6
ppr00rsm:11.83.0.7
trt00nsm:11.83.0.8



File2 Format:

172.25.0.97 abc00asp abc00asp.atl.mnn.net
172.25.0.97 ppr00rsm ppr00rsm.atl.mnn.net
172.25.0.97 bppr00rsm pr00rsm.atl.mnn.net
172.25.0.97 ypr00rsm ppr00rsm.atl.mnn.net
172.25.0.97 ppr00rsm ppr00rsm.atl.mnn.net
172.25.0.97 trt00nsm trt00nsm.atl.mnn.net


The outfile will indicate match files:


abc00asp
ppr00rsm
trt00nsm


Thank you.
# 2  
Old 10-21-2008
Hammer & Screwdriver Here is one solution

Code:
> cat file261
abc00asp:11.83.0.1
def01asp:11.83.0.2
efg00asy:11.83.0.5
rkl01atm:11.83.0.6
ppr00rsm:11.83.0.7
trt00nsm:11.83.0.8

> cat file262
172.25.0.97 abc00asp abc00asp.atl.mnn.net
172.25.0.97 ppr00rsm ppr00rsm.atl.mnn.net
172.25.0.97 bppr00rsm pr00rsm.atl.mnn.net
172.25.0.97 ypr00rsm ppr00rsm.atl.mnn.net
172.25.0.97 ppr00rsm ppr00rsm.atl.mnn.net
172.25.0.97 trt00nsm trt00nsm.atl.mnn.net

> cat comp_261_262 
#! /usr/bin/bash

cut -d":" -f1 file261 | sort >file261a
cut -d" " -f2 file262 | sort >file262a

comm -12 file261a file262a

> comp_261_262 
abc00asp
ppr00rsm
trt00nsm
>

# 3  
Old 10-21-2008
comp_261_262 is the actual script file, after running the script it creates 2 files:
file261a and file262a


but I dont understand how u get here:
how did u find the match result.


> comp_261_262
abc00asp
ppr00rsm
trt00nsm
>

Last edited by amir07; 10-21-2008 at 04:59 PM..
# 4  
Old 10-21-2008
Hammer & Screwdriver comp_261_262

That is the name of a short script I wrote. Perhaps it all could have been done in one command, but it is easier to see with the three commands inside one executable file.

The file contains:
#! /usr/bin/bash

cut -d":" -f1 file261 | sort >file261a
cut -d" " -f2 file262 | sort >file262a

comm -12 file261a file262a


the first part sets the version for scripting
then two cut commands to get to the important part of each file
finally, the comm command to show what is in common
# 5  
Old 10-21-2008
well, this cut -d" " -f2 file262 | sort >file262a does not isolate the files 2nd field.
There is a tab inbetween for this file data.

But I am still could not get the result of matching field.
# 6  
Old 10-21-2008
Hammer & Screwdriver if it is a tab, a couple alternatives

As below, could change the tab to a space, so the previous logic would work.

Code:
> cat comp_261_262 
#! /usr/bin/bash

cut -d":" -f1 file261 | sort >file261a
tr "\t" " " <file262 | cut -d" " -f2 | sort >file262a

comm -12 file261a file262a

Could also use awk to take apart the two input files. Awk treats space characters and tab characters as delimiters.
# 7  
Old 10-21-2008
It works this time, since TAB logic fixes it.
One more help how can I reverse the logic so that ONLY UN-MATCH items shows up.

thanks and appreciate it very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare & print content of file which is not matching

Hi All I want to compare 2 files using awk and get output of content which is not matching I have 2 files a.txt 123 456 780 143 b.txt A|B|C|167|D|E C|K|D|123|D|E A|B|D|789|G|F C|D|G|143|A|B Not matching line from b.txt O/P A|B|C|167|D|E A|B|D|789|G|F (3 Replies)
Discussion started by: aaysa123
3 Replies

2. Shell Programming and Scripting

Compare two files and count number of matching lines

Dear All, I would like to compare two files and return the number of matches found. Example File A Lx2 L1_Mus1 L1Md_T Lx5 L1M2 L1_Mus3 Lx3_Mus Lx9 Lx2A L1Md_A L1Md_F2 File B L1_Mus3 L1_Mus3 (3 Replies)
Discussion started by: paolo.kunder
3 Replies

3. Shell Programming and Scripting

Compare and matching column entries in 2 files and

I have 2 files. File 1 has more columns (6 columns but the last column has spaces) than file 2 (file 2 has 4 columns). The entries in file 1 do not change but column 4 in file 2 can be different from the the entry in file 1. I want to create a script that reads in file 1 and then uses column 1 2... (5 Replies)
Discussion started by: kieranfoley
5 Replies

4. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

Compare two files and get matching data

Hi, Can anyone help me to compare two files and get the matching data... say i have file1 and file2 ... file1 has 300 unique data with that i need to match with file2 to see how may are matching.. file2 have 1000 records. (4 Replies)
Discussion started by: zooby
4 Replies

7. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

8. Shell Programming and Scripting

compare two columns of different files and print the matching second file..

Hi, I have two tab separated files; file1: S.No ddi fi cu o/l t+ t- 1 0.5 0.6 o 0.1 0.2 2 0.2 0.3 l 0.3 0.4 3 0.5 0.8 l 0.1 0.6 ... (5 Replies)
Discussion started by: vasanth.vadalur
5 Replies

9. Shell Programming and Scripting

compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files (4 Replies)
Discussion started by: shellscripter
4 Replies

10. Shell Programming and Scripting

Compare two files UNMATCH in solaris

I have these following code, which give me output of a match items. Can any please help me to reverse the code so that I can get UNMATCH items: #! /usr/bin/bash cut -d":" -f1 file261 | sort >file261a tr "\t" " " <file262 | cut -d" " -f2 | sort >file262a comm -12 file261a file262a ... (8 Replies)
Discussion started by: amir07
8 Replies
Login or Register to Ask a Question