Unix Programming - Lookup between 2 files ( korn shell )

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2006 > Lookup between 2 files ( korn shell )





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Lookup between 2 files ( korn shell )
pawan_test

2006-01-29, 9:32 pm

Hello

Thanks guys for your suggestions;

FILE1=/home/pavi/DS.txt
FILE2=/home/pavi/chain.txt

awk -F ":" '{print $1}' $FILE1 > f1.txt
awk -F " " '{print $2}' $FILE2 > f2.txt


DS.txt contains;

ALBD:xxxxx
FLN:yyyyyyy
WLG:ttttttt

chain.txt contain;

ECK: ECK: 331
ECK: ECK: 336
FED :FED :427
FIE :FIE: 212
FLN :FLN: 140
FLN :FLN :141
FMF: FMF: 183
FR1 :FR1: 303
FTT :FTT :423
GAT :GAT: 185
GEN :GEN: 396
GFY :GFY: 268
GIA :GIA: 228
GMD :GMD: 264
MIN: MIN: 158
MRO :MRO: 354
MY0 :MY0: 315
NCS :NCS: 390
OSC :ALBD: 377
OSC :ALBD :378
OSC: ALBD: 379
OSC ALBD 380
WLG :WLG :243
WLG: WLG: 244
WLG :WLG: 245
WLG :WLG :246
MAT: WLG :247

the result.txt has to look like this;


OSC :ALBD: 377
OSC :ALBD :378
OSC: ALBD: 379
FLN :FLN: 140
FLN :FLN :141
WLG :WLG :243
WLG: WLG: 244
WLG :WLG: 245
WLG :WLG :246
MAT: WLG :247

the script has to match the 1st column from DS.txt with the 2nd column
of chain.txt and only those that matched, move all the corresponding
1st , 2nd and 3rd columns of chain.txt to a output file say result.txt

this is what i am trying to accomplish.

can anyone please help me ..

tx
pavi

Logan Shaw

2006-01-29, 9:32 pm

pawan_test wrote:
> the script has to match the 1st column from DS.txt with the 2nd column
> of chain.txt and only those that matched, move all the corresponding
> 1st , 2nd and 3rd columns of chain.txt to a output file say result.txt


Here's how I'd do it:

#! /usr/bin/perl

use IO::File;

my $ds_fh = IO::File->new ("DS.txt", "r");
die "can't open DS.txt ($!)" if not defined $ds_fh;

my $chain_fh = IO::File->new ("chain.txt", "r");
die "can't open chaint.txt ($!)" if not defined $chain_fh;

my $line;
my %should_print;
while (defined ($line = $ds_fh->getline()))
{
my @ds_fields = split (/\s+:\s+/, $line);

$should_print{$ds_fields[0]} = 1;
}

while (defined ($line = $chain_fh->getline()))
{
my @chain_fields = split (/\s+:\s+/, $line);

print $line if $should_print{$chain_fields[1]};
}

Hope that helps.

- Logan
pawan_test

2006-01-29, 9:32 pm

Hey Logan.

thanks for your suggestion. but i have to do it in shell ( Korn ) only.
..
can you please let me know if you would do it it shell

tx
pavi

Chris F.A. Johnson

2006-01-29, 9:32 pm

On 2006-01-27, pawan_test wrote:
> Hey Logan.
>
> thanks for your suggestion. but i have to do it in shell ( Korn ) only.


You have to do what? Please read <http://cfaj.freeshell.org/google>.

> can you please let me know if you would do it it shell



--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
pawan_test

2006-01-29, 9:32 pm

Hello.,

i am trying to write a korn shell program with the above requirement.
so i was looking for any suggestions on this forum.

thanks & regards
pavi

pawan_test

2006-01-29, 9:32 pm

Hello.,

i am trying to write a korn shell program with the above requirement.
so i was looking for any suggestions on this forum.

thanks & regards
pavi

pawan_test

2006-01-29, 9:32 pm

Hello.,

i am trying to write a korn shell program with the above requirement.
so i was looking for any suggestions on this forum.

thanks & regards
pavi

Chris F.A. Johnson

2006-01-29, 9:32 pm

On 2006-01-28, pawan_test wrote:
> Hello.,
>
> i am trying to write a korn shell program with the above requirement.
> so i was looking for any suggestions on this forum.


What "above requirement"?

You are posting to a Usenet newsgroup, not a web forum. Please
read the link I posted previously.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Logan Shaw

2006-01-29, 9:32 pm

pawan_test wrote:
> thanks for your suggestion. but i have to do it in shell ( Korn ) only.


Ah, well you could've mentioned that!

In that case, I would do this:

#! /bin/ksh

# make an alphabetized list of tokens we are looking for
awk -F: '{print $1}' DS.txt | sort | sed -e 's/$/:/' > DS-list.txt

# process each line of chain.txt
while read line
do
token=`echo $line | awk -F: '{print $2}' | sed -e 's/ //g'`
if look "$token:" DS-list.txt > /dev/null 2>&1
then
echo "$line"
fi
done < chain.txt

# clean up
rm DS-list.txt

The logic behind this method is to create a file with a sorted list of
the tokens that are allowed, then use the "look" command to check whether
a given token is in the file. Just in case one token can be a prefix
of another, I add a ":" to the end of every line in the list of tokens
and search by adding the ":" to my search string as well.

Note that this isn't tested, so it may not work at all, but it should.

- Logan
pawan_test

2006-01-30, 8:42 am

Hi all.,

Hi ALL.,

THANKS for all your responses.
it seems to be working if the DS.txt is in this format
ALBD:xxxxx
FLN:yyyyyyy
WLG:ttttttt

i am getting the desired output i.e
OSC :ALBD: 377
OSC :ALBD :378
OSC: ALBD: 379
FLN :FLN: 140
FLN :FLN :141
WLG :WLG :243
WLG: WLG: 244
WLG :WLG: 245
WLG :WLG :246
MAT: WLG :247


but when i modified the DS.txt as;

ALBD:xxxxx
FLN:yyyyyyy
GIE:ttttttt ( here there is no match)
WLG:ttttttt

then the desired output is getting repeated twice..result.txt is;

OSC :ALBD: 377
OSC :ALBD :378
OSC: ALBD: 379
FLN :FLN: 140
FLN :FLN :141
WLG :WLG :243
WLG: WLG: 244
WLG :WLG: 245
WLG :WLG :246
MAT: WLG :247
OSC :ALBD: 377
OSC :ALBD :378
OSC: ALBD: 379
FLN :FLN: 140
FLN :FLN :141
WLG :WLG :243
WLG: WLG: 244
WLG :WLG: 245
WLG :WLG :246
MAT: WLG :247

the code that i tried is;
for item in `cat $FILE1 | awk -F: '{print $1}'`
do
grep -w $item $FILE2 >>result_file.txt
done

can anyone please give me a suggestion;

thanks
pavi

Ralf Fassel

2006-01-30, 8:42 am

* "pawan_test" <sridhara007@gmail.com>
| then the desired output is getting repeated twice..result.txt is;
--<snip-snip>--
| the code that i tried is;
| for item in `cat $FILE1 | awk -F: '{print $1}'`
| do
| grep -w $item $FILE2 >>result_file.txt
| done
|
| can anyone please give me a suggestion;

You probably need to remove the result_file.txt file before the loop.
Otherwise you just append to an already existing file.

R'
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com