script to compare first column of two files and find difference


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to compare first column of two files and find difference
# 1  
Old 10-22-2008
script to compare first column of two files and find difference

Hi,

I want to write a script which will compare the 1st column of both the files and will give the difference.
e.g:-
my 1st file contains:

89 /usr
52 /usr/local
36 /tmp
92 /opt
96 /home
27 /etc/opt/EMCom
1 /SF_Appl_01
7 /SF_Data_01
1 /tmpiq


my 2nd file contains:
79 /usr
22 /usr/local
26 /tmp
92 /opt
86 /home
17 /etc/opt/EMCom
1 /SF_Appl_01
7 /SF_Data_01
8 /tmpiq
87 /HS_Appl_01
86 /HS_Data_01


Now I want to find the differnce of 1st column of both the files and notify if differnce is more than 10. Please help me on this.
# 2  
Old 10-22-2008
Code:
# awk '{if (FNR != NR) { if (a[$2]) { x = $1 - a[$2]; if ((x >= 0 ? x : -x) > 10) { print "oops at line", FNR, ":", $0 } } } else { a[$2] = $1; } }' file1 file2

You must change file1 and file2 accordingly with your file names.
# 3  
Old 10-22-2008
Code:
nawk '{
if(NR==FNR)
	arr[$2]=$1
else
{
	gap=$1-arr[$2]
	if(gap>10 || gap<-10)
		print $2" "$1"  "arr[$2]"  "gap
}
}' file1 file2

# 4  
Old 10-22-2008
thanks it worked

Thanks a lot. It is working fine.




Quote:
Originally Posted by redoubtable
Code:
# awk '{if (FNR != NR) { if (a[$2]) { x = $1 - a[$2]; if ((x >= 0 ? x : -x) > 10) { print "oops at line", FNR, ":", $0 } } } else { a[$2] = $1; } }' file1 file2

You must change file1 and file2 accordingly with your file names.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

I want to find the difference between two files, only for the header (column names)

Hi All, I want to find the difference between two files, by checking only the headers (column names) and report if any new column is added in the latest file. For Ex: If the file "declartion.txt has these columns url;image;id;showcase_id;showcase_name and the actual file "feed.txt" has... (34 Replies)
Discussion started by: Praveen Pandit
34 Replies

3. Shell Programming and Scripting

How to get difference of the same column between two files when other column matches?

File 1: 20130416,235800,10.78.25.104,BR2-loc,60.0,1624,50.0,0,50.0,0 20130416,235800,10.78.25.104,BR1-LOC,70.0,10,50.0,0,70.0,0 20130416,235800,10.78.25.104,Hub_None,60.0,15,60.0,0,50.0,0 File 2: 20130417,000200,10.78.25.104,BR2-loc,60.0,1626,50.0,0,50.0,0... (3 Replies)
Discussion started by: Lakshmikumari
3 Replies

4. Shell Programming and Scripting

Script to compare 2 files and prints difference as output sidebyside

Hi All, Am trying script to compare 2 files and print the difference found from old file to new file on line by line basis on side by side display. Basically line by line comparision and files may contain blank line as well I know we have compare/diff commands but i don't how to make... (10 Replies)
Discussion started by: Optimus81
10 Replies

5. Homework & Coursework Questions

Script to find difference between 2 files by column

Hi , i am newbie to shell scripting and am trying to do the below job, A shell script to be run with a command like sh Compare.ksh file1.txt file2.txt 1 2 > file3.txt 1 2-are the key columns Consider the delimiter would be Tab or comma File 1: SK TEST NAME MATHS PHYSICS 21 1 AAA... (1 Reply)
Discussion started by: shakthi666
1 Replies

6. Shell Programming and Scripting

Script to find difference between 2 files by column

Hi , i am newbie to shell scripting and am trying to do the below job, A shell script to be run with a command like sh Compare.ksh file1.txt file2.txt 1 2 > file3.txt 1 2-are the key columns Consider the delimiter would be Tab or comma File 1: SK TEST NAME MATHS PHYSICS 21 1... (1 Reply)
Discussion started by: shakthi666
1 Replies

7. Shell Programming and Scripting

script to find whether the difference between two files in only additions but not modifications

i basically have 2 files and sdiff of the files is given below sdiff file1 file 2 control_file < path INDEX < size 613 < mode 0444 ... (1 Reply)
Discussion started by: under_cons
1 Replies

8. Shell Programming and Scripting

PHP Compare 2 Arrays find difference & case insensitive

Hi, I need an elegant solotion in php. I need to compare 2 arrays (array1 & array2), to find all instances of array 2 which is not in array1. I don't want to see any instances of array1 wich is not in array2 (here "the") Array1: This, is, the, data, of, array1 Array2: this, is, data, Of,... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. Shell Programming and Scripting

find difference in file column...

Hi All, i have a file that is tab delimited. i need help to find the rows which are having same price based on the site code but some times, there are difference so i need to find only the records which are different in all site code. Dept Sec Barcode 10001 10002 10003 10004... (1 Reply)
Discussion started by: malcomex999
1 Replies

10. Shell Programming and Scripting

to compare two files and to print the difference

suppose one file P1168S P2150L P85L Q597R R1097C Another file P2150L P85L Q597R R1097C R1379C R1587K Then output shud be R1379C R1587K thanks (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question