Perl script to compare same filenames file line by line from 2 different path

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • optimusprime
    New Member
    • Apr 2013
    • 4

    Perl script to compare same filenames file line by line from 2 different path

    Hi All,

    am very new to perl, I have to shell script which compares 2 files line by line and get the difference (am using sdiff command)
    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
    #echo $filename
        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
    echo "File comparision done, please see resultfile"
    Basically, i have to write perl script
    a. takes 2 filepath as arguments while running the main script

    b. search the sub folder on each path and find same filename
    (note: sub folders in both path will have same name and as well same filename *.txt inside)

    3. If samefile found, then do
    a. sort both the file
    b. do line by line comparision
    c. generate the resultfile for each. Result file should have only :
    different line in one file or either way
    completely different line in both

    basically sdiff output structure,

    results/lines from both file side by side
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    What have you tried thus far to complete your homework assignment?

    What errors/warnings are you receiving?

    What part of the task do you not know how to do or is giving you trouble?

    How does your output differ from your expected results?

    Comment

    • optimusprime
      New Member
      • Apr 2013
      • 4

      #3
      Hi RonB,
      As I said above am new to perl. Am still learning very basic of it. It's not homework I have done this in unix fairly but needs to be done in perl

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        So, are you looking for someone to write the script for you, or are you asking for help in troubleshooting your script?

        Comment

        • RonB
          Recognized Expert Contributor
          • Jun 2009
          • 589

          #5
          You gave a brief outline of what the script is supposed to do, but didn't say what parts of the process you need help doing.

          For step "a", perl stores command line args in the @ARGV array. You can access that array directly, or for a more robust solution you can use the Getopt::Long module to parse the command line args.


          For step "b", you can use the File::Find module, which searches a directory tree much like the systems find command.


          For getting the diff, you could use the Text::Diff module.

          Comment

          • optimusprime
            New Member
            • Apr 2013
            • 4

            #6
            Thanks RonB for the information. unfortunately management doesn't want to have script which depends on the installing modules. since they expecting a single script which should run with minimum s/w requirement even without including/install modules on everytime/each servers.

            But anyway on knowledge wise your information is much helpful.

            Comment

            • RonB
              Recognized Expert Contributor
              • Jun 2009
              • 589

              #7
              The first 2 modules I mentioned are core modules, which means they come as part of the default install of perl. Neither of them are required for your task, but do make the script easier to write and more robust.

              The 3rd module is not core and can be omitted. The advantage of using it (and most modules) is that it avoids using system calls which makes the scripts more platform independent and avoids the "reinventin g the wheel" syndrom.

              If management doesn't allow the installation of modules, then there's little point in using perl, since the vast array of cpan modules is one of its greatest strengths. You might as well stick with only using shell scripts.

              Comment

              Working...