LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script for comparing certain lines in two files (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-for-comparing-certain-lines-in-two-files-559653/)

mou5e 06-06-2007 12:09 PM

Shell script for comparing certain lines in two files
 
I spent 3 days in searching and trying different scripts and packages but i have no success. All I want to do is compare first and last 2 lines in 2 files.


10x to all.

forrestt 06-06-2007 12:52 PM

Code:

#!/bin/sh

# change this to the correct 2 files.
FILE_ONE=/path/to/file1
FILE_TWO=/path/to/file2

FILE_ONE_TAIL=`tail -2 $FILE_ONE`
FILE_TWO_TAIL=`tail -2 $FILE_TWO`

if [ "$FILE_ONE_TAIL" = "$FILE_TWO_TAIL" ]; then
    echo The last two lines of the files match.
else
    echo The last two lines of the files DO NOT match.
fi


bsdunix 06-06-2007 01:02 PM

forrestt already posted, but here's another suggestion:

Code:

#!/bin/sh
echo "First two lines of files:" > temp
head -n 2 file1 file2 >> temp
echo "Last two lines of files:" >> temp
tail -n 2 file1 file2 >> temp
cat temp


Centinul 06-06-2007 01:12 PM

Here is a slightly more robust version. This is not guaranteed by any stretch of the imagination because this isn't tested and I'm not a BASH expert.

Code:

#!/bin/sh

## Usage scipt_name.sh <file 1> <file 2>

## file names
FILE_1="$1"
FILE_2="$2"

## if both files exist and are readable
if [ (-r $FILE_1) -a (-r $FILE_2) ]; then

        ## Grab last two lines of each file
        FILE_1_END="`tail -2 $FILE_1`"
        FILE_2_END="`tail -2 $FILE_2`"

        ## Compare last two lines
        if [ "$FILE_1_END" == "$FILE_2_END" ]; then
            echo "The last two lines of the files match.\n"
        else
            echo "The last two lines of the files DO NOT match.\n"
            echo "The last two lines of the first file are:\n $FILE_1_END\n"
            echo "The last two lines of the second file are:\n $FILE_2_END\n"
        fi


        ## Grab last two lines of each file
        FILE_1_START="`head -n 2 $FILE_1`"
        FILE_2_START="`head -n 2 $FILE_2`"

        ## Compare first two lines
        if [ "$FILE_1_START" == "$FILE_2_START" ]; then
            echo "The last two lines of the files match.\n"
        else
            echo "The first two lines of the files DO NOT match.\n"
            echo "The first two lines of the first file are:\n $FILE_1_START\n"
            echo "The first two lines of the second file are:\n $FILE_2_START\n"
        fi
else
        echo "Either the files don't exist or you don't have permission to read them.\n"
fi


forrestt 06-06-2007 01:19 PM

You can't have spaces in a variable assignment in bash. But I agree, your program is more robust. Change the lines:
Code:

FILE_1 = "$1"
FILE_2 = "$2"

to
Code:

FILE_1="$1"
FILE_2="$2"


Centinul 06-06-2007 01:26 PM

Quote:

Originally Posted by forrestt
You can't have spaces in a variable assignment in bash. But I agree, your program is more robust. Change the lines:
Code:

FILE_1 = "$1"
FILE_2 = "$2"

to
Code:

FILE_1="$1"
FILE_2="$2"


Thanks for the input. I've made the requested changes. I can't test because I'm on a *gulp* Windows 2000 PC *gulp* at work.

Thanks again!

forrestt 06-06-2007 01:30 PM

Well, it isn't working anyway (several other minor errors). I'm fixing it and will post results shortly.

forrestt 06-06-2007 01:36 PM

Code:

#!/bin/sh

## Usage scipt_name.sh <file 1> <file 2>

## file names
FILE_1="$1"
FILE_2="$2"

echo $FILE_1 $FILE_2
## if both files exist and are readable
if [ -r "$FILE_1" ]; then
  if [ -r "$FILE_2" ]; then

        ## Grab last two lines of each file
        FILE_1_END=`tail -2 $FILE_1`
        FILE_2_END=`tail -2 $FILE_2`

        ## Compare last two lines
        if [ "$FILE_1_END" = "$FILE_2_END" ]; then
            echo "The last two lines of the files match.\n"
        else
            echo "The last two lines of the files DO NOT match.\n"
            echo "The last two lines of the first file are:\n$FILE_1_END\n"
            echo "The last two lines of the second file are:\n$FILE_2_END\n"
        fi


        ## Grab first two lines of each file
        FILE_1_START=`head -2 $FILE_1`
        FILE_2_START=`head -2 $FILE_2`

        ## Compare first two lines
        if [ "$FILE_1_START" = "$FILE_2_START" ]; then
            echo "The first two lines of the files match.\n"
        else
            echo "The first two lines of the files DO NOT match.\n"
            echo "The first two lines of the first file are:\n$FILE_1_START\n"
            echo "The first two lines of the second file are:\n$FILE_2_START\n"
        fi
    else
        echo "Cannot read $FILE_2"
    fi
else
    echo "Cannot read $FILE_1"
fi


Centinul 06-06-2007 01:36 PM

Quote:

Originally Posted by forrestt
Well, it isn't working anyway (several other minor errors). I'm fixing it and will post results shortly.

Thanks for taking a look at it. I've made a couple modifications since your post to fix the extremely noticable errors. That's what I get for writing it fast.

Centinul

forrestt 06-06-2007 01:40 PM

I just have the advantage of being able to debug it :)


All times are GMT -5. The time now is 12:11 PM.