Results 1 to 4 of 4

Thread: sed help wanted

  1. #1
    Join Date
    Mar 2004
    Posts
    6

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

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    The following 'sed' command transform =="..." en ~/.../
    Code:
    sed -e 's/=="\([^"]*\)"/~\/\1\//'
    Jean-Pierre.

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

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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.

Posting Permissions

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