473,394 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Script to compare files recursively using sdiff

Hi All,

I have been surfing to get some idea on how to compare same files from two different paths.

one path will have oldfiles directory and another path will have newfiles directory. Each main directories will have sub-directories in them and

each sub-directories inturn will have *.txt files(simple plain text file having several lines in them).


Note : one advantage is that :

All the sub-directories name in both oldfiles and newfiles main directory will have same directory names.

All the text filenames(*.txt) in sub-directories in both main folders will be the same as well

Now,

a. script has to accept 2 paths ie, oldfiles and newfiles directory path as arguments

b. read first sub-folder name from oldfiles and search the same sub-folder name in newfiles path. If same sub folder found, then

c. check, if text files present, if present check if both filename matches, if so, then

d. Sort the files first, then do sdiff on those 2 files and store the results as a seperate file.

So, to give example how folder structure will look like :

Main folders:

oldfiles path : /tmp/oldfiles/

newfiles path : /tmp/newfiles/


Each main folders will have sub-folders :

oldfiles :
subdirA
subA.txt

subdirB
subB.txt

subdirC
subC.txt

newfiles :

subdirA
subA.txt

subdirB
subB.txt



Each sub-dirs will ahve *.txt having same filename in them.


From the above ex :

script should generate sdiff results in output folders as:

subdirA_subA_result.txt
subdirB_subB_result.txt

I hope have mentioned the what i tend to achieve clearly.

from the below script which i wrote it doesn;t checks for same sub-folders/files and even not generating seperate result files instead

it reads all the *.txt files and just produce one single result file.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash 
  2.  
  3. # cmp_dir - program to compare two directories 
  4.  
  5. # Check for required arguments 
  6. if [ $# -ne 2 ]; then 
  7.     echo "usage: $0 directory_1 directory_2" 1>&2 
  8.     exit 1 
  9. fi 
  10.  
  11. # Make sure both arguments are directories 
  12. if [ ! -d $1 ]; then 
  13.     echo "$1 is not a directory!" 1>&2 
  14.     exit 1 
  15. fi 
  16.  
  17. if [ ! -d $2 ]; then 
  18.     echo "$2 is not a directory!" 1>&2 
  19.     exit 1 
  20. fi 
  21.  
  22. # Process each file in directory_1, comparing it to directory_2 
  23. find $1/ -name '*.txt' -print | while read src 
  24. do 
  25. #for filename in $1/*.txt; do 
  26. #echo $filename 
  27.     fn=$(basename "$filename") 
  28.     if [ -f "$filename" ]; then 
  29.         #if [ ! -f "$2/$fn" ]; then 
  30.             #echo "$fn is missing from $2" 
  31.             #missing=$((missing + 1)) 
  32.         #fi 
  33.                 sort $filename 
  34.                 #echo $filename 
  35.                 sort $2/$fn 
  36.                 #echo $2/$fn 
  37.                 sdiff $filename $2/$fn | egrep '>|<|\|' > resultfile.txt 
  38.     fi 
  39. #done 
  40. done 
  41. echo "File comparision done, please see resultfile"
  42.  
Apr 5 '13 #1
1 4800
Luuk
1,047 Expert 1GB
It will only create one result because you did not add the path in line 37 like this:
Expand|Select|Wrap|Line Numbers
  1. sdiff $filename $2/$fn | egrep '>|<|\|' > $1/resultfile.txt 
NOTE: On a second run of this script, your script WILL find the result.txt of the previous run

NOTE2: The check in line 28
Expand|Select|Wrap|Line Numbers
  1. if [ -f "$filename" ]; then 
can be skipped if you change line 23 to:
Expand|Select|Wrap|Line Numbers
  1. find $1/ -type f -name '*.txt' -print | while read src 
Apr 5 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Raseliarison nirinA | last post by:
hi all, i found an unanswered question at http://www.faqts.com/knowledge_base/index.phtml/fid/538 with possible response below. i've tried to send it at faqt.python but can't figure out how to...
1
by: Mike James | last post by:
I admit it. I'm a newbie... I want to concatenate a series of text files by recursively going down a folder hierarchy. All the files have the same extension (*.txt) and I'm running WinXP Home...
1
by: Evgeny Gopengauz | last post by:
Hello all! My question is: how to compare two database structures if I only have their DDL files? As the result I would expect an sql-script upgrating one database structure to another. The...
1
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many...
2
seshu
by: seshu | last post by:
Hi Everybody I have a piece of code to compare two documents using vb.net but the problem here is it is comparing the whloe sentence but not word by word how to compare word by...
2
by: Zytan | last post by:
You can download them here: http://msdn2.microsoft.com/en-us/vstudio/aa718338.aspx This snippet seems wrong: Visual C# 2005 Code Snippets -filesystem -Search a Directory for Files Recursively ...
3
by: shona | last post by:
Hi, can any one told me how to compare files with same name but different extension.. for eg. if a.txt & a.doc then ans is same files.. Thanks
1
by: kumarboston | last post by:
Hi all, I am trying to copy couple of directories and their files to another place. Each directory have 25-30 files and numbered accordingly, the problem I am facing is, I have to copy files...
2
mageswar005
by: mageswar005 | last post by:
How can i open doc,xls,html,swf files directly using php (without asking open,save,close options)? Regards, M.Mageswaran
3
by: Susan StLouis | last post by:
I'm writing a program that can be used to compare files. The program features a select that contains a list of files. After selecting several of the files. a "Biggest" button can be pushed to find...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.