Two files comparision with single field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Two files comparision with single field
# 1  
Old 04-10-2013
Two files comparision with single field

Hi ,

Im new to uxin environment and shell scripting....

please help me with the code for the following scenario.....

file 1 contains the following fields


Code:
abc 200 rupee IND
cdf  400 dollar USA
efg  300 euro  GER
 hij  600 pound ENG

file 2
Code:
SBI abc 321 dollar CANAD
kvr mnd  345 pound FRANC
axs efg  879 euro   RUSSIA

if field 1 of file1 matches field 2 of file2 then complete row of filed2 in file should be displayed.

output file:

Code:
SBI abc 321 dollar CANAD
axs efg 879 euro RUSSIA

Can someone help on this ....

Last edited by joeyg; 04-10-2013 at 03:38 PM.. Reason: Please wrap data and commands within CodeTags
# 2  
Old 04-10-2013
Code:
awk 'NR==FNR{a[$1]++;next}a[$2]' file1 file2

# 3  
Old 04-12-2013
thanks for the reply...

im getting the below error ..
Code:
 
$ awk 'NR==FNR{a[$1]++;next}a[$2]' f1.dat_old f2.dat_old
awk: syntax error near line 1
awk: bailing out near line 1

please let me know how to resolve.

thanks
Shivaji...
# 4  
Old 04-12-2013
On Solaris use /usr/xpg4/bin/awk rather than awk


--
@pravin27
It is better to use
Code:
$2 in a

instead of
Code:
a[$2]

Because otherwise awk will create a new element a[$2] for every line in the second file.

Last edited by Scrutinizer; 04-12-2013 at 03:36 AM..
# 5  
Old 04-12-2013
thanks.....its working Smilie
# 6  
Old 04-12-2013
would you please explain the login in the syntax "
Code:
{a[$1]++;next}a[$2]

"


thanks
Shivaji

Last edited by Scrutinizer; 04-12-2013 at 03:34 AM.. Reason: code tags
# 7  
Old 04-13-2013
Hi.

Assuming that the files are standardized to have fields separated by single spaces, then here are 2 alternate solutions:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate extraction by matching fields.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C cut fgrep join

FILES="data?"

pl " Input data file $FILES:"
head $FILES

pl " Results, grep:"
fgrep -f <(cut -f1 -d" " data1) data2

pl " Results, join:"
sort -t " " --key=1,1 data1 > f1
sort -t " " --key=2,2 data2 > f2
join -1 1 -2 2 -t " " -o 2.1,2.2,2.3,2.4,2.5 f1 f2
rm -f f1 f2

exit 0

prodiucing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
cut (GNU coreutils) 6.10
fgrep GNU grep 2.5.3
join (GNU coreutils) 6.10

-----
 Input data file data?:
==> data1 <==
abc 200 rupee IND
cdf 400 dollar USA
efg 300 euro GER
hij 600 pound ENG

==> data2 <==
SBI abc 321 dollar CANAD
kvr mnd 345 pound FRANC
axs efg 879 euro RUSSIA

-----
 Results, grep:
SBI abc 321 dollar CANAD
axs efg 879 euro RUSSIA

-----
 Results, join:
SBI abc 321 dollar CANAD
axs efg 879 euro RUSSIA

See man pages for details.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparision of two data columns in different files

Hi All, I have a requirement to compare data column which is the last field in two different files and trigger and alert if the difference is greater than 1 for each row. File1 Jan Acount1 2014 11223 Feb Account2 2014 2345 Mar Account3 2014 1233 File2 Jan Account1 2014... (1 Reply)
Discussion started by: Naresh Babu
1 Replies

2. Shell Programming and Scripting

Comparision of two text files

Dear all, I am having two files big files i need an output file as first occurance of file1 field in file2 example: file1:raju ranifile2:raju|123 raju|879 rani|623 rani|253result:raju|123 rani|623pls help me in this regard (3 Replies)
Discussion started by: suryanarayana
3 Replies

3. Shell Programming and Scripting

awk comparision between 2 files and substitution in third

Hi All, I have two files in the following format. File 1 : 1044|1|20121031|2910039.4|MR|201210|G1044|E 1082|2|20121031|1664662.84|MR|201210|G1082|E 1696|3|20121031|190801.5|MR|201210|G1696|E 1824|4|20121031|196350|MR|201210|G1824|E 1900|5|20121031|221447.8|MR|201210|G1900|E File 2 :... (7 Replies)
Discussion started by: nua7
7 Replies

4. UNIX and Linux Applications

Unix Shell Scripting : Comparision of two files

Hi, We need to compare a text file File1.txt and config file File2.txt in a way that it checks if the content of File1.txt exists between the range mentioned in File2.cfg. The range here is the range between col1 and col2 of File2.cfg If the content of File1.txt lies between the range of... (12 Replies)
Discussion started by: CFA
12 Replies

5. Shell Programming and Scripting

perl: comparision of field line by line in two files

Hi everybody, First I apologize if my question seems demasiad you silly, but it really took 4 days struggling with this, I looked at books, forums ... And Also ask help to a friend that is software developer and he told me that it is a bad idea do it by perl... but this is my problem. I moved to... (8 Replies)
Discussion started by: Thelost
8 Replies

6. Shell Programming and Scripting

Comparision of fields in 2 files.

Hi Experts, I have two huge files in the format as shown below.I need to open a file1 and file 2 , cut first 24 characters of file 1 and search if the key exists in file 2 first field (delimted by *). If the value exists , copy the third field from file 2 and replace the 5th field in file 1 .... (4 Replies)
Discussion started by: nua7
4 Replies

7. UNIX for Advanced & Expert Users

Comparision of two files.

File Structure file1.txt.arch 029429288,1,,,02087400376,N,02087400376,N,0,02087400376,N,0,0,8010,08000151736,U,N,,08000151736,U,20100726111237,20100726111237,0,20100726111651,00004140,16,16,10,N;... (1 Reply)
Discussion started by: ravigupta2u
1 Replies

8. Shell Programming and Scripting

Column comparision in two files

Hi, I need to compare a column in two different csv files file1 xyz.com,2/2/12,a,b,c eg.com,2/2/23,a,b,ga file2 1,2,ua,xyz.com 1,2,ua,abc.com 1,2,ua,eg.com 1,2,ua,easg.com 1,2,ua,zth.com Read all entries in file1(which has 1000+) and compare column1 of file1 with the column4... (13 Replies)
Discussion started by: nuthalapati
13 Replies

9. Shell Programming and Scripting

replace single field out of 60

I have a pipe delimited file with over 60 fields on each line, and hundreds of lines...I want to replace field 6 only.... So what is the better way to do this command.... awk -F'|' '{print $1"|"$2"|"$3"|"$4"|"$5"| TXT6"$7"|"$8"|"....$59"|"$60}' infile > outfile I dont even know if this... (2 Replies)
Discussion started by: ajp7701
2 Replies

10. Shell Programming and Scripting

comparision of string in various files

i want to take position 19-24(only first line) from all files and need to compare any duplication is there or not. If duplication, then i have to print the file names. I have written to take the characters from 19-24 from all files. but how to compare ? ... (1 Reply)
Discussion started by: senthil_is
1 Replies
Login or Register to Ask a Question