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 > sed help wanted

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-15-04, 14:13
db_Romain db_Romain is offline
Registered User
 
Join Date: Mar 2004
Posts: 6
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!
Reply With Quote
  #2 (permalink)  
Old 03-15-04, 15:40
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
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