Sponsored Content
Top Forums Shell Programming and Scripting Search and compare files from two paths Post 302787533 by Optimus81 on Friday 29th of March 2013 07:33:38 PM
Old 03-29-2013
Thanks DGPickett. Sure will tryout what you said.

I tried some very simple solution to compare *.txt files from 2 different directories. I was able to compare them and generate sdiff result.

Code:
#!/bin/bash
# cmp_dir - program to compare two directories
# Check for required arguments
if [ $# -ne 2 ]; then
    echo "usage: $0 directory_1 directory_2" 1>&2
    exit 1
fi
# Make sure both arguments are directories
if [ ! -d $1 ]; then
    echo "$1 is not a directory!" 1>&2
    exit 1
fi
if [ ! -d $2 ]; then
    echo "$2 is not a directory!" 1>&2
    exit 1
fi
# Process each file in directory_1, comparing it to directory_2
find $1/ -name '*.txt' -print | while read src
do
for filename in $1/*.txt; do
    fn=$(basename "$filename")
    if [ -f "$filename" ]; then
        #if [ ! -f "$2/$fn" ]; then
            #echo "$fn is missing from $2"
            #missing=$((missing + 1))
        #fi
                sort $filename
                #echo $filename
                sort $2/$fn
                #echo $2/$fn
                sdiff $filename $2/$fn | egrep '>|<|\|' > resultfile.txt
    fi
done
done

when i execute the above script ie,
./filecomp.sh oldfiles newfile

I get the resultfile.txt which will have the sdiff output(with grep). now my problem is not sure how create separate resultfile as it reads files.

I have 2 different folders :
a. oldfiles - conatains several files(*.txt)
b. newfile - contains several files(*.txt)

Files in the two folders will have the same filenames. ie,
oldfiles folder -
aa.txt
bb.txt

newfiles folder -
aa.txt
bb.txt

so, what am trying to do in above script, is to read file aa.txt from oldfiles folder and aa.txt from newfile folder then do sort/sdiff command and then put the result file in output folder with filename aa_result.txt

ie, output folder will contain results
aa_result.txt
bb_result.txt

this where am struck, how to get the separate resultfile on each inputfile.

Anyhelp will be greatful.
 

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
mktemp(1)							   User Commands							 mktemp(1)

NAME
mktemp - make temporary filename SYNOPSIS
mktemp [-dtqu] [-p directory] [template] DESCRIPTION
The mktemp utility makes a temporary filename. To do this, mktemp takes the specified filename template and overwrites a portion of it to create a unique filename. See OPERANDS. The template is passed to mkdtemp(3C) for directories or mkstemp(3C) for ordinary files. If mktemp can successfully generate a unique filename, the file (or directory) is created with file permissions such that it is only read- able and writable by its owner (unless the -u flag is given) and the filename is printed to standard output. mktemp allows shell scripts to safely use temporary files. Traditionally, many shell scripts take the name of the program with the PID as a suffix and used that as a temporary filename. This kind of naming scheme is predictable and the race condition it creates is easy for an attacker to win. A safer, though still inferior approach is to make a temporary directory using the same naming scheme. While this guaran- tees that a temporary file is not subverted, it still allows a simple denial of service attack. Use mktemp instead. OPTIONS
The following options are supported: -d Make a directory instead of a file. -p directory Use the specified directory as a prefix when generating the temporary filename. The directory is overridden by the user's TMPDIR environment variable if it is set. This option implies the -t flag. -q Fail silently if an error occurs. This is useful if a script does not want error output to go to standard error. -t Generate a path rooted in a temporary directory. This directory is chosen as follows: If the user's TMPDIR environment variable is set, the directory contained therein is used. Otherwise, if the -p flag was given the specified directory is used. If none of the above apply, /tmp is used. In this mode, the template (if specified) should be a directory component (as opposed to a full path) and thus should not contain any forward slashes. -u Operate in unsafe mode. The temp file is unlinked before mktemp exits. This is slightly better than mktemp(3C), but still introduces a race condition. Use of this option is discouraged. OPERANDS
The following operands are supported: template template can be any filename with one or more Xs appended to it, for example /tmp/tfile.XXXXXX. If template is not specified, a default of tmp.XXXXXX is used and the -t flag is implied. EXAMPLES
Example 1 Using mktemp The following example illustrates a simple use of mktemp in a sh(1) script. In this example, the script quits if it cannot get a safe tem- porary file. TMPFILE=`mktemp /tmp/example.XXXXXX` if [ -z "$TMPFILE" ]; then exit 1; fi echo "program output" >> $TMPFILE Example 2 Using mktemp to Support TMPDIR The following example uses mktemp to support for a user's TMPDIR environment variable: TMPFILE=`mktemp -t example.XXXXXX` if [ -z "$TMPFILE" ]; then exit 1; fi echo "program output" >> $TMPFILE Example 3 Using mktemp Without Specifying the Name of the Temporary File The following example uses mktemp without specifying the name of the temporary file. In this case the -t flag is implied. TMPFILE=`mktemp` if [ -z "$TMPFILE" ]; then exit 1; fi echo "program output" >> $TMPFILE Example 4 Using mktemp with a Default Temporary Directory Other than /tmp The following example creates the temporary file in /extra/tmp unless the user's TMPDIR environment variable specifies otherwise: TMPFILE=`mktemp -p /extra/tmp example.XXXXX` if [ -z "$TMPFILE" ]; then exit 1; fi echo "program output" >> $TMPFILE Example 5 Using mktemp to Remove a File The following example attempts to create two temporary files. If creation of the second temporary file fails, mktemp removes the first file before exiting: TMP1=`mktemp -t example.1.XXXXXX` if [ -z "$TMP1" ]; then exit 1; fi TMP2=`mktemp -t example.2.XXXXXX` if [ -z "$TMP2" ]; then rm -f $TMP1 exit 1 fi Example 6 Using mktemp The following example does not exit if mktemp is unable to create the file. That part of the script has been protected. TMPFILE=`mktemp -q -t example.XXXXXX` if [ ! -z "$TMPFILE" ] then # Safe to use $TMPFILE in this block echo data > $TMPFILE ... rm -f $TMPFILE fi ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of mktemp with the -t option: TMPDIR. EXIT STATUS
The following exit values are returned: 0 Successful completion. 1 An error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |Interface Stability |Committed | +-----------------------------+-----------------------------+ SEE ALSO
sh(1), mkdtemp(3C), mkstemp(3C), attributes(5), environ(5) NOTES
The mktemp utility appeared in OpenBSD 2.1. The Solaris implementation uses only as many `Xs' as are significant for mktemp(3C) and mkstemp(3C). SunOS 5.11 10 Jan 2008 mktemp(1)
All times are GMT -4. The time now is 07:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy