The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
file Lookup using awk jerome Sukumar Shell Programming and Scripting 1 08-30-2007 03:28 AM
getting particular text after grep from lookup file napolayan UNIX for Dummies Questions & Answers 10 10-20-2006 10:52 AM
reverse lookup file problem Westy564 IP Networking 2 01-09-2004 02:55 PM
file lookup gillbates UNIX for Dummies Questions & Answers 6 12-12-2003 02:04 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-20-2006
pavan_test pavan_test is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 53
Lookup with a file

Hi All,

i have a variable which has a value in it.
RETAILER='JEWL'

i have a text file. Name: file.txt

file.txt
________
WLG 150
JEWL 60
CVS 240
FLN 120
WND 120


I am trying to write a korn script.the script, based on the value in the RETAILER will do a look up against the file.txt. if match found then return the corresponding value in the 2nd column in the file.txt.

Eg: do a look up with RETAILER with file.txt.
JEWL and JEWL (in file.txt) match found then return the value corresponding to JEWL in file.txt i.e 60.

Eg2: RETAILER='WLG'. do a look up with file.txt and return 150.

can anyone please help me. ( korn shell)

Thanks & Regards
pavi.
  #2 (permalink)  
Old 07-20-2006
ducatisti ducatisti is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 2
cat file.txt | awk '/JEWL/ {print $2}'


cat the file and pipe it to awk. Awk is a pattern/action language. The above simple script will look for the pattern JEWL and print the 60 that is the second feild of the row that begins with JEWL.

So you can pull in a variable on the command line and then if that variable matches any of the first feilds of the file.txt file, then, print feild 2.


try this on the command line so that you can see the above program string work. Then we can talk about translating the rest of the algorithm into ksh.


-Douglas
  #3 (permalink)  
Old 07-20-2006
pavan_test pavan_test is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 53
lookup

Thanks a lot
  #4 (permalink)  
Old 07-20-2006
pavan_test pavan_test is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 53
lookup ( korn shell)

hello Douglas,

i tried this one;
cat file.txt | awk '/JEWL/ {print $2}'

but i am not getting any output.

thanks
pavi
  #5 (permalink)  
Old 07-20-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
ducatisti, you need not use cat. Use awk directly on the file.
try this:
Code:
# RETAILER=JEWL
# cat /tmp/tmp.tmp
WLG 150
JEWL 60
CVS 240
FLN 120
WND 120
# awk -v awkret=$RETAILER '{if($1==awkret) print $2}' /tmp/tmp.tmp
60
For solaris, use nawk instead of awk.
  #6 (permalink)  
Old 07-21-2006
ducatisti ducatisti is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 2
file.txt contents

Yea I know, but I was workin gto explain that the file was being worked through for Pavi's benefit. But thanks. And this will also save a process.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0