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-21-04, 15:03
db_Romain db_Romain is offline
Registered User
 
Join Date: Mar 2004
Posts: 6
sed help wanted

Hi,
This post follows the one i posted a few days ago and which was magnificently answered by aigles

I would like to extend my request script .for that i would like it to search character strings in addition to integers.
The variables (for example) have these values:
ID=1
DESIGNATION=2
PRICE=3
STOCK=4
and correspond to the name and field number of the file from which are made the requests

i would like to transform condition part the command line request
"WHERE Designation==Sav" to:
$2 ~ /Sav/ ,maybe with a sed for further use as an if condition in an awk

Here's my actual sed:
CONDITION=$(echo $2 | \
tr '[a-z]' '[A-Z]' | \
sed -e 's/^.*WHERE //' \
-e 's/[ \t]AND[ \t]/ \&\& /g' \
-e 's/[ \t]OR[ \t]/ || /g' \
-e 's/\([A-Z_]\+\)/\\\$\$&/g' \
-e 's/ //g')
eval CONDITION=\"$CONDITION\"

For example with the condition request:
WHERE Designation==Sav AND Stock>10, i would like it to become:
$2 ~ /Sav/ && $3>10

I would only like to transform it what's after == is a character string:
i don't want it to mess with Stock==10 which becomes $3==10

Any Help would be hugely appreciated
Reply With Quote
  #2 (permalink)  
Old 03-22-04, 02:40
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
The following 'sed' command transform =="..." en ~/.../
Code:
sed -e 's/=="\([^"]*\)"/~\/\1\//'
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 03-22-04, 11:22
db_Romain db_Romain is offline
Registered User
 
Join Date: Mar 2004
Posts: 6
Thanks but i found my way out !
just out of curiosity ,would you happen to know an efficient way to test if a $ is immediately followed by an integer in a character string?

i made it work but it seems ugly...

Thanks anyways
Reply With Quote
  #4 (permalink)  
Old 03-22-04, 12:26
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
To test if the variable STRING contains $n where n is a number :

Ksh (not sure that the syntax is valid with bash)

if [[ "$STRING" = *\+([0-9])* ]] ; then
echo "STRING contains $n"
fi

All shells, with expr :

if expr "$STRING" : '.*\$[0-9]+' > /dev/null ; then
echo "STRING contains $n"
fi

If you want to extract $n or n (the first ones in case of multiple $n)

field=$(echo "$STRING" | sed 's/.*\(\$[0-9]+\)/\1/)
number=$(echo "$STRING" | sed 's/.*\$\([0-9]+\)/\1/)
__________________
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