Simple awk command to compare two files and print first difference
Hello,
I have two text files, each with a single column,
file 1:
file 2:
I am trying to identify the value in red above which is the first value that doesn't match the second file. I need to print that value and exit.
At first I tried diff,
diff file1 file2 | head -n 2
This gives what I want, but there are multiple lines of output and so it was more steps to get the value into a bash variable, which is what I need.
I then tried awk,
awk ' NR==FNR { a[NR]=$0; next } !($0 in a){ print $1; exit } ' file2 file1
Note that the order of input files is reversed because I want the first line of file1 that does not match file2. This just prints the first line of file1. Even if it did work, I think that this just tells me that the value is, or is not, in the file, not if the lines match.
I am sure I could do a loop with read, but that would be slow.
This seems like a very simple task. Are there any suggestions?
LMHmedchem
Last edited by Scrutinizer; 04-26-2017 at 07:17 PM..
but that doesn't do anything at all. Am I right that evaluating !($0 in a) looks for $0 anywhere in a[]? I am checking that the files match, so it matters that the value appears on the same line in both files, not that it appears anywhere.
@OP, you second suggestion seems to work alright but you forgot the second quote:
However, it would read the whole of file1 first and put it in memory..
Another approach you could try:
This User Gave Thanks to Scrutinizer For This Post:
By slower I mean 0m0.391s as opposed to 0m0.156s with the first method. Not enough difference to bother with but I guess you need some reason to pick a method.
The method suggested by rdrtx1 also worked but again was a bit slower,
Hi
PRIM_SEQ=`some sql code`
and output of PRIM_SEQ is like below
120
130
STB_SEQ=`some sql code`
and output of STB_SEQ is like below
115
110
i need to compare this two variables output ( decimal numbers)
1) What I want to do is to compare every number in the PRIM_SEQ with... (8 Replies)
Hi,
I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this
File1
1 rs1 abc
3 rs4 xyz
1 rs3 stu
File2
1 kkk rs1 AA 10
1 aaa rs2 DD 20
1 ccc ... (2 Replies)
Hello. I have two files. FILE1 was extracted from FILE2 and modified thanks to help from this post. Now I need to replace the extracted, modified lines into the original file (FILE2) to produce the FILE3.
FILE1
1466 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04
1467 55.27433... (1 Reply)
It seems like a common task, but I haven't been able to find the solution.
vitallog.txt
1310,John,Hancock
13211,Steven,Mills
122,Jane,Doe
138,Thoms,Doe
1500,Micheal,May
vitalinfo.txt
12122,Jane,Thomas
122,Janes,Does
123,Paul,Kite
**OUTPUT**
vitalfiltered.txt
12122,Jane,Thomas... (2 Replies)
Hello,
I am strugling from quite a some time to compare flat files with over 1 million records could anyone please help me.
I want to compare two pipe delimited flat files, file1 with file2 and output the unmatched rows from file2 in file3
Sample File1:
... (9 Replies)
I have two files like this:
#FILE 1
ABCD 4322 26485
JMTJ 5311 97248
XMPJ 4321 58978
#FILE 2
ABCD 4321 26485
JMTJ 5311 97248
XMPJ 4321 68978
What to do: Compare the two files and find those lines that doesn't match. And have a new file like this:
#FILE 3
"from file 1"
ABCD 4322 26485... (11 Replies)
hey guys, I have two files both with two columns, I have already created an
awk code to ignore certain lines (e.g lines that start with 963) as they wou
ld begin with a certain string, however, the rest I have added together and
calculated the average.
At the moment the code also displays... (3 Replies)