The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk to compare lines of two files and print output on screen chlfc Shell Programming and Scripting 3 03-24-2008 04:16 AM
Check File Exists and compare to previous day file script rbknisely Shell Programming and Scripting 3 02-07-2008 11:53 AM
compare files by lines and columns giviut Shell Programming and Scripting 4 01-17-2008 06:00 AM
Trying to compare lines in 2 files brdholman Shell Programming and Scripting 2 09-20-2007 07:46 AM
need help appending lines/combining lines within a file... mr_manny Shell Programming and Scripting 2 01-06-2006 06:45 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-22-2008
sabertooth2000 sabertooth2000 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 2
Help! How to compare two lines in a file

Hello,

I am newcomer and sorry for this simple question. I want to how to compare two lines in a file? For example, to compare the first line and the second line of a file to know if they are same?

Thanks in advance!
Leon
  #2 (permalink)  
Old 04-22-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
That's a rather unusual way to arrange things, often you want to see if all lines in one file are present in another, or some such.

Anyway, uniq can tell you whether two adjacent lines are identical. To just extract the first two lines from a file, use head -n 2

Code:
head -n 2 file | uniq -c
If this prints a 2 and the line then the two lines are identical, otherwise it will print two lines with an occurrence count (of course, one each).

This is pretty bare-bones but perhaps you can add some details about what sort of application you have for this problem. Maybe you also want to look at diff and cmp
  #3 (permalink)  
Old 04-22-2008
sabertooth2000 sabertooth2000 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 2
Thanks for your reply, but I still have some doubts since I want to take use of the comparation result to do further judgement.
Like this,

if (identical);then
do something
else
do something else
fi

Do you have some advises?
  #4 (permalink)  
Old 04-23-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Again, the unconventional arrangement to compare the first two lines of the same file makes this a bit weird. The following uses some constructs which are very specific to the shell.

The backticks allow you to use the output from one command as the command-line arguments of another command. Those are ASCII 96 accents (grave ones), not regular apostrophes.

So the argument to "set" is two dashes and the output from the comparison. This puts the output from head | uniq in $1, $2. $3 etc, split on whitespace just like if you had typed it as arguments to a command.

The case statement examines the first output token from set (and thus from head | uniq) -- if it is 2, it means there were two identical lines, otherwise they were different.

Code:
set -- `head -2 file | uniq -c`
case $1 in
  2) do something ;;
  *) do something else;;
esac
Depending on which shell you use, there may be other constructions you can use, but this I hope will work in any Bourne-compatible shell.

Modern shells have a construct which allows you to express this more naturally:

Code:
if cmp <(head -n 1 file) <(sed -n 2p file) >/dev/null; then
  do something
else
  do something else
fi
Sort of like a pipeline, this compares the output from two commands (behind the scenes, using something like temporary files) with cmp, which normally compares files.

cmp will print a message if the files are different; we discard that, by redirecting any output to /dev/null

sed -n 2p prints the second line from the input.

Of course, you could write all that in longhand, using temporary variables, but it might not work in all shells if there are e.g. backslashes in the input file:

Code:
tmp1="`head -n 1 file`"
tmp2="`sed -n 2p file`"
case $tmp1 in
  "$tmp2") do something ;;
  *) do something else ;;
esac
Some would prefer to use if ["$tmp1" = "$tmp2" ] but then they would probably fail to quote the arguments properly. case is somewhat more robust against quoting issues than if [ ... ]

Last edited by era; 04-23-2008 at 12:37 AM.. Reason: Redirect cmp to /dev/null; longhand version
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0