Sponsored Content
Full Discussion: Q: Howto compare 2 files
Top Forums UNIX for Dummies Questions & Answers Q: Howto compare 2 files Post 302696083 by Don Cragun on Tuesday 4th of September 2012 09:03:43 AM
Old 09-04-2012
Save the following in a file named diffs.awk:
Code:
#!/bin/ksh
if [ $# -ne 2 ]
then
        printf "Usage: %s f1 f2\n    Two file operands are required.\n" "$(basename "$0")"
        exit 1
fi
awk 'FNR==1{
        if(NR==1) f1 = FILENAME
        else    f2 = FILENAME
}
f2=="" {# Make an entry for this record from the 1st file in array c1:
        c1[$0]
        next
}
 {      # We are now in the 2nd file.  Look for matching entry from 1st file.
        if($0 in c1) {
                # Matching entry found.  Delete the entry from the list of
                # unmatched entries from the 1st file.
                delete c1[$0]
                next
        }
        # No match found...
        if(c2only++ == 0) printf("The following entries are only in %s:\n", f2)
        print
}
END {   # Any entries remaining in c1 are present only in the 1st file.
        for(i in c1) {
                if(c1only++ == 0)
                        printf("The following entries are only in %s:\n", f1)
                print i
        }
        printf("%d unmatched entries found in %s\n", c1only, f1)
        printf("%d unmatched entries found in %s\n", c2only, f2)
}' "$@"

and make it executable:
Code:
chmod +x diffs.awk

then, for your example, invoke it with:
Code:
diffs.awk File1 File2

With File1 and File2 as shown in the first message in this thread, the output produced is:
Code:
The following entries are only in File1:
456123456
1 unmatched entries found in File1
0 unmatched entries found in File2

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Howto Archive Including Hidden Files?

Hi I want to archive the following all the files and directory like listed below: $ ls -a . .. .bash_history .bash_logout .bash_profile .bashrc .emacs .mysql_history public_html .viminfo What I tried is to use the following command $ gtar cvzf allmyfiles.tar.gz * ... (1 Reply)
Discussion started by: monkfan
1 Replies

2. UNIX for Dummies Questions & Answers

Howto removing files with the same inode

Dear all, # ls -li total 16 2623392 drwxrwxrwx 2 root root 512 Apr 10 01:57 10HPA- 8447490 drwxr-xr-x 3 root root 512 Apr 14 05:29 118OQ- 8447490 drwxr-xr-x 3 root root 512 Apr 14 05:29 118OQ-.old 1925572 drwxrwxrwx 2 root root 512... (3 Replies)
Discussion started by: fu4d
3 Replies

3. Shell Programming and Scripting

compare files in two directories and output changed files to third directory

I have searched about 30 threads, a load of Google pages and cannot find what I am looking for. I have some of the parts but not the whole. I cannot seem to get the puzzle fit together. I have three folders, two of which contain different versions of multiple files, dist/file1.php dist/file2.php... (4 Replies)
Discussion started by: bkeep
4 Replies

4. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

5. Shell Programming and Scripting

HowTo translate KSH Scripts to DOS Batch Files ?

Hi there, in near future I have to change my work surrounding from HP UNIX to Windows Vista (great to get rid of old hardware :), but bad to loose UNIX :( ). As I heavily use KSH scripts to do my job, I was wondering, if there is any HowTo available, supporting me in re-writing the scripts to... (4 Replies)
Discussion started by: Joe-K7
4 Replies

6. Shell Programming and Scripting

Compare 2 folders to find several missing files among huge amounts of files.

Hi, all: I've got two folders, say, "folder1" and "folder2". Under each, there are thousands of files. It's quite obvious that there are some files missing in each. I just would like to find them. I believe this can be done by "diff" command. However, if I change the above question a... (1 Reply)
Discussion started by: jiapei100
1 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

howto monitor a directory for files then sftp them

Morning all I hope I have put this in the correct forum. I have a requirement to monitor a directory on a server for files being sftp'ed in and then to sftp them of to another server. The issues I have though of are making sure the files have completely transferred onto the server before they... (6 Replies)
Discussion started by: ltodd2
6 Replies

9. Shell Programming and Scripting

Compare multiple files, and extract items that are common to ALL files only

I have this code awk 'NR==FNR{a=$1;next} a' file1 file2 which does what I need it to do, but for only two files. I want to make it so that I can have multiple files (for example 30) and the code will return only the items that are in every single one of those files and ignore the ones... (7 Replies)
Discussion started by: castrojc
7 Replies

10. Shell Programming and Scripting

Howto compare the columns of 2 diff tables of 2 different schemas in UNIX shell script

HI All, I am new to Unix shell scripts.. Could you please post the unix shell script for for the below request., There are two different tables(sample1, sample2) in different schemas(s_schema1, s_schema2). Unix shell script to compare the columns of two different tables of two... (2 Replies)
Discussion started by: Rajkumar Gopal
2 Replies
DXmSvnGetDisplayed(3X)													    DXmSvnGetDisplayed(3X)

NAME
DXmSvnGetDisplayed - Retrieves (returns) displayed entry numbers and related information necessary to draw a corresponding display. SYNOPSIS
void DXmSvnGetDisplayed( Widget widget, int *entries(), XtPointer *tags(), int *ys(), int len ); PARAMETERS
The identifier (widget ID) of the SVN widget. A pointer to an array of integers to receive the entry numbers of the entries being dis- played. A pointer to an array of longwords to receive the application's entry_tag value for each entry displayed. If tags are not required, a Null pointer may be passed. A pointer to an array of longwords to receive the y coordinates for each entry displayed. If y coordinates are not required, a Null pointer can be passed. The number of entries allocated in the provided array. DESCRIPTION
The DXmSvnGetDisplayed routine returns information about the entries that the SVN widget is currently displaying. This information can then be used to keep a simultaneous display up to date with the SVN widget window (in the case of a dialog box, for example, which might contain totals for the entries being displayed). The application is responsible for managing the memory used to return this list of entries. As such, note the following: At the minimum, the number of entries in the array should be capable of holding at least the number of entries indicated by the value returned from the DXmSvnGetNumDisplayed routine. If there are more entries in the array than the application will need, the SVN widget will set the value for those extra entries to 0. If the capacity of the arrays passed is less than the number of selected entries, only the number of entries allocated in the provided array (the value for len) will be returned. DXmSvnGetDisplayed(3X)
All times are GMT -4. The time now is 04:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy