Get previous string

Get previous string

am 07.01.2008 09:10:51 von shulamitm

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!

Re: Get previous string

am 07.01.2008 09:52:32 von Cyrus Kriticos

shulamitm wrote:
>
> 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)?

$ STRING="CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes"
$ set -- $STRING
$ echo $3
CSM@csUSo

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Get previous string

am 07.01.2008 10:13:06 von Cyrus Kriticos

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)?
>
> thanks in advance!

#!/bin/sh

STRING="CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes"
set -- $STRING
while [ "$2" != "no" ]; do shift; done
echo $1

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Get previous string

am 07.01.2008 10:14:26 von Vakayil Thobias

"shulamitm" wrote in message
news:4675234b-e854-4527-bdc6-b4e195f717c9@21g2000hsj.googleg roups.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}' | sed 's/^.*Yes //'

Re: Get previous string

am 07.01.2008 10:19:24 von Vakayil Thobias

"Vakayil Thobias" wrote in message
news:1199697272.957524@slbhw0...
>
> "shulamitm" wrote in message
> news:4675234b-e854-4527-bdc6-b4e195f717c9@21g2000hsj.googleg roups.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}' | sed 's/^.*Yes //'
>

echo $x | awk -F"No" '{print $1}' | awk '{print $NF}'

Re: Get previous string

am 07.01.2008 12:52:51 von mik3l3374

On Jan 7, 4:10 pm, 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)?
>
> thanks in advance!


echo $x | awk '{ for(i=1;i<=NF;i++ ) if ($i =="No") print $(i-1) }'

Re: Get previous string

am 07.01.2008 13:31:14 von Maxwell Lol

shulamitm writes:

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

Here's a sed version

sed -n -e 's:\([a-zA-Z@]*\) Yes::g' -e's:\([a-zA-Z@]*\) No:\1:pg'

It works if there is more than one "No" on a line.

Re: Get previous string

am 07.01.2008 13:40:19 von Cyrus Kriticos

Maxwell Lol wrote:
> shulamitm writes:
>
>> CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes
>>
>> I need to get the previous string to "No" string.
>> In this example: "CSM@csUSo"
>>
>
> Here's a sed version
>
> sed -n -e 's:\([a-zA-Z@]*\) Yes::g' -e's:\([a-zA-Z@]*\) No:\1:pg'
sed: command garbled: s:\([a-zA-Z@]*\) No:\1:pg


Add a ; before the last g.


$ uname -a
SunOS unknown 5.10 Generic_118855-33 i86pc i386 i86pc

> It works if there is more than one "No" on a line.

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Get previous string

am 07.01.2008 15:40:42 von Ed Morton

On 1/7/2008 2:10 AM, 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)?
>
> thanks in advance!

awk -v RS=" " '/^No$/{print p}{p=$0}'

Ed.

Re: Get previous string

am 07.01.2008 20:43:57 von joe

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)?

> thanks in advance!

Shulamitm,

Here is how I did it. I wrote this shell script (in /tmp/try) and
called "prev":

vvv
#!/bin/sh

prev=""

echo
for field in `echo $1`; do

if [ "$field" = "No" ]; then

echo "field previous to \"No\" is \"$prev\""
fi

prev="$field"
done
^^^

Here are some examples of execution:

vvv
joe@airlink9:/tmp/try$ ./prev "CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes"

field previous to "No" is "CSM@csUSo"



joe@airlink9:/tmp/try$ ./prev "No CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes"

field previous to "No" is ""
field previous to "No" is "CSM@csUSo"



joe@airlink9:/tmp/try$ ./prev "No CSM@csUCtn Yes CSM@csUSo
No CSM@csUSo Yes Fred
No"

field previous to "No" is ""
field previous to "No" is "CSM@csUSo"
field previous to "No" is "Fred"
^^^

I hope this helps.

-Joe

Re: Get previous string

am 10.01.2008 02:55:35 von cfajohnson

On 2008-01-07, shulamitm wrote:
>
> 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: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence