Lookup between 2 files ( korn shell )


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Lookup between 2 files ( korn shell )
# 8  
Old 01-27-2006
lookup between 2 files ( korn shell)

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
# 9  
Old 01-27-2006
Here you do

#!/bin/sh

for item in `cat file_1|awk -F: '{print $1}'`
do
grep -w $item file_2 >>result_file
done

Good Luck
# 10  
Old 01-28-2006
Hey Pavi,


This should work out for you....

Quote:
cat file1.txt|while read LINE
do
col1=`echo $LINE|awk -F ":" '{print $1}'`
awk -F ":" '{if ($2 == "'$col1'") print $0}' file2.txt >> res.txt
done
mv res.txt result.txt
Larry Please correct me if I m wrong, but I think in the solution you gave, even if the first column of file2 matches it will be placed in the result, but we need to compare the second column of file2. And please can you explain the -w option of grep please, this newb is a bit confused about it

Cheers,

Gaurav
# 11  
Old 01-30-2006
lookup between 2 files ( korn shell)

Hello Gaurav.,

i tried with the cod eyou suggested.

########################################
## no output for this code ####
cat $FILE1 | while read LINE
do
col1=`echo $LINE|awk -F ":" '{print $1}'`
awk -F " " '{if ($2==$col1) print $0}' $FILE2 >> res.txt
done
cp res.txt result1.txt
rm res.txt

##########################################

it is creating 1byte file result.txt . it is not writing the output.

can you suggest ay alternate method.

thanks
pavi
# 12  
Old 01-30-2006
Hi Pavi,

I looked carefully at the chain.txt and this may be hapeneing due to the difference in format of each line. Means the position of ':' is different for different records

Here is chain.txt

chain.txt contain;

ECK: ECK: 331
ECK: ECK: 336 (':' is just after the 1st coloumn)
FED :FED :427 ( As you can observe ':' is just before the 2nd column)
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 (No ':' in this record)
WLG :WLG :243
WLG: WLG: 244
WLG :WLG: 245
WLG :WLG :246
MAT: WLG :247

Are you sure that this file be like this only, otherwise if there is one uniform format for DS.txt and chain.txt, the solution will work.
I may be wrong, can some guru please throw some light on it.

If possible copy and paste some sample data from the files.

Bye,
Gaurav
# 13  
Old 01-30-2006
Code:
[/tmp]$ cat pavan.ksh 
#! /bin/ksh
while read line
do
DL=${line%%:*}
sed -n -e "s_:.* *${DL} *.*:_&_p" chain.txt > result.txt
done < DS.txt
[/tmp]$ ./pavan.ksh 
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
[/tmp]$


Last edited by vino; 01-30-2006 at 03:15 AM.. Reason: enhancement...
# 14  
Old 01-30-2006
lookup between 2 files ( korn shell)

Hello Gaurav.,

ALL the fields delimiter in chain.txt is a : ( colon delimited).
like ECK:ECK:331
ECK: ECK: 336
FED:FED:427
FIE:FIE: 212

when i was posting the question may be it was my type error.

chain.txt contain;

ECK: ECK: 336 (':' is just after the 1st coloumn) ( my typo error)
FED :FED :427 ( As you can observe ':' is just before the 2nd column)(type error)

tx
pavi


chain.txt contain;

ECK: ECK: 331
ECK: ECK: 336 (':' is just after the 1st coloumn)
FED :FED :427 ( As you can observe ':' is just before the 2nd column)
FIE :FIE: 212
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Korn shell Script to combine Two files in one

Hello All , I am new to this Forum, I am trying to write a script to combine two data files with 1 column in common and others columns are different . File1 Apple 29 tomatao 4 grapes 25 File2 Apple fruit tomatao veg grapes fruit other (3 Replies)
Discussion started by: gagan0119
3 Replies

2. Shell Programming and Scripting

Korn shell - lookup table

Hi All I need to pass country code into a pipe delimited file for lookup. It will search country code (column 3) in the file, if the country code matched, it will return value from other columns. Here is my mapping file. #CountryName|CountryRegion|CountryCode-3|CountryCode-2... (5 Replies)
Discussion started by: lafrance
5 Replies

3. Shell Programming and Scripting

Korn shell script to sync/move files that are not in use

Hello all. This may seem like a dumb/easy question but right now I have a little script I made that uses rsync to sync a directory that has files in it that may or may not be complete files. I want to come up with a better solution for this. What it is is I have a directory lets say /incomplete... (4 Replies)
Discussion started by: linuxn00b
4 Replies

4. Shell Programming and Scripting

korn shell remove files question

how do you show each filename in a giving directory and delete the specific file in korn script i was thinking using ls rm ? but i cant make it work (0 Replies)
Discussion started by: babuda0059
0 Replies

5. Shell Programming and Scripting

korn shell how to solve rm file and display files

Write a KSH script called cleanse which displays the name of each file in a given directory and allows the user to interactively decide whether or not to keep or delete the specific file. Notes: Again, please check for errors. can any one help on this problem ?? (1 Reply)
Discussion started by: babuda0059
1 Replies

6. UNIX for Dummies Questions & Answers

Initializing files to empty in korn shell

hello, i want to know how to initialize a file to an empty one in korn shell scripting? i'm using a file name and building it during a while loop using >>. The problem occurs when the file is not empty before reaching the while loop. therefore, i want to initialize it before the loop to get... (6 Replies)
Discussion started by: alrinno
6 Replies

7. UNIX for Dummies Questions & Answers

Korn shell awk use for updating two files

Hi, I have two text files containing records in following format: file1 format is: name1 age1 nickname1 path1 name2 age2 nickname2 path2 file 1 example is: abcd 13 abcd.13 /home/temp/abcd.13 efgh 15 efgh.15 /home/temp/new/efgh.15 (4 Replies)
Discussion started by: alrinno
4 Replies

8. UNIX for Dummies Questions & Answers

find and FTP multiple files in Korn Shell

I want to FTP multiple files in a directory that the files were created since midnight of the same day using korn shell. I can use the "find" command using -newer arguement that compares against a time stamp file. The script identifies the files, I can't use a variable = `find . ` as the... (2 Replies)
Discussion started by: lambjam
2 Replies

9. Shell Programming and Scripting

How to process multiple files in Korn Shell

How do I make the below ksh to process all of the files inside a user specified directory? Currently it can only process one file at a time. #!/bin/ksh tr -s '\11 ' ' ' < $1 > temp0 sed -e 's/,//g' temp0 > temp1 cut -d' ' -f1,4,5 temp1 > final_output rm temp0 temp1 (3 Replies)
Discussion started by: stevefox
3 Replies

10. Shell Programming and Scripting

korn shell + sftp + list files

Hello!!! I need a korn shell script in AIX that inside sftp environment, changes a remote directory, lists the files inside it, and stores in an array. I got it working before make a sftp, but after.. I can't.. The way it is, it lists the files in local path... so.. not what I want, but... (1 Reply)
Discussion started by: alienET
1 Replies
Login or Register to Ask a Question