niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
Lookup with a file
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
pawan_test
*nix forums beginner


Joined: 04 Oct 2005
Posts: 38

PostPosted: Thu Jul 20, 2006 9:35 pm    Post subject: Lookup with a file Reply with quote

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.
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Thu Jul 20, 2006 10:19 pm    Post subject: Re: Lookup with a file Reply with quote

On 2006-07-20, pawan_test wrote:
Quote:
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.

grep "$RETAILER" file.txt | cut -d ' ' -f2

Or, if you need the result in a variable:

num=$( grep "$RETAILER" file.txt )
num=${num##* }

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Back to top
pawan_test
*nix forums beginner


Joined: 04 Oct 2005
Posts: 38

PostPosted: Fri Jul 21, 2006 2:07 am    Post subject: Re: Lookup with a file Reply with quote

Thanks a lot John

Pavi
Chris F.A. Johnson wrote:
Quote:
On 2006-07-20, pawan_test wrote:
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.

grep "$RETAILER" file.txt | cut -d ' ' -f2

Or, if you need the result in a variable:

num=$( grep "$RETAILER" file.txt )
num=${num##* }

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Back to top
Ed Morton
*nix forums Guru


Joined: 20 Feb 2005
Posts: 1073

PostPosted: Fri Jul 21, 2006 4:23 am    Post subject: Re: Lookup with a file Reply with quote

pawan_test wrote:
Quote:
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.


awk -v t="$RETAILER" '$1 == t { print $2 }' file.txt

Ed.
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 1:42 pm | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Running php file everyday on scheduled time sachin PHP 1 Fri Jul 21, 2006 12:49 pm
No new posts Regarding thesaurus iso file Srikanth modules 0 Fri Jul 21, 2006 10:42 am
No new posts how can i get a file descriptor not used? mars system 0 Fri Jul 21, 2006 7:41 am
No new posts small GTK "Open file" dialog David Siroky Debian 0 Fri Jul 21, 2006 7:30 am
No new posts Trouble Declaring 3D Array in Header File free2klim C++ 1 Fri Jul 21, 2006 4:07 am

Mortgage | Mobile Phones | Mortgage Loans | Mortgage Calculator | Credit Card
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.3095s ][ Queries: 16 (0.2235s) ][ GZIP on - Debug on ]