The UNIX and Linux Forums  

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 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 01:16 AM
Check File Exists and compare to previous day file script rbknisely Shell Programming and Scripting 3 02-07-2008 08:53 AM
compare files by lines and columns giviut Shell Programming and Scripting 4 01-17-2008 03:00 AM
Trying to compare lines in 2 files brdholman Shell Programming and Scripting 2 09-20-2007 04:46 AM
need help appending lines/combining lines within a file... mr_manny Shell Programming and Scripting 2 01-06-2006 03:45 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-22-2008
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
Reply With Quote
Forum Sponsor
  #2  
Old 04-22-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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
Reply With Quote
  #3  
Old 04-22-2008
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?
Reply With Quote
  #4  
Old 04-22-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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-22-2008 at 09:37 PM. Reason: Redirect cmp to /dev/null; longhand version
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0