Get previous string - Unix

This is a discussion on Get previous string - Unix ; Hello all, I have the following line: CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes I need to get the previous string to "No" string. In this example: "CSM@csUSo" How can I do it (SUN OS)? thanks in advance!...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Get previous string

  1. Get previous string

    Hello all,

    I have the following line:

    CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes

    I need to get the previous string to "No" string. In this example:
    "CSM@csUSo"

    How can I do it (SUN OS)?

    thanks in advance!

  2. Re: Get previous string


    "shulamitm" wrote in message
    news:677a016b-d8c2-4889-a128-eac891c01a71@s19g2000prg.googlegroups.com...
    > Hello all,
    >
    > I have the following line:
    >
    > CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes
    >
    > I need to get the previous string to "No" string. In this example:
    > "CSM@csUSo"
    >
    > How can I do it (SUN OS)?
    >
    > thanks in advance!


    x="CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes"
    echo $x | awk -F"No" '{print $1' | awk '{print $NF}'




  3. Re: Get previous string

    shulamitm writes:

    > I have the following line:
    >
    > CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes
    >
    > I need to get the previous string to "No" string. In this example:
    > "CSM@csUSo"
    >
    > How can I do it (SUN OS)?


    There must be 1001 ways. How about:

    echo "CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes" |\
    sed -e 's/ *No *.*//' | sed -e 's/.* //'

    --
    Ben.

  4. Re: Get previous string

    shulamitm wrote:
    > Hello all,
    >
    > I have the following line:
    >
    > CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes
    >
    > I need to get the previous string to "No" string. In this example:
    > "CSM@csUSo"
    >
    > How can I do it (SUN OS)?


    It's SunOS, not SUN OS.

    But, you can do it this way:

    echo "CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes" |
    fmt -1 |
    sed -e '/^No$/q' |
    tail -2 |
    head -1

    Or another way:

    echo "CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes" |
    fmt -1 |
    awk '$1 == "No" { print p } { p=$1 }'

    There are certainly ways to do it without using "fmt -1" (although
    I like that trick). For example, here's a way that involves defining
    a function:

    foo()
    {
    for token in "$@"
    do
    if [ x"$token" = x"No" ]
    then
    echo "$prev"
    return
    fi

    prev="$token"
    done
    }

    x="CSM@csUCtn Yes CSM@csiiUSo No CSM@csUSo Yes"
    foo $x

    Hope that helps.

    - Logan

  5. Re: Get previous string

    On 2008-01-07, shulamitm wrote:
    > Hello all,
    >
    > I have the following line:
    >
    > CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes
    >
    > I need to get the previous string to "No" string. In this example:
    > "CSM@csUSo"
    >
    > How can I do it (SUN OS)?


    In any POSIX shell, without using any external commands:

    line="CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes"
    set -f
    set -- ${line%% No *}
    shift $(( $# - 1 ))
    previous=$1

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

+ Reply to Thread