![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 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 |
|
||||
|
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 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 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 Last edited by era; 04-23-2008 at 12:37 AM.. Reason: Redirect cmp to /dev/null; longhand version |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|