Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2004
    Posts
    45

    Unanswered: 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.

  2. #2
    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

  3. #3
    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

  4. #4
    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?

  5. #5
    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

  6. #6
    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

  7. #7
    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}'

  8. #8
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    or even...

    awk '$3==var {print $1}' var=e configFile

  9. #9
    Join Date
    Jul 2004
    Posts
    45
    Guys,
    Thank you very much for your solutions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •