Compare two files and output diff to third file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files and output diff to third file
# 1  
Old 09-24-2008
Compare two files and output diff to third file

I have made several attempts to read two files of ip addresses and eliminate records from file1 that are in file2.

My latest attempt follows. Everything works except my file3 is exactly the same as file1 and it should not be.

Code:
# !/usr/bin/bash
#
# NoInterfaces
# Utility will create a file of ip addresses( file1) from an ARP file from network admin.
# Utility will create a file of interface ip addresses(file2) from an ARP file from network admin.
# Utility will create a file of user ip addresses(file3) from file1 and file2 created within utility
#

# set some variables
DIR=/export/home/gordonl
file1=$DIR/ipstrings     # Large file containing all arp ip strings
file2=$DIR/intfip        # small file containing interface ip strings
file3=$DIR/output.txt    # output file contains all of file1 - file2
# null the files
>$file1
>$file2
>$file3

# Locate all Internet IP Address strings from RtrARP
grep Internet $DIR/RtrARP | cut -d" " -f3 | sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 > $file1

# Locate all Router Interface strings
grep "ip address 1" $DIR/RtrARP | cut -d" " -f4 | sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 > $file2

while read HOST
do
   grep -v "$HOST" $file1 > $file3
done < $file2

thanks in advance for any help.

JB
# 2  
Old 09-24-2008
Replace:

Code:
while read HOST
do
   grep -v "$HOST" $file1 > $file3
done < $file2

with:

Code:
grep -v -f $file2 $file1 > $file3

Regards
# 3  
Old 09-24-2008
You can search the forum or just look here at the bottom of this page for similar problems.
If you can't find anything ? try to post some samples of your file.
# 4  
Old 09-24-2008
Thanks Franklin.... just what I needed. I had to use another version of grep to allow the -f to work.

Code:
/usr/xpg4/bin/grep -v -f $file2 $file1 > $file3

danmero,

I did search and came up with lots of hits, and I tried several of them, using comm, uniq, diff, etc. I felt I needed to show my utility to the group and someone would see my simple processes and help me keep it that way. I would love to be able to just sit down and sream out some perl code, but I am a few lessons short of that at this time.

Thanks again to all of this board. You have helped me every time I came to you.

JB
# 5  
Old 09-24-2008
You want the output of records that are unique to file1.
assuming both files have a record layout of
xxx.xxx.xxx.xxx ie., a single column
Code:
awk 'FILENAME=="file1" { if( $0 in arr) {continue} else {print $0 } }
      FILENAME=="file2" {arr[$0]++ }'  file2 file1

# 6  
Old 09-24-2008
Jim,

I tried that one before posting, but could never get it to give me the expected output. Maybe I did replace the variables in your code correctly. Also all three positions may not be fillled in the last three octets, so it may be xxx.xx.xxx.x or most any way we folks write ip addresses.

Franklin,

I spoke too soon. My output using grep eliminated not just xxx.xxx.xxx.1 but anything with a 1 in the first position of the last octet.

JB
# 7  
Old 09-25-2008
What's the format of the data in the files? Perhaps you want fgrep and/or grep -x and/or massage the data in file1 into proper regular expressions.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to use diff output to compare to a separate file

I have two files: smw:/working/iso_testing # cat a QConvergeConsoleCLI-1.1.03-49.x86_64.rpm aaa_base-13.2+git20140911.61c1681-1.3.i586.rpm acpica-20140724-2.1.2.i586.rpm test.rpm smw:/working/iso_testing # cat b QConvergeConsoleCLI-1.1.03-49.x86_64.rpm... (12 Replies)
Discussion started by: jedlund21
12 Replies

2. UNIX for Dummies Questions & Answers

To compare two files,Output into a new file

Hi Please help me to compare two files and output into a new file file1.txt 15114933 |4001 15291649 |933502 15764675 |4316 15764678 |4316 15761974 |282501 15673104 |933505 15673577 |933505 15673098 |933505 15673096 |933505 15673092 |933505 15760705 ... (13 Replies)
Discussion started by: Ankita Talukdar
13 Replies

3. Shell Programming and Scripting

Compare two CSV files and put the difference in third file with line no,field no and diff value.

I am having two csv files i need to compare these files and the output file should have the information of the differences at the field level. For Example, File 1: A,B,C,D,E,F 1,2,3,4,5,6 File 2: A,C,B,D,E,F 1,2,4,5,5,6 out put file: (12 Replies)
Discussion started by: karingulanagara
12 Replies

4. Shell Programming and Scripting

Compare two files and output in another file

I have two files ' 1st one ALIC-000352-B ALIC-000916-O DDS-STNGD FDH-PPO1-001 PFG-30601-001 2nd one 'ALIC-000352-B' 'ALIC-000916-O' 'DDS-STNGD' 'FDH-PPO1-001' (4 Replies)
Discussion started by: Pratik4891
4 Replies

5. Shell Programming and Scripting

compare 2 CSV fields from same diff output

Attached is a file called diff.txt It is the output from this command: diff -y --suppress-common-lines --width=5000 1.txt 2.txt > diff.txt I have also attached 1.txt and 2.txt for your convenience. Both 1.txt and 2.txt contain one very long CSV string. File 1.txt is a CSV dump of... (0 Replies)
Discussion started by: gvolpini
0 Replies

6. Shell Programming and Scripting

awk to compare diff output by fields

Diff output as follows: < AAA BBB CCC DDD EEE 123 > PPP QQQ RRR SSS TTT 111 > VVV WWW XXX YYY ZZZ 333 > AAA BBB CCC DDD EEE 124 How can i use awk to compare the last field to determine if the counter has increased, and need to ensure that the first 4 fields must have the same... (15 Replies)
Discussion started by: ux4me
15 Replies

7. Shell Programming and Scripting

Using Diff to Compare 2 files

Hi I've been trying various methods that I have found online with regards to comparing 2 files using the diff command. Nothing seems to work. The problem is that I'm not too familiar with the proper syntax. Can you please assist me. Here is my script: #!/bin/bash awk -F',' -v file1="$1"... (9 Replies)
Discussion started by: ladyAnne
9 Replies

8. Shell Programming and Scripting

compare two files, diff the result

Hi Everyone, 1.txt 00:01:01 asdf 00:33:33 1234 00:33:33 0987 00:33:33 12 00:33:33 444 2.txt vvvv|ee 444|dd33|ee dddd|ee 12|ee 3ciur|fdd the output should be: (6 Replies)
Discussion started by: jimmy_y
6 Replies

9. Shell Programming and Scripting

diff 2 files; output diff's to 3rd file

Hello, I want to compare two files. All records in file 2 that are not in file 1 should be output to file 3. For example: file 1 123 1234 123456 file 2 123 2345 23456 file 3 should have 2345 23456 I have looked at diff, bdiff, cmp, comm, diff3 without any luck! (2 Replies)
Discussion started by: blt123
2 Replies
Login or Register to Ask a Question