To find and compare the data | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

To find and compare the data

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-21-2013
Shenbaga.d Shenbaga.d is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 12 August 2014, 9:19 AM EDT
Posts: 41
Thanks: 2
Thanked 0 Times in 0 Posts
To find and compare the data

hi ,

from the awk command i'm getting the output where the 2nd column of both are match in row wise,
if it present in that same column some where else, I cannot get the correct output.
awk 'FNR==NR {a[$1]=$5;next} $1 in a {if ($5==a[$1]) t=0; else {t=$5-a[$1]};print $0,t}' file.tsv file2.tsv

please check the thread http://www.unix.com/shell-programmin...ent-files.html

---------- Post updated at 07:50 PM ---------- Previous update was at 07:40 PM ----------


Code:
col1 col2 col3
11 15465 13.60
12 24656 11.00
13 245896 754.00


Code:
col1 col2 col3
11 245896 754.00
12 15465 13.60
13 24656 11.00

this will give the wrong output
Sponsored Links
    #2  
Old 06-21-2013
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 21 October 2014, 10:08 AM EDT
Location: Out running a Marathon.
Posts: 2,332
Thanks: 73
Thanked 155 Times in 144 Posts
I recognize that for some people here, english is not the primary language. I do not think you explained this well.

1. show input file

2. show your command

3. show what you are getting

4. show what you want to get for output.
Sponsored Links
    #3  
Old 06-23-2013
Shenbaga.d Shenbaga.d is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 12 August 2014, 9:19 AM EDT
Posts: 41
Thanks: 2
Thanked 0 Times in 0 Posts
Hi,

here i have given the input files and output files, and awk command
awk 'FNR==NR {a[$1]=$4;next} $1 in a {if ($4==a[$1]) t=0; else {t=$4-a[$1]};print $0,t}' file1.tsv file2.tsv

i have to find the COL1 number in file2 and get the difference of the matched number's COL3 Value that is i have to find the 14150524 in file2 and take a difference of 39.88 - 37.88 and the result i want is like 10527 14150524 Sa 39.88 -2




File 1

Code:
SR.no	COL1	COL2	COL3
10527	14150524	Sa	39.88
10528	12311440	Sa	0
10529	12441731	Sa	111.66
10530	15120599	Sa	69.97
10531	21635123	Sa	149.99
10532	9854892	Sa	27.53
10533	14526541	Sa	67.06
10534	10993779	Sa	99
10535	15684120	Sa	112.99
10536	6051457	Sa	249
10537	10983989	Sa	149.97
10538	8222030	Sa	59
10539	10910428	Sa	237.2
10540	8477371	Sa	126.44

File2

Code:
SR.no	COL1	COL2	COL3
10536	6051457	Sb	249
10537	10983989	Sb	149.97
10538	8222030	Sb	59
10530	15120599	Sb	69.97
10531	21635123	Sb	149.99
10532	8477371	Sb	126.44
10533	14526541	Sb	67.09
10534	10993779	Sb	99
10535	15684120	Sb	112.99
10527	14150524	Sb	37.88
10528	12311440	Sb	0
10529	12441731	Sb	111.66
10539	10910428	Sb	237.2
10540	9854892	Sb	27.54

Result

Code:
SR.no	COL1	COL2	COL3	COL4
10527	14150524	Sa	39.88	0
10528	12311440	Sa	0	0
10529	12441731	Sa	111.66	0
10530	15120599	Sa	69.97	0
10531	21635123	Sa	149.99	0
10532	9854892	Sa	27.53	-98.91
10533	14526541	Sa	67.06	-0.03
10534	10993779	Sa	99	0
10535	15684120	Sa	112.99	0
10536	6051457	Sa	249	0
10537	10983989	Sa	149.97	0
10538	8222030	Sa	59	0
10539	10910428	Sa	237.2	0
10540	8477371	Sa	126.44	98.9

    #4  
Old 06-23-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 1 October 2014, 9:32 AM EDT
Location: France
Posts: 2,930
Thanks: 81
Thanked 624 Times in 596 Posts
Quote:
result i want is like 10527 14150524 Sa 39.88 -2
Assuming you want result -2 you may want t=a[$1]-$4 (scanning file2 at first)


Code:
awk 'FNR==NR{a[$1]=$4;next}$1 in a{t=a[$1]-$4;print $0,(FNR==1)?"":t}' file2 file1

---------- Post updated at 12:08 PM ---------- Previous update was at 11:53 AM ----------

with a bit of formating

Code:
awk 'FNR==NR{a[$1]=$4;next}$1 in a{t=a[$1]-$4;printf"%-8s %8s %-4s %8s %8s\n",$1,$2,$3,$4,(FNR==1)?"COL4":t}' file2 file1


Code:
$ awk 'FNR==NR{a[$1]=$4;next}$1 in a{t=a[$1]-$4;printf"%-8s %8s %-4s %8s %8s\n",$1,$2,$3,$4,(FNR==1)?"COL4":t}' f2 f1
SR.no        COL1 COL2     COL3     COL4
10527    14150524 Sa      39.88       -2
10528    12311440 Sa          0        0
10529    12441731 Sa     111.66        0
10530    15120599 Sa      69.97        0
10531    21635123 Sa     149.99        0
10532     9854892 Sa      27.53    98.91
10533    14526541 Sa      67.06     0.03
10534    10993779 Sa         99        0
10535    15684120 Sa     112.99        0
10536     6051457 Sa        249        0
10537    10983989 Sa     149.97        0
10538     8222030 Sa         59        0
10539    10910428 Sa      237.2        0
10540     8477371 Sa     126.44    -98.9


Last edited by ctsgnb; 06-23-2013 at 06:16 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare 2 files and match column data and align data from 3 column asnandhakumar Shell Programming and Scripting 5 10-16-2012 09:58 AM
Get data from 3 differrnt oracle DB & then compare data Amit.Sagpariya Shell Programming and Scripting 4 10-14-2008 07:03 AM
Compare Data in the same file lweegp UNIX for Dummies Questions & Answers 1 10-17-2005 02:32 AM
Compare data files ithomp Shell Programming and Scripting 1 06-15-2004 10:08 AM
compare data bensky UNIX for Dummies Questions & Answers 3 10-10-2003 05:17 AM



All times are GMT -4. The time now is 09:33 AM.