If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > get the value corresponding to the pattern in a line

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-14-04, 11:04
deebee deebee is offline
Registered User
 
Join Date: Jul 2004
Posts: 45
get the value corresponding to the pattern in a line

1 a b c
2 d e f
3 g h i

I have the above configuration file. What I need is when I enter a letter (between a to i ) script should return the corresponding line no. or digit that assigned to the line. For eg. if I enter letter 'e', I should get 2.
let me know if have the answer.
thanks.
Reply With Quote
  #2 (permalink)  
Old 07-14-04, 11:30
mvillan mvillan is offline
Registered User
 
Join Date: Dec 2003
Location: Ogden Utah
Posts: 34
hello

1 a b c
2 d e f
3 g h i

So you have a file that stores the pattern above and you want to type e on the command line and then have the number two sent to your screen?
__________________
mvilla
Reply With Quote
  #3 (permalink)  
Old 07-14-04, 11:33
deebee deebee is offline
Registered User
 
Join Date: Jul 2004
Posts: 45
Quote:
Originally Posted by mvillan
1 a b c
2 d e f
3 g h i

So you have a file that stores the pattern above and you want to type e on the command line and then have the number two sent to your screen?
Exactly! if my shell file name is myShell.ksh then command would be
myShell.ksh e confg.txt

thanks
Reply With Quote
  #4 (permalink)  
Old 07-14-04, 14:28
deebee deebee is offline
Registered User
 
Join Date: Jul 2004
Posts: 45
I guess I found it myself I didnt realise it was so easy
grep $inputChar $cfgfile | cut -c1
any better suggestion?
Reply With Quote
  #5 (permalink)  
Old 07-16-04, 01:20
srsjc srsjc is offline
Registered User
 
Join Date: Jun 2004
Posts: 20
What I need is when I enter a letter (between a to i ) script should return the corresponding line no

grep -n <pattern> <filename> |cut -d ":" -f1


Jc
Reply With Quote
  #6 (permalink)  
Old 07-16-04, 01:25
srsjc srsjc is offline
Registered User
 
Join Date: Jun 2004
Posts: 20
Quote:
Originally Posted by deebee
I guess I found it myself I didnt realise it was so easy
grep $inputChar $cfgfile | cut -c1
any better suggestion?
The problem in the above is if the file is of the format

1 a w e
2 s d g
...
...
..
9 w d f
10 a w e

If you execute it will return 1 and 1 but not 1 and 10
Reply With Quote
  #7 (permalink)  
Old 07-16-04, 03:49
SiverSloth SiverSloth is offline
Registered User
 
Join Date: Jun 2004
Posts: 29
use awk

Rather than using cut use awk '{print $1}' so it becomes

grep <patern> <file> | awk '{print $1}'
Reply With Quote
  #8 (permalink)  
Old 07-16-04, 04:17
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
or even...

awk '$3==var {print $1}' var=e configFile
Reply With Quote
  #9 (permalink)  
Old 07-16-04, 10:29
deebee deebee is offline
Registered User
 
Join Date: Jul 2004
Posts: 45
Guys,
Thank you very much for your solutions.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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