LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-15-2011, 11:19 PM   #1
ali2011
Member
 
Registered: Nov 2011
Location: USA, CA
Distribution: Ubuntu+Fedora
Posts: 80

Rep: Reputation: Disabled
Matching Between Two Files:


I have two files, a.txt and b.txt. The file a.txt (3 columns) is containing the data in file b.txt (1 column) and more. What I am looking to do is to search in a.txt for each element in b.txt, and print its corresponding line. Here is a partial cut of the two big files:

a.txt
Code:
planetlab4-dsl.cs.cornell.edu	42.4436     -76.4816
planetlab2.cs.purdue.edu	40.4274     -86.9167
planet2.scs.cs.nyu.edu	40.7292     -73.9937
planetlab2.cnis.nyit.edu	40.814     -73.6081
ricepl-1.cs.rice.edu	29.7225     -95.3924
pl1.eecs.utk.edu	35.9483     -83.9367
pl2.eecs.utk.edu	35.9483     -83.9367
planetlab2.itwm.fhg.de	49.26     7.46
planetlab06.cs.washington.edu	47.6531     -122.313
planetlab2.cs.ubc.ca	49.2637     -123.237
pub1-s.ane.cmc.osaka-u.ac.jp	34.81     135.52
pub2-s.ane.cmc.osaka-u.ac.jp	34.81     135.52
planetlab1.citadel.edu	32.7984     -79.9614
planetlab2.citadel.edu	32.7984     -79.9614
pl2.ernet.in	28.6     77.09
planetlab3.inf.ethz.ch	47.3794     8.54513
planetlab4.inf.ethz.ch	47.3794     8.54513
b.txt
Code:
planetlab2.cnis.nyit.edu
planetlab1.cs.purdue.edu
planetlab2.cs.purdue.edu
planet3.cs.ucsb.edu
planetlab1.cs.colorado.edu
planetlab02.sys.virginia.edu
planet1.cs.rochester.edu
planetlab2.byu.edu
planetlab06.cs.washington.edu
planetlab04.cs.washington.edu
out.txt

Code:
planetlab2.cnis.nyit.edu	40.814     -73.6081
planetlab2.cs.purdue.edu	40.4274     -86.9167
planetlab06.cs.washington.edu	47.6531     -122.313
So, the script will look for each line in b.txt in the first column in a.txt, and if a match found, it will print that 3 columns corresponding line from b.txt in out.txt. In this example just 3 matches were found.

Last edited by ali2011; 12-15-2011 at 11:23 PM.
 
Old 12-15-2011, 11:43 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 208Reputation: 208Reputation: 208
Here's one possible solution.

Hash the contents of file a, then look up the fields in file b. If there's a match, print it.

Code:
#!/bin/bash

declare -A servers

function hash_contents() {
    while read line; do
        fields=($line)
        servers[${fields[0]}]="${fields[1]}\t${fields[2]}"
    done < $@
}

function lookup_fields() {
    while read line; do
        [ ! -z "${servers[$line]}" ] && \
            echo -e $line'\t'${servers[$line]}
    done < $@
}

hash_contents a.txt
lookup_fields b.txt

exit 0
 
1 members found this post helpful.
Old 12-16-2011, 12:53 AM   #3
ali2011
Member
 
Registered: Nov 2011
Location: USA, CA
Distribution: Ubuntu+Fedora
Posts: 80

Original Poster
Rep: Reputation: Disabled
Worked perfectly, thanks a lot.

All the best
 
Old 12-16-2011, 06:54 AM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,320

Rep: Reputation: 252Reputation: 252Reputation: 252
There is also the command join:
Code:
$ join <(sort a.txt) <(sort b.txt)
planetlab06.cs.washington.edu 47.6531     -122.313
planetlab2.cnis.nyit.edu 40.814     -73.6081
planetlab2.cs.purdue.edu 40.4274     -86.9167
 
2 members found this post helpful.
Old 12-16-2011, 07:22 AM   #5
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 208Reputation: 208Reputation: 208
Quote:
Originally Posted by Reuti View Post
There is also the command join
Ah! Just totally pwned my solution with a one-liner.

Quote:
"Those who don't understand UNIX are condemned to reinvent it, poorly." Henry Spencer
Anyhow, good solution.
 
  


Reply

Tags
bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 8 12-18-2013 06:36 PM
matching and printing fields of two files ksaad Programming 3 11-07-2011 03:15 PM
[bash] find - filter files matching a list of files hashbang#! Programming 6 11-25-2010 06:01 PM
Finding matching patterns in 2 files herveld Programming 25 12-01-2008 04:35 PM
help with matching input with 2 different files in php ice99 Programming 1 12-09-2006 09:55 PM


All times are GMT -5. The time now is 06:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration