Compare two numbers which present in two different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two numbers which present in two different files
# 1  
Old 06-03-2013
Compare two numbers which present in two different files

Hi,

I need to compare the two floating or integer numbers which present in two different files,
Ex:
File 1:
Code:
col1 col2 col3
11 ssa 13.60
12 ssb 11.00
13 ssc 754.00

File 2:
Code:
col1 col2 col3
11 sa 12.75
12 sb 11.00
13 sc 763.00

here i have to compare 3 column if 1st column match, whether its equal or less than or greater than..
Please healp me...
# 2  
Old 06-03-2013
Code:
awk 'BEGIN {print "In file_2 value is:"} FNR==NR {a[$1]=$3;next} $1 in a {if ($3==a[$1]) t="equal"; else if ($3<a[$1]) t="less"; else {t="greater"};print $1,t}' file_1 file_2
In file_2 value is:
11 less
12 equal
13 greater

Edit: some more readable
Code:
awk '
	BEGIN {print "In file 2 value is:"}
	FNR==NR {
		a[$1]=$3;next} 
	$1 in a {
		if ($3==a[$1]) t="equal"
			else if ($3<a[$1]) t="less"
			else {t="greater"
			}
	print $1,t}
	' 1 2


Last edited by Jotne; 06-03-2013 at 05:24 AM..
# 3  
Old 06-03-2013
Thanks Jotne
# 4  
Old 06-13-2013
hi ,

in above output i'm only getting one column and its output,

but i need the entire line and last column should contain the output

could you help me to do that..

---------- Post updated at 05:34 PM ---------- Previous update was at 05:19 PM ----------

hi jonte i asked one doubt you clarify it, could you do it in using math operation can we print output of it.
with all input line and end of the line the output should come. Pls help me

---------- Post updated at 05:37 PM ---------- Previous update was at 05:34 PM ----------

I need to compare the two floating or integer numbers which present in two different files,
Ex:
File 1:
Code:
col1 col2 col3
11 ssa 13.60
12 ssb 11.00
13 ssc 754.00

File 2:
Code:
col1 col2 col3
11 sa 12.75
12 sb 11.00
13 sc 763.00

here i have to compare 3 column if 1st column match, whether its equal or less than or greater than.. using math operation i need the numeric output.
Please help me...

Last edited by Scott; 06-13-2013 at 09:31 AM.. Reason: Please use code tags for code and data
# 5  
Old 06-13-2013
For the above input files, what is the output format are you expecting ?

---------- Post updated at 07:35 AM ---------- Previous update was at 07:29 AM ----------

Are you looking for this ?
Code:
$ awk 'FNR==NR {a[$1]=$3;next} $1 in a {if ($3==a[$1]) t=0; else {t=$3-a[$1]};print $0,t}' 1.txt 2.txt 
11 sa 12.75 -0.85
12 sb 11.00 0
13 sc 763.00 9

# 6  
Old 06-13-2013
Hi Raja,
result is fine but its printing the result in next line like this
Code:
11 sa 12.75
 -0.85 12 sb 11.00 
  0 13 sc 763.00 
  9

for this what i needs to change
# 7  
Old 06-13-2013
Code:
$ awk 'FNR==NR {a[$1]=$3;next} {OFS=" "} $1 in a {if ($3==a[$1]) t=0; else {t=$3-a[$1]};print $0,t}' 1.txt 2.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to compare numbers in alphanumeric string

Hi, I will be having file names like below, 1420SP1.01804 1420SP1.01805D 1420SP1.01805 1420SP1.01806D 1420SP1.01806 1420SP1.01901D 1420SP1.01901 1420SP1.01902D 1420SP1.01902 1420SP1.01903D 1420SP1.01903 1420SP1.01904 1420SP1.01905 From this, I need to list file names which is... (3 Replies)
Discussion started by: Sumanthsv
3 Replies

2. Shell Programming and Scripting

Compare 2 files and extract the data which is present in other file - awk is not working

file2 content f1file2 content f1,1,2,3,4,5 f1,2,4,6,8,10 f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; !($1 in a)' file2 file1output f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; ($1 in a)' file2 file1output nothing... (4 Replies)
Discussion started by: gksenthilkumar
4 Replies

3. Shell Programming and Scripting

Extract numbers from output file and compare to value

Hi, I developed a script which finally originates a similar output: net0:up,Tx=475198bps,Rx=31009bps net1:up,Tx=39596bps,Rx=35678bps Of course the figures change and also the amount of interfaces (ex: could be more then net0 and net1). This is done automatically. The next step i'm... (4 Replies)
Discussion started by: nms
4 Replies

4. Shell Programming and Scripting

Unzip all the files with subdirectories present and append a part of string from the main .zip files

Hi frnds, My requirement is I have a zip file with name say eg: test_ABC_UH_ccde2a_awdeaea_20150422.zip within that there are subdirectories on each directory we again have .zip files and in that we have files like mama20150422.gz and so on. Iam in need of a bash script so that it unzips... (0 Replies)
Discussion started by: Ravi Kishore
0 Replies

5. Shell Programming and Scripting

Checking in a directory how many files are present and basing on that merge all the files

Hi, My requirement is,there is a directory location like: :camp/current/ In this location there can be different flat files that are generated in a single day with same header and the data will be different, differentiated by timestamp, so i need to verify how many files are generated... (10 Replies)
Discussion started by: srikanth_sagi
10 Replies

6. Shell Programming and Scripting

List all the files in the present path and Folders and subfolders files also

Hi, I need a script/command to list out all the files in current path and also the files in folder and subfolders. Ex: My files are like below $ ls -lrt total 8 -rw-r--r-- 1 abc users 419 May 25 10:27 abcd.xml drwxr-xr-x 3 abc users 4096 May 25 10:28 TEST $ Under TEST, there are... (2 Replies)
Discussion started by: divya bandipotu
2 Replies

7. Shell Programming and Scripting

compare 2 files and extract the data which is not present in other file with condition

I have 2 files whose data's are as follows : fileA 00 lieferungen 00 attractiop 01 done 02 forness 03 rasp 04 alwaysisng 04 funny 05 done1 fileB alwayssng dkhf fdgdfg dfgdg sdjkgkdfjg funny rasp (7 Replies)
Discussion started by: rajniman
7 Replies

8. Shell Programming and Scripting

compare the interval of 2 numbers of input2with interval of several numbers of input1

Help plz Does any one have any idea how to compare interval ranges of 2 files. finding 1-4 (1,2,3,4) of input2 in input1 of same key "a" values (5-10, 30-40, 45-60, 80-90, 100-120 ). Obviously 1-4 is not one of the range with in input1 a. so it should give out of range. finding 30-33(31,32,33)... (1 Reply)
Discussion started by: repinementer
1 Replies

9. Shell Programming and Scripting

how to compare big real numbers

Hi everyone, I need to compare 2 big Floating/Real numbers in a script. After the comparission it is showing worng values in my script. echo "Enter value1" read value1 echo "Enter value2" read value2 Result=`echo "if($value1 > $value2) 1" | bc` if ; then echo "$value1 is... (4 Replies)
Discussion started by: padarthy
4 Replies

10. Shell Programming and Scripting

compare decimal numbers

Hi anyone, i need to compare two decimal numbers i thought that it could be do it with if but... :( So, i'm writing in csh and i really apreciate if anyone can help me if ( $ppl_kn <= $ppl_wb ) then echo "############# KNdiscount model has the lowest perplexity" set... (5 Replies)
Discussion started by: tmxps
5 Replies
Login or Register to Ask a Question