Match words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match words
# 1  
Old 01-06-2008
Match words

I have two files

First one is as follow:

5:John
4:Michel
9:Rachel
100:George

Second file is as follow:

9
100
4
5
5
4
9
100
100

I want to process the two files to have the output.

Rachel
George
Michel
John
John
Michel
Rachel
George
George

Could any one help me to have script using bash scripting or perl?
# 2  
Old 01-06-2008
Code:
#!/bin/sh
while read line
do
     while IFS=":" read a b
     do       
        if [ "$a" == "$line" ];then
          echo $b
        fi
     done < "file1"
done <"file2"

# 3  
Old 01-06-2008
Hi.

Using a shell array:
Code:
#!/usr/bin/env sh

# @(#) s2       Demonstrate saving file in array, processing based on index.

set -o nounset
echo

debug=":"
debug="echo"

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash

echo

FILE1=${1-data1}
echo " Input file $FILE1:"
cat $FILE1

echo
FILE2=${1-data2}
echo " Input file $FILE2:"
cat $FILE2

echo
echo " Results from processing:"

while IFS=":" read a b
do
  content[$a]="$b"
done <$FILE1

while read a
do
  echo ${content[$a]}
done <$FILE2

exit 0

Producing:
Code:
% ./s2

(Versions displayed with local utility "version")
GNU bash 2.05b.0

 Input file data1:
5:John
4:Michel
9:Rachel
100:George

 Input file data2:
9
100
4
5
5
4
9
100
100

 Results from processing:
Rachel
George
Michel
John
John
Michel
Rachel
George
George

See http://www.tldp.org/LDP/abs/html/arrays.html#EX66 for examples of array use .... cheers, drl
# 4  
Old 01-06-2008
Since OP is Open2Perl Smilie

Code:
#!/usr/bin/perl

my %lookup;
my $main = pop @ARGV;
while (<>) {
        split /:/;
        $lookup{$_[0]} = $_[1];
}

@ARGV = ($main);
while (<>) {
        chomp;
        exists($lookup{$_}) and print $lookup{$_};
}

Output:

Code:
$ cat file.1
5:John
4:Michel
9:Rachel
100:George

$ cat file.2
9
100
4
5
5
4
9
100
100

$ l.pl file.1 file.2
Rachel
George
Michel
John
John
Michel
Rachel
George
George

I'm sure this can made cleaner/tighter in perl (I'm out of touch with perl).

HTH
# 5  
Old 01-07-2008
A small one (in one line)

$ cat fileb | while read num
> do
> awk -F ":" '$1=="'"$num"'" {print $2}' filea
> done

And If you want to check for "NO MATCH FOUND" in filea(first file)

$ cat fileb | while read num
> do
> NAME=`awk -F ":" '$1=="'"$num"'" {print $2}' filea`
> [ -z $NAME ] && echo "NO MATCH FOUND" || echo $NAME
> done

//Jadu
# 6  
Old 01-07-2008
If the filesize is small, anything goes Smilie
# 7  
Old 01-07-2008
use nawk on Solaris
Code:
awk 'BEGIN{FS=":"}
FNR==NR{
  array[$1]=$2
  next
}
{ print array[$1] }' file file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

2. Shell Programming and Scripting

Grep two words with exact match

HI Input : Counters Counter Int Ints Counters Counters Ints Ints I want to grep Counter|Int Output : Counter (1 Reply)
Discussion started by: pareshkp
1 Replies

3. Shell Programming and Scripting

Match words and fetch data in front of it in second column

Hi all, I have 2 files one file contain data like this in one column AST3 GSTY4 JST3 second file containign data like this in 2 columns AST3(PAXXX),GSTY4(PAXXY) it is used in diabetes KST4 it is used in blood... (6 Replies)
Discussion started by: manigrover
6 Replies

4. Shell Programming and Scripting

Match the word or words and fetch the entries

Hi all, I have 7 words Now I have 1 file which contain data in large number of rows and columns and 6th column contain any of these words or may be more than one words among above 7 words: I want script should search for the above mentioned 7 words in the 6th column ... (9 Replies)
Discussion started by: manigrover
9 Replies

5. Shell Programming and Scripting

Match groups of capital words using gawk

Hi I'd like to extract from a text file, using gawk, the groups of words beginning with a capital letter, that are not at the begining of a sentence (i.e. Not after a full stop and a pace ". "), including special characters like registered or trademark (® or ™ ). For example I would like to... (1 Reply)
Discussion started by: louisJ
1 Replies

6. Shell Programming and Scripting

Count number of match words

Input: some random text SELECT TABLE1 some more random text some random text SELECT TABLE2 some more random text some random text SELECT TABLE3 some more random text some random text SELECT TABLE1 some more random text Output: 'SELECT TABLE1' 2 'SELECT TABLE2' 1 'SELECT TABLE3' 1 I... (5 Replies)
Discussion started by: chitech
5 Replies

7. Shell Programming and Scripting

awk or grep to match # of words before and after pattern

Pipe binary file matches grep results to file I am using grep to match a pattern, but the output is strange. $ grep -r -o "pattern" * Gives me: Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches To find the lines before/after, I then have to use the... (1 Reply)
Discussion started by: chipperuga
1 Replies

8. Shell Programming and Scripting

printing words based on column match

pls help Input: file1 word1 text1 word2 text2 word3 text3 file2 word1 text11 word3 text13 can u pls help in getting the same output: file1 text1 text2 text3 (1 Reply)
Discussion started by: bha148
1 Replies

9. Shell Programming and Scripting

Print all the words after a match word

Hi, I want to print all words till the last word after the match of "ERROR" word. For e.g. I'll get an sqlplus error with e.g. 1 $ ./calltest_fn.ksh var test_var:=test_fn1; calltest_fn.ksh file1 file2 file3 ERROR at line 4: ORA-06550: line 4, column 11: PLS-00201: identifier... (5 Replies)
Discussion started by: dips_ag
5 Replies

10. Shell Programming and Scripting

grep on multiple words to match text template

hi, I have few text templates as a simple ex: template 1 city Name: zip code: state Name: template2: employee Name: Phone number: I wish to grep on given text file and make sure the text file matches one of these templates. Please give your ideas. (6 Replies)
Discussion started by: rider29
6 Replies
Login or Register to Ask a Question