Sponsored Content
Top Forums Shell Programming and Scripting Search and compare files from two paths Post 302787033 by DGPickett on Thursday 28th of March 2013 03:17:22 PM
Old 03-28-2013
Well, I do not have the test facility, so you need to check where the blank line is coming from. I assume diff -U0 will print only lines starting with +, -, |; so stick a tee after it, or "pg ;true |" and see what the first part delivers. Or put the word echo before sdiff to see what the whole command line is.

The idea is that the lists of files are identical, so any delete or add will be - or + and the checksum does not matter. If the files are identical, the checksum and size will be identical, and diff should toss them. If the files are different, sdiff should be able to show that.

The blank line from diff can be filtered if just a nuisance, using case (?*) to process only not empty lines and ignoring empty lines that fit (*).

The comm command is much stricter, as it is not designed for the eye but for bit by bit perfection. However, comm is just good for finding deleted and new; to get from both to changed still needs a comparison by cksum result compare or cmp. Comm demands sorted inputs, so if two files have different cksum, they would sort to non-adjacent places. So, a comm of file names is nice, followed by a cmp of files.
Code:
( export LC_ALL=C head1=... head2=... # LC_ALL controls sort order, some systems sort not binary by default
comm <(
  cd $head1
  find * -type f | sort
 ) <(
  cd $head2
  find * -type f | sort
 )| sed '\
  s/^\t\t/both /
  t
  s/^\t/add /
  t
  s/^/del /
 ' | while read stat fn
 do
  case $stat in
   (add)
    echo "New: $fn"
    cat $head2/$fn
    ;;
   (del)
    echo "Deleted: $fn"
    ;;
   (*)
    if [ "" != "$(cmp $head1/$fn $head2/$fn 2>&1)" ]
    then
     echo "Different: $fn"
     sdiff $head1/$fn $head2/$fn 2>&1
    fi
    ;;
   esac
  done
 )

The \t needs to be a real tab, above. The comm unifies the two file name lists and tells you if they are a only, b only or both by tabbing the lines as if to put them in three columns. You can remove columns in com using -1 (new and both), -2 (old and both), -23 (old only), -3 (old and new but no both), etc. It is robust set logic for shell scripting.

Last edited by DGPickett; 03-28-2013 at 04:23 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search & compare paragraphs between two files

Hello Guys, Greetings to All. I am stuck in my work here today while trying to comapre paragraphs between two files, I need your help on urgent basis, without your inputs I can not proceed. Kindly find some time to answer my question, I'll be grateful to you for ever. My detailed issue is as... (10 Replies)
Discussion started by: NARESH1302
10 Replies

2. UNIX Desktop Questions & Answers

how to display paths of files in a directory

hi guys does anyone know how to display the file paths of the files stored within a directory at the command terminal? e.g. if i have a directory called "home", how do i display the file paths of the files inside the directory? cheers (2 Replies)
Discussion started by: Villaman69
2 Replies

3. HP-UX

Search environment variables for paths

Hi, I am using the HP machine at the moment and by default I have been setup with the kron shell i.e. my home profile is .kshrc I would like to access a program anywhere on the system so I have added a path and created an environment variable like this: export myvarpath=/a/abc/def/ghij... (3 Replies)
Discussion started by: cyberfrog
3 Replies

4. Shell Programming and Scripting

compare two files and search keyword and print output

You have two files to compare by searching keyword from one file into another file File A 23 >pp_ANSWER 24 >aa hello 25 >jau head wear 66 >jss oops 872 >aqq olps ploww oww sss 722 >GG_KILLER ..... large files File B Beta done KILLER John Mayor calix meyers ... (5 Replies)
Discussion started by: cdfd123
5 Replies

5. Shell Programming and Scripting

Search compare and determine duplicate files

Hi May i ask if someone know a package that will search a directory recursively and compare determine duplicate files according to each filename, date modified or any attributes that will determine its duplicity If none where should i start or what are those command in shell scripting that... (11 Replies)
Discussion started by: jao_madn
11 Replies

6. UNIX for Dummies Questions & Answers

Determining file size for a list of files with paths

Hello, I have a flat file with a list of files with the path to the file and I am attempting to calculate the filesize for each one; however xargs isn't playing nicely and I am sure there is probably a better way of doing this. What I envisioned is this: cat filename|xargs -i ls -l {} |awk... (4 Replies)
Discussion started by: joe8mofo
4 Replies

7. Shell Programming and Scripting

Replace directory paths in multiple files at once

I need to update about 2400 files in a directory subtree, with a new directory path inside the files I need to change this occurence in all files: /d2/R12AB/VIS/apps/tech_st/10.1.2 with this: /u01/PROD/apps/apps_st/10.1.3 I know how to change single words using "find . -type f -print0 |... (6 Replies)
Discussion started by: wicus
6 Replies

8. UNIX Desktop Questions & Answers

Change name of files to their paths -- find loop

Dear All, I have many sub-folders but each of them have a file with same name but different data. I want to either move or copy them into a new folder but they need to have the path of where they are coming as part of their name... I have managed to find the files but dont know how to change... (2 Replies)
Discussion started by: A-V
2 Replies

9. UNIX for Dummies Questions & Answers

Search for string in a file then compare it with excel files entry

All, i have a file text.log: cover6 cover3 cover2 cover4 other file is abc.log as : 0 0 1 0 Then I have a excel file result.xls that contains: Name Path Pass cover2 cover3 cover6 cover4 (1 Reply)
Discussion started by: Anamika08
1 Replies

10. Shell Programming and Scripting

Script for linking files with paths in 2 text files

I have 2 txt files, 1.txt and 2.txt which contain the paths to files that need to be linked. Example 1.txt: /root/001/folder2/image4.nii.gz /root/002/folder2/image4.nii.gz Example 2.txt: /root/001/folder2/image5.nii.gz /root/002/folder2/image5.nii.gz Each line represents images from... (7 Replies)
Discussion started by: LeftoverStew
7 Replies
SDIFF(1)							     GNU Tools								  SDIFF(1)

NAME
sdiff - find differences between two files and merge interactively SYNOPSIS
sdiff -o outfile [options] from-file to-file DESCRIPTION
The sdiff command merges two files and interactively outputs the results to outfile. If from-file is a directory and to-file is not, sdiff compares the file in from-file whose file name is that of to-file, and vice versa. from-file and to-file may not both be directories. sdiff options begin with -, so normally from-file and to-file may not begin with -. However, -- as an argument by itself treats the remaining arguments as file names even if they begin with -. You may not use - as an input file. sdiff without -o (or --output) produces a side-by-side difference. This usage is obsolete; use diff --side-by-side instead. Options Below is a summary of all of the options that GNU sdiff accepts. Each option has two equivalent names, one of which is a single letter preceded by -, and the other of which is a long name preceded by --. Multiple single letter options (unless they take an argument) can be combined into a single command line argument. Long named options can be abbreviated to any unique prefix of their name. -a Treat all files as text and compare them line-by-line, even if they do not appear to be text. -b Ignore changes in amount of white space. -B Ignore changes that just insert or delete blank lines. -d Change the algorithm to perhaps find a smaller set of changes. This makes sdiff slower (sometimes much slower). -H Use heuristics to speed handling of large files that have numerous scattered small changes. --expand-tabs Expand tabs to spaces in the output, to preserve the alignment of tabs in the input files. -i Ignore changes in case; consider upper- and lower-case to be the same. -I regexp Ignore changes that just insert or delete lines that match regexp. --ignore-all-space Ignore white space when comparing lines. --ignore-blank-lines Ignore changes that just insert or delete blank lines. --ignore-case Ignore changes in case; consider upper- and lower-case to be the same. --ignore-matching-lines=regexp Ignore changes that just insert or delete lines that match regexp. --ignore-space-change Ignore changes in amount of white space. -l --left-column Print only the left column of two common lines. --minimal Change the algorithm to perhaps find a smaller set of changes. This makes sdiff slower (sometimes much slower). -o file --output=file Put merged output into file. This option is required for merging. -s --suppress-common-lines Do not print common lines. --speed-large-files Use heuristics to speed handling of large files that have numerous scattered small changes. -t Expand tabs to spaces in the output, to preserve the alignment of tabs in the input files. --text Treat all files as text and compare them line-by-line, even if they do not appear to be text. -v --version Output the version number of sdiff. -w columns --width=columns Use an output width of columns. Note that for historical reasons, this option is -W in diff, -w in sdiff. -W Ignore horizontal white space when comparing lines. Note that for historical reasons, this option is -w in diff, -W in sdiff. SEE ALSO
cmp(1), comm(1), diff(1), diff3(1). DIAGNOSTICS
An exit status of 0 means no differences were found, 1 means some differences were found, and 2 means trouble. GNU Tools 22sep1993 SDIFF(1)
All times are GMT -4. The time now is 09:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy