Get previous string

Hello all,

I have the following line:

CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] csUSo Yes

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

How can I do it (SUN OS)?

thanks in advance!
shulamitm [ Mo, 07 Januar 2008 09:10 ] [ ID #1901406 ]

Re: Get previous string

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

$ STRING="CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] csUSo Yes"
$ set -- $STRING
$ echo $3
CSM [at] csUSo

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
Cyrus Kriticos [ Mo, 07 Januar 2008 09:52 ] [ ID #1901408 ]

Re: Get previous string

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

#!/bin/sh

STRING="CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] 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.
Cyrus Kriticos [ Mo, 07 Januar 2008 10:13 ] [ ID #1901409 ]

Re: Get previous string

"shulamitm" <shulamitmi [at] bezeq.com> wrote in message
news:4675234b-e854-4527-bdc6-b4e195f717c9 [at] 21g2000hsj.googleg roups.com...
> Hello all,
>
> I have the following line:
>
> CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] csUSo Yes
>
> I need to get the previous string to "No" string.
> In this example: "CSM [at] csUSo"
>
> How can I do it (SUN OS)?
>
> thanks in advance!

x="CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] csUSo Yes"

echo $x | awk -F"No" '{print $1}' | sed 's/^.*Yes //'
Vakayil Thobias [ Mo, 07 Januar 2008 10:14 ] [ ID #1901410 ]

Re: Get previous string

"Vakayil Thobias" <vakayil.thobias [at] alcatel-lucent.com> wrote in message
news:1199697272.957524 [at] slbhw0...
>
> "shulamitm" <shulamitmi [at] bezeq.com> wrote in message
> news:4675234b-e854-4527-bdc6-b4e195f717c9 [at] 21g2000hsj.googleg roups.com...
>> Hello all,
>>
>> I have the following line:
>>
>> CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] csUSo Yes
>>
>> I need to get the previous string to "No" string.
>> In this example: "CSM [at] csUSo"
>>
>> How can I do it (SUN OS)?
>>
>> thanks in advance!
>
> x="CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] csUSo Yes"
>
> echo $x | awk -F"No" '{print $1}' | sed 's/^.*Yes //'
>

echo $x | awk -F"No" '{print $1}' | awk '{print $NF}'
Vakayil Thobias [ Mo, 07 Januar 2008 10:19 ] [ ID #1901411 ]

Re: Get previous string

On Jan 7, 4:10 pm, shulamitm <shulami... [at] bezeq.com> wrote:
> Hello all,
>
> I have the following line:
>
> CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] csUSo Yes
>
> I need to get the previous string to "No" string.
> In this example: "CSM [at] 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) }'
mik3l3374 [ Mo, 07 Januar 2008 12:52 ] [ ID #1901416 ]

Re: Get previous string

shulamitm <shulamitmi [at] bezeq.com> writes:

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

Here's a sed version

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

It works if there is more than one "No" on a line.
Maxwell Lol [ Mo, 07 Januar 2008 13:31 ] [ ID #1901420 ]

Re: Get previous string

Maxwell Lol wrote:
> shulamitm <shulamitmi [at] bezeq.com> writes:
>
>> CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] csUSo Yes
>>
>> I need to get the previous string to "No" string.
>> In this example: "CSM [at] csUSo"
>>
>
> Here's a sed version
>
> sed -n -e 's:\([a-zA-Z [at] ]*\) Yes::g' -e's:\([a-zA-Z [at] ]*\) No:\1:pg'
sed: command garbled: s:\([a-zA-Z [at] ]*\) 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.
Cyrus Kriticos [ Mo, 07 Januar 2008 13:40 ] [ ID #1901421 ]

Re: Get previous string

On 1/7/2008 2:10 AM, shulamitm wrote:
> Hello all,
>
> I have the following line:
>
> CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] csUSo Yes
>
> I need to get the previous string to "No" string.
> In this example: "CSM [at] csUSo"
>
> How can I do it (SUN OS)?
>
> thanks in advance!

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

Ed.
Ed Morton [ Mo, 07 Januar 2008 15:40 ] [ ID #1901425 ]

Re: Get previous string

shulamitm <shulamitmi [at] bezeq.com> wrote:
> Hello all,

> I have the following line:

> CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] csUSo Yes

> I need to get the previous string to "No" string.
> In this example: "CSM [at] 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 [at] airlink9:/tmp/try$ ./prev "CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] csUSo Yes"

field previous to "No" is "CSM [at] csUSo"



joe [at] airlink9:/tmp/try$ ./prev "No CSM [at] csUCtn Yes CSM [at] csUSo No CSM [at] csUSo Yes"

field previous to "No" is ""
field previous to "No" is "CSM [at] csUSo"



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

field previous to "No" is ""
field previous to "No" is "CSM [at] csUSo"
field previous to "No" is "Fred"
^^^

I hope this helps.

-Joe
joe [ Mo, 07 Januar 2008 20:43 ] [ ID #1901439 ]

Re: Get previous string

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

In any POSIX shell, without using any external commands:

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


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
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
cfajohnson [ Do, 10 Januar 2008 02:55 ] [ ID #1904169 ]
Linux » comp.unix.shell » Get previous string

Vorheriges Thema: How To Turn Off Auto Tab Complete?
Nächstes Thema: help with a script change