Visit Our UNIX and Linux User Community

how to compare file line by line with awk

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to compare file line by line with awk
# 1  
Old 07-17-2012
Question how to compare file line by line with awk

im a newbee to unix.

I have a requirement to compare two files with awk.


here i want to compare each line in file1 with corresponding line in file2 and prinf the line with difference. ie to check

required result as shown below
a=a (dont print since records are same)
b=b (dont print since records are same)
c=d (print "diff" or any text since records are different)
d=e (print "diff" or any text since records are different)
e=null (print "diff" or any text since records are different)

I have gone through many programs in forum and i could get only scripts which lookup each value in file1 exists in file2, which is not my requirement.

Appreciate your help in advance

Kiran Smilie
# 2  
Old 07-17-2012
Try this awk 1 liner...
awk '{if(FNR==NR){f1[NR]=$0;m++}else{f2[FNR] = $0; n++};t=m>n?m:n}END{for(i=1;i<=t;i++) printf("%s",f1[i]!=f2[i]?i":  diff\n" : "")}' file1 file2

# 3  
Old 07-17-2012
Originally Posted by kiranps
I have a requirement to compare two files with awk.

Your sample data files have an unequal number of lines but you did not state how to handle that. Is that a typo? If not, should comparison stop when the shorter file's end is reached? Or should the shorter file be treated as if it had enough empty lines to match the excess lines of the longer file? Or perhaps that should just be considered a match?

The following code uses paste, which treats a shorter file as if it had empty lines after end-of-file; it assumes that there are no tab characters in your lines (if there are, a different delimiter is required); and it prints out a line number when there's a difference.

paste f1 f2 | awk -F'\t' '$1 != $2 {print NR}'


Last edited by alister; 07-17-2012 at 04:45 PM..
# 4  
Old 07-18-2012
Thanks a lot alister

It is not typo. It should consider this as empty line and continue to compare.
I am generating files daily and i am comparing files generated today and on any previous days.
The data in the files are generated in sorted order, so i need to compare files to find the missing/changed line/new entry.

@shamrock: thank you for your quick response. i will test this out today.

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Honey, I broke awk! (duplicate line removal in 30M line 3.7GB csv file)

I have a script that builds a database ~30 million lines, ~3.7 GB .cvs file. After multiple optimzations It takes about 62 min to bring in and parse all the files and used to take 10 min to remove duplicates until I was requested to add another column. I am using the highly optimized awk code: awk... (34 Replies)
Discussion started by: Michael Stora
34 Replies

4. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

5. Shell Programming and Scripting

awk concatenate every line of a file in a single line

I have several hundreds of tiny files which need to be concatenated into one single line and all those in a single file. Some files have several blank lines. Tried to use this script but failed on it. awk 'END { print r } r && !/^/ { print FILENAME, r; r = "" }{ r = r ? r $0 : $0 }' *.txt... (8 Replies)
Discussion started by: sdf
8 Replies

6. Shell Programming and Scripting

reading a file inside awk and processing line by line

Hi Sorry to multipost. I am opening the new thread because the earlier threads head was misleading to my current doubt. and i am stuck. list=`cat /u/Test/programs`; psg "ServTest" | awk -v listawk=$list '{ cmd_name=($5 ~ /^/)? $9:$8 for(pgmname in listawk) ... (6 Replies)
Discussion started by: Anteus
6 Replies

7. Shell Programming and Scripting

Awk not working due to missing new line character at last line of file

Hi, My awk program is failing. I figured out using command od -c filename that the last line of the file doesnt end with a new line character. Mine is an automated process because of this data is missing. How do i handle this? I want to append new line character at the end of last... (2 Replies)
Discussion started by: pinnacle
2 Replies

8. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

9. Shell Programming and Scripting

awk script to compare and insert a line

Hi I want to compare a string at fixed position 10-20 for all the lines starting with 6. if they dont match it should take a copy of a line starting with 1 and insert it before the line starting with 6. How do i this? Please help Eg 1 test 1 765533 7643743 6 yes 3 5363653 373833 7... (9 Replies)
Discussion started by: appsguy616
9 Replies

10. Shell Programming and Scripting

compare data line by line from a file

Hi there How can I compare data line by line from a file? I need to compare the second value with the fourth to know if they are different. If those values are different, I require to send my first value to the output until the complete file has been read. This is my file: 0 FALSE... (1 Reply)
Discussion started by: loperam
1 Replies

Featured Tech Videos