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?
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
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
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.
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:
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:
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 01:37 AM..
Reason: Redirect cmp to /dev/null; longhand version
HI,
I have 2 text files. file1 and file2.
file1.txt (There are no duplicates in this file)
1234
3232
4343
3435
6564
6767
1213
file2.txt
1234,wq,wewe,qwqw
1234,as,dfdf,dfdf
4343,asas,sdds,dsds
6767,asas,fdfd,fdffd
I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Need help to create the script that does the following : -
1. Compare the current line "column B and C" with next line "column B and C"
2. If they are the same, print output to a file
Input file
2014-08-25 04:45:56.673|T1|JO|Begin|10
2014-08-25 04:55:56.673|T1|JO|Begin|11
2014-08-25... (8 Replies)
Hi Folks,
I need to compare the cron's timings from a text file. Need to display how much time does it took for that job.
For example i have the below txt file, I have cron1 started at 05:23:15 and completed at 05:25:57, now i need to find how much time did it took to complete corn1 job for... (8 Replies)
Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help
File 1
123 232 W343... (6 Replies)
Hello,
I have this file that sometime contains 0 lines and sometimes 1 or more.
It's supposed to then put the result (could be 0 or 1 or 2 or more) into a variable.
Then it's supposed to echo using an if else statement depending on the value of the variable.
flagvar='wc -l $tempfile |... (1 Reply)
could someone write a short script which is able to take a text file input and compare lines 1 and 2, 3 and 4, 5 and 6 ... ... until the end of the file. And to prompt if the lines are not equal.
Thank you! (10 Replies)
Hi ,
I have a CSV file ( file.csv) with some data as
below:
A,1,abc,x,y,z,,xyz,20100101,99991231
A,1,abc,x,y,z,234,xyz,20100101,99991231
I have to delete the duplicate line based on
unique identifiers which are values in the
fields- 2,3,4,8.These coulmns in both the rows
have same... (6 Replies)
Hey guys I am having a problem with being able to find unused profiles in a configuration check script I am trying to create for accountability purposes for managing a large number of systems. What I am trying to do is run a script that will look at the raw config data in a file and pull all the... (3 Replies)
Hi everybody,
I have a file that looks like:
A B C D -1 0
E F G H -2 0
I J K L +1
M N O P -6
I would like to compare $5 of every line. If both values are negative, I calculate a mean value and write the first line and delete the second one. If the two $5 values are different only... (6 Replies)
Hi, I want to compare the last two lines of a files, specifically characters 32 - 50 in both lines and generate an exit code if the range of characters do not match. Please advise. Thanks in advance! (2 Replies)