Fetch lines from a file matching column2 of another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch lines from a file matching column2 of another file
# 1  
Old 08-14-2008
Fetch lines from a file matching column2 of another file

Hi guys,
Please help me out in this problem. I have two files

FILE1
abc-23 : 4529675
cde-42 : 9824532
dge-91 : 1245367
gre-45 : 9824532
fgr-76 : 4529675

FILE2
4529675 : Gal Glu house-2-be
9824532 : cat mouse
1245367 : sirf surf-2-beta

where FILE2 is a static file with fixed contents.

I need an output file like this-

FILE3
abc-23 Gal Glu house-2-be
cde-42 cat mouse
dge-91 sirf surf-2-beta
gre-45 cat mouse
fgr-76 Gal Glu house-2-be

It should contain column 1 of FILE1 and column 2 of FILE2 where it matches column 2 of FILE1 with column 1 of FILE2.

Thanks.
# 2  
Old 08-14-2008
one way to do it:
Code:
while read line
do
    str=`echo $line | cut -d' ' -f3`  &&  echo -n `echo $line | cut -d' ' -f1`  &&  grep $str file2 | cut -d' ' -f3-
done < file1

# 3  
Old 08-14-2008
Thanks! Yogesh

There is no space between column1 and 2 of the output

abc-23Gal Glu house-2-be
cde-42cat mouse
dge-91sirf surf-2-beta
gre-45cat mouse
fgr-76Gal Glu house-2-be

I need a tab between column1 and 2
# 4  
Old 08-14-2008
this is a problem about mapping from two files which had been answered many times.

So, show us what have you tried before ? Smilie
# 5  
Old 08-14-2008
Code:
nawk 'BEGIN{FS=":"}
 {
 if(NR==FNR)
 {
	gsub(/ /,"",$1)
	t[$1]=$2
 }
 else
 {
	gsub(/ /,"",$2)
 	print $1"  "t[$2]
 }
 }' file2 file1

# 6  
Old 08-21-2008
Thanks summer_cherry :)

The code is working fine but If you could explain what's happening in the code I can understand it better.

Thanks,
smriti
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to average matching lines in file

The awk below executes and is close (producing the first 4 columns in desired). However, when I add the sum of $7, I get nothing returned. Basically, I am trying to combine all the matching $4 in f1 and output them with the average of $7 in each match. Thank you :). f1 ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to combine matching lines in file

I am trying to combine all matching lines in the tab-delimited using awk. The below runs but no output results. Thank you :). input chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 47433390 47433999 SYN1... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Substraction of matching lines from a file.

I have 2 files: file1.txt contains /html/mybook/Charts/143712/reptiles.pdf /html/mybook/Charts/198459/spices.pdf /html/mybook/Charts/198459/fresh_nuts.pdf /html/mybook/Charts/123457/dome_anim.pdf /html/mybook/Charts/123457/vegetables.pdf /html/content/3DInteractive/174091/CSPSGGB.html ... (6 Replies)
Discussion started by: Jojan Paul
6 Replies

4. UNIX for Dummies Questions & Answers

FIND matching pattern of lines in a file

I need to search for two patterns in a file and find number of matching lines. find . -type f | xargs grep "DROP TABLE" | wc -l find . -type f | xargs grep "DROP SYNONYM" | wc -l The above code works. However I am looking at finding a commnd that will simplify as on a singe command... (2 Replies)
Discussion started by: Siva SQL
2 Replies

5. Shell Programming and Scripting

Print matching lines in a file

Hello everyone, I have a little script below: die "Usage infile outfile reGex" if @ARGV != 3; ($regex) = @ARGV; open(F,$ARGV) or die "Can't open"; open(FOUT,"+>$ARGV") or die "Can't open"; while (<F>) { print FOUT if /$regex/.../$regex/; } No matter what I give $regex on the... (2 Replies)
Discussion started by: new bie
2 Replies

6. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

7. Shell Programming and Scripting

Print lines matching value(s) in other file using awk

Hi, I have two comma separated files. I would like to see field 1 value of File1 exact match in field 2 of File2. If the value matches, then it should print matched lines from File2. I have achieved the results using cut, paste and egrep -f but I would like to use awk as it is efficient way and... (7 Replies)
Discussion started by: SBC
7 Replies

8. Shell Programming and Scripting

How to print file without few exactly matching lines?

Hi I have a very long file with 4 columns of numbers for example 1875 1876 12725 12723 13785 13786 4232 4230 13184 13185 ... (2 Replies)
Discussion started by: ananyob
2 Replies

9. Shell Programming and Scripting

delete lines in file matching a pattern

I have a text file, a sample of which is as follows: r/- * 0: WINDOWS/Microsoft.NET/Framework/v2.0.50727/ASP.NETWebAdminFiles/Images/headerGRADIENT_Tall.gif r/- * 0: WINDOWS/SoftwareDistribution/Download/cf8ec753e88561d2ddb53e183dc05c3e/backoff.jpg r/- * 0: ... (2 Replies)
Discussion started by: stumpyuk
2 Replies
Login or Register to Ask a Question