PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Get previous string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Get previous string

Réponse
 
LinkBack Outils de la discussion
Vieux 07/01/2008, 08h10   #1
shulamitm
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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!
  Réponse avec citation
Vieux 07/01/2008, 08h52   #2
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get previous string

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.
  Réponse avec citation
Vieux 07/01/2008, 09h13   #3
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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)?
>
> 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.
  Réponse avec citation
Vieux 07/01/2008, 09h14   #4
Vakayil Thobias
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get previous string


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





  Réponse avec citation
Vieux 07/01/2008, 09h19   #5
Vakayil Thobias
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get previous string


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


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



  Réponse avec citation
Vieux 07/01/2008, 11h52   #6
mik3l3374@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get previous string

On Jan 7, 4:10 pm, shulamitm <shulami...@bezeq.com> 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) }'
  Réponse avec citation
Vieux 07/01/2008, 12h31   #7
Maxwell Lol
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get previous string

shulamitm <shulamitmi@bezeq.com> 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.
  Réponse avec citation
Vieux 07/01/2008, 12h40   #8
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get previous string

Maxwell Lol wrote:
> shulamitm <shulamitmi@bezeq.com> 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.
  Réponse avec citation
Vieux 07/01/2008, 14h40   #9
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get previous string



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.

  Réponse avec citation
Vieux 07/01/2008, 19h43   #10
Joseph H. Rosevear
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get previous string

shulamitm <shulamitmi@bezeq.com> 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 s.

-Joe
  Réponse avec citation
Vieux 10/01/2008, 01h55   #11
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Get previous string

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 <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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h22.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,21143 seconds with 19 queries