|
|
|
|
|
|
| Author |
Message |
pawan_test *nix forums beginner
Joined: 04 Oct 2005
Posts: 38
|
Posted: Thu Jul 20, 2006 9:35 pm Post subject:
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. |
|
| Back to top |
|
 |
Chris F.A. Johnson *nix forums Guru
Joined: 20 Feb 2005
Posts: 2268
|
Posted: Thu Jul 20, 2006 10:19 pm Post subject:
Re: Lookup with a file
|
|
|
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
|
Posted: Fri Jul 21, 2006 2:07 am Post subject:
Re: Lookup with a file
|
|
|
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
|
Posted: Fri Jul 21, 2006 4:23 am Post subject:
Re: Lookup with a file
|
|
|
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 |
|
 |
|
|
The time now is Sun Nov 23, 2008 1:42 pm | All times are GMT
|
|
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
|
|