Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2003
    Posts
    106

    Unanswered: Shell script pattern matching

    We are writing a shell script for installing a website on linux/solaris machine.

    Since we donno shell script we are struggling a lot,and no proper persons to help us here. Our requirement is

    Assume I have a file say sachi.txt

    It has

    Abc
    Ssd
    Sds
    /opt="/opt/sourcecode"
    sds

    Now we have a shell script which has

    Read a

    And assume a=sourcecode
    Now we need to search sachi.txt for sourcecode pattern and get /opt/sourcecode in a shell variable say $res

    You send us the best possible method to do this.
    Sachi

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Depending on what you want to get into the variable :
    Code:
    # Get line /opt="/opt/sourcecode"
    res=`grep "$a"  sachi.txt
    
    # Get "/opt/sourcecode"
    res=`awk -F'=' '$0 ~ Pattern { print $2 }' Pattern="$a"`
    
    # Get /opt/sourcecode
    res=`awk -F'"' '$0 ~ Pattern { print $2 }' Pattern="$a"`
    Jean-Pierre.

  3. #3
    Join Date
    Jan 2003
    Posts
    106

    we tried what u said, but

    the first line

    res=`grep "$a" sachi.txt

    worked fine.

    but the second stmt
    res=`awk -F'=' '$0 ~ Pattern { print $2 }' Pattern="$a"`

    did not work
    when i execute it,it waits for user input and does nothing

    and you are not using the res variable which has the search string in the second statement at all.

    i think it should be
    res=`awk -F'=' '$0 ~ Pattern { print $2 }' Pattern="$res"`

    but how do i solve the "Waiting for user input problem"
    Sachi

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    I forgot the input file for awk commands :
    Code:
    # Get line /opt="/opt/sourcecode"
    res=`grep "$a"  sachi.txt
    
    # Get "/opt/sourcecode"
    res=`awk -F'=' '$0 ~ Pattern { print $2 }' Pattern="$a" sachi.txt`
    
    # Get /opt/sourcecode
    res=`awk -F'"' '$0 ~ Pattern { print $2 }' Pattern="$a" sachi.txt`
    These are 3 independant commands, you chooses one depending on the result you need (third i think).
    Jean-Pierre.

Posting Permissions

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