Sponsored Content
Full Discussion: Diff for multiple files
Top Forums UNIX for Dummies Questions & Answers Diff for multiple files Post 302813983 by Don Cragun on Tuesday 28th of May 2013 01:37:57 PM
Old 05-28-2013
When I thought you always wanted to work on three directories, I started working on the following script:
Code:
#!/bin/ksh
IAm="${0##*/}"
if [ $# -ne 3 ]
then    printf "Usage: %s dir1 dir2 dir3\n" "$IAm" >&2
        exit 1
fi
if [ ! -d "$1" ] || [ ! -d "$2" ] || [ ! -d "$3" ]
then    printf "%s: All three operands must be directories\n" "$IAm" >&2
        exit 2
fi
if [ "x${2:0:1}" = x/ ]
then    d2="$2"
else    d2="$PWD/$2"
fi
if [ "x${3:0:1}" = x/ ]
then    d3="$3"
else    d3="$PWD/$3"
fi
if ! cd "$1"
then    printf "%s: Can't change directory to \"%s\"\n" "$IAm" "$1" >&2
        exit 3
fi
for i in *
do      if [ ! -e "$d2/$i" ]
        then    if [ ! -e "$d3/$i" ]
                then    printf "Only in %s: %s\n" "$1" "$i"
                else    printf "Only in %s and %s: %s\n" "$1" "$3" "$i"
                 fi 
        else    if [ ! -e "$d3/$i" ]
                then    printf "Only in %s and %s: %s\n" "$1" "$2" "$i"
                fi
        fi
done

which produces the output:
Code:
Only in dir1: 2.txt
Only in dir1 and dir3: 5.txt
Only in dir1: 6.txt

for the directory hierarchy given in the 1st message in this thread when it is saved in a file named tester and invoked as:
Code:
./tester dir1 dir2 dir3
    or
./tester dir1 dir3 dir2

Note that it was written using the Korn shell, but it uses an extension not specified by the standards (${var:start:length} to get a substring from a variable). This is at least available in recent versions of bash and ksh versions more recent that November 16, 1988.

Your specification of the output you wanted is very vague, so this might not even be close to what you want; but I hope it will give you some idea of how to approach your problem.

If you want us to pursue your new, more general problem with an unlimited number of directories and unspecified combinations of comparisons, you need to give a much more detailed description of what you want to happen, how you want to specify the options and operands for this command, and how you want the output formatted. Try to write a man page describing the utility you want to create.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. 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

2. AIX

Locking a file when using VI to prevent multiple-edit sessions by diff users

At the office, we often have to edit one file with VI. We are 4-6 workers doing it and sometimes can be done at the same time. We have found a problem and want to prevent it with a file lock. Is it possible and how ? problem : Worker-a starts edit VI session on File-A at 1PM Worker-b... (14 Replies)
Discussion started by: Browser_ice
14 Replies

3. Shell Programming and Scripting

Find duplicates from multuple files with 2 diff types of files

I need to compare 2 diff type of files and find out the duplicate after comparing each types of files: Type 1 file name is like: file1.abc (the extension abc could any 3 characters but I can narrow it down or hardcode for 10/15 combinations). The other file is file1.bcd01abc (the extension... (2 Replies)
Discussion started by: ricky007
2 Replies

4. Shell Programming and Scripting

Diff b/w 2 files

Hi Masters, I have two files named file1 and file2. Both the files contains the same contents with some difference in comments,space.But no content change. I tried to find the diff between the two files to make sure that contents are same. For that i tried diff -ibw file1 file2 But... (1 Reply)
Discussion started by: ecearund
1 Replies

5. Shell Programming and Scripting

Call single function multiple times diff set of parameters

Okay, not sure if it can be done, I would think it could be done and I'm just having a hard time with it. fun_close_wait(){ ipVar="${1} ${2}" portVar=`cat "${5}" | cut -d' ' -f 1` for ip in $ipVar do for port in $portVar do netstatVar=`netstat -n | grep... (4 Replies)
Discussion started by: cbo0485
4 Replies

6. Shell Programming and Scripting

diff of files

Hi, I have 2 files.I want to check if file1 is contained in file2. A.txt: ----- AAA BBB B.txt: ------ CCC AAA BBB DDD I want to check if A.txt is contained in B.txt. Can it be done using SED ? (12 Replies)
Discussion started by: giri_luck
12 Replies

7. Shell Programming and Scripting

diff bw two files

Hi All, I have two files which look as below File1 serial="1" name="abc" type="employee" field="IT" serial="2" name="cde" type="intern" field="Marketing" serial="3" name="pqr" type="contractor" field="IT" serial="4" name="xyz" type="employee" field="Sales" File2 serial="1"... (3 Replies)
Discussion started by: grajp002
3 Replies

8. UNIX for Dummies Questions & Answers

Using AWK: Extract data from multiple files and output to multiple new files

Hi, I'd like to process multiple files. For example: file1.txt file2.txt file3.txt Each file contains several lines of data. I want to extract a piece of data and output it to a new file. file1.txt ----> newfile1.txt file2.txt ----> newfile2.txt file3.txt ----> newfile3.txt Here is... (3 Replies)
Discussion started by: Liverpaul09
3 Replies

9. Shell Programming and Scripting

Help with create multiple directories under diff file systems

Hi, Need help ...I want to create multiple directories in different /file systems using for loop..eg.../ORCL_data01/oradata/orcl/ctl. ../ORCL_data01/oradata/orcl/data. ../ORCL_data01/oradata/orcl/redo. Script :- ========= for dir in `ls -d... (8 Replies)
Discussion started by: Linux6.5
8 Replies

10. Shell Programming and Scripting

Diff 3 files, but diff only their 2nd column

Guys i have 3 files, but i want to compare and diff only the 2nd column path=`/home/whois/doms` for i in `cat domain.tx` do whois $i| sed -n '/Registry Registrant ID:/,/Registrant Email:/p' > $path/$i.registrant whois $i| sed -n '/Registry Admin ID:/,/Admin Email:/p' > $path/$i.admin... (10 Replies)
Discussion started by: kenshinhimura
10 Replies
All times are GMT -4. The time now is 11:51 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy