Results 1 to 2 of 2

Thread: sed help wanted

  1. #1
    Join Date
    Mar 2004
    Posts
    6

    Unanswered: sed help wanted

    Hi,i have a little sed problem in my BASH script
    Here's the deal:

    from the input string :"SELECT Name WHERE Stock >=15 AND Price<=10", i would like to transform that to :
    "$4 >=15 && $3 <=10" and put the result in a variable for further use in a awk . 3 and 4 are the values of previously declared variables STOCK=4 , PRICE=3

    here's my sed:
    CONDITION=`_s_upper_case "$2"`

    CONDITION=`echo "$CONDITION" | sed -e 's/\(^.*WHERE \)//' |\
    sed -e 's/AND/\&\&/g'| sed -e 's/OR/\|\|/g' | sed -e "s/\([A-Z]+\)/$$&/g"`

    I try to insert \$$ before each character string but it doesn't work,
    I understand there must some use of eval to replace STOCk and PRICE
    by their numerical value but i can't figure it out

    Any Help would be greatly appreciated!

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    You can do something like this :

    Code:
    STOCK=4
    PRICE=3
    Statement="SELECT Name WHERE Stock >= 15 AND PRICE <= 10"
    Condition=$(echo $Statement             | \
                tr '[a-z]' '[A-Z]'          | \
                sed -e 's/^.*WHERE //'        \
                    -e 's/ AND / \&\& /g'     \
                    -e 's/ OR / || /g'        \
                    -e 's/\([A-Z]\+\)/\\\$\$&/g')
    eval Condition=\"$Condition\"
    echo $Condition
    Result: $4 >= 15 && $3 <= 10


    I'am not sure that SED is the right solution to analyze the request.
    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
  •