LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 06-06-2007, 01:09 PM   #1
mou5e
LQ Newbie
 
Registered: Feb 2007
Distribution: Debian Lenny
Posts: 20

Rep: Reputation: 0
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.
 
Old 06-06-2007, 01:52 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
Old 06-06-2007, 02:02 PM   #3
bsdunix
Senior Member
 
Registered: May 2006
Distribution: Caldera, CTOS, Debian, FreeBSD, Mac OS X, Mandrake, Minix, OpenBSD, Slackware, SuSE
Posts: 1,757

Rep: Reputation: 79
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
 
Old 06-06-2007, 02:12 PM   #4
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
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

Last edited by Centinul; 06-06-2007 at 02:36 PM.
 
Old 06-06-2007, 02:19 PM   #5
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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"
 
Old 06-06-2007, 02:26 PM   #6
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
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!
 
Old 06-06-2007, 02:30 PM   #7
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Well, it isn't working anyway (several other minor errors). I'm fixing it and will post results shortly.
 
Old 06-06-2007, 02:36 PM   #8
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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

Last edited by forrestt; 06-06-2007 at 02:38 PM.
 
Old 06-06-2007, 02:36 PM   #9
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
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
 
Old 06-06-2007, 02:40 PM   #10
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
I just have the advantage of being able to debug it
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
insert lines in a function using shell script shyamdey Programming 1 08-30-2006 08:48 AM
shell script for expanding lines in a file b123coder Programming 1 05-25-2006 05:51 AM
How can I use a shell script to add and replace lines in a file? abefroman Programming 10 12-27-2005 06:05 PM
shell script to copy lines from a file Warmduvet Programming 2 09-14-2004 10:25 PM


All times are GMT -5. The time now is 10:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration