![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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-22-2008 at 09:37 PM. Reason: Redirect cmp to /dev/null; longhand version |
|||
| Google The UNIX and Linux Forums |