Home > Archive > Unix Programming > July 2006 > Lookup with a file
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 with a file
|
|
| pawan_test 2006-07-20, 7:00 pm |
| 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.
| |
| Bit Twister 2006-07-20, 7:00 pm |
| On 20 Jul 2006 14:33:44 -0700, 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.
You can use a for loop to read the file, set to parse the line, check
for match, ......
Although it's bash this might help
http://tldp.org/LDP/abs/html/index.html
| |
| Pascal Bourguignon 2006-07-20, 7:00 pm |
| "pawan_test" <sridhara007@gmail.com> writes:
> 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)
You don't need anything specificly korn, you can do that in normal sh.
AFAIK, this should work as well in sh, bash, ksh or similar:
while read k v ; do
if [ "$k" = "$RETAILER" ] ; then echo "$v" ; fi
done < file.txt
Oops, sorry, I used test(1) and echo(1).
Well, I assume in ksh they're built-ins as in bash...
Would:
#!/bin/ksh
awk "/^$RETAILER /"'{print $2}'<file.txt
have been accepted?
--
__Pascal Bourguignon__ http://www.informatimago.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d? s++:++ a+ C+++ UL++++ P--- L+++ E+++ W++ N+++ o-- K- w---
O- M++ V PS PE++ Y++ PGP t+ 5+ X++ R !tv b+++ DI++++ D++
G e+++ h+ r-- z?
------END GEEK CODE BLOCK------
| |
| Chris F.A. Johnson 2006-07-20, 7:00 pm |
| On 2006-07-20, Pascal Bourguignon wrote:
> "pawan_test" <sridhara007@gmail.com> writes:
To pawan_test: please do not multi-post. If you feel an article
belongs in more than one group, corss-post (i.e., put all the
groups, but not more than 3, on the Newsgroups: line).
Oh... you're using Google Groups. Bad idea.
>
> You don't need anything specificly korn, you can do that in normal sh.
> AFAIK, this should work as well in sh, bash, ksh or similar:
>
> while read k v ; do
> if [ "$k" = "$RETAILER" ] ; then echo "$v" ; fi
> done < file.txt
>
>
> Oops, sorry, I used test(1) and echo(1).
> Well, I assume in ksh they're built-ins as in bash...
They are built into all Bourne-type shells.
> Would:
>
> #!/bin/ksh
> awk "/^$RETAILER /"'{print $2}'<file.txt
>
> have been accepted?
awk "/^$RETAILER / {print \$2}" file.txt
Awk is slow for searches; grep is faster on a large file.
--
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
| |
| Pascal Bourguignon 2006-07-20, 7:00 pm |
| "Chris F.A. Johnson" <cfajohnson@gmail.com> writes:
> awk "/^$RETAILER / {print \$2}" file.txt
>
> Awk is slow for searches; grep is faster on a large file.
And this is important how?
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Klingon function calls do not have "parameters" -- they have
"arguments" and they ALWAYS WIN THEM."
| |
| Chris F.A. Johnson 2006-07-20, 7:00 pm |
| On 2006-07-20, Pascal Bourguignon wrote:
> "Chris F.A. Johnson" <cfajohnson@gmail.com> writes:
>
> And this is important how?
Because people do not like waiting for a command to execute.
With a single instance, the difference is not great, but snippets
such as these are liable to be incorporated into larger scripts in
which it will make a difference.
By coding efficiently from the start, making large scripts
responsive instead of sluggish is much easier.
--
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
|
|
|
|
|