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 > sed problem: variables countaining special symbols
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

sed problem: variables countaining special symbols

Réponse
 
LinkBack Outils de la discussion
Vieux 21/08/2006, 16h35   #1
Andy B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut sed problem: variables countaining special symbols

Hi Guys,

Wonder if you can , this problem is getting me down.

My script is falling over with a sed problem.

NEWSTORE=JvHCdEMzDegPI
NPASS=EoWH/Q4hds7nA

sed -e '/bill/s/'"${NEWSTORE}"'/'"${NPASS}"'/g' /tmp/temp

The above sed command works fine when there are no "special characters"
but when they appear as above i get a sed: command garbled: error.
Tried a few things with no luck, to escape theses.

Anyone out there that can plz

  Réponse avec citation
Vieux 21/08/2006, 17h09   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed problem: variables countaining special symbols

On 2006-08-21, Andy B wrote:
> Hi Guys,
>
> Wonder if you can , this problem is getting me down.
>
> My script is falling over with a sed problem.
>
> NEWSTORE=JvHCdEMzDegPI
> NPASS=EoWH/Q4hds7nA
>
> sed -e '/bill/s/'"${NEWSTORE}"'/'"${NPASS}"'/g' /tmp/temp
>
> The above sed command works fine when there are no "special characters"
> but when they appear as above i get a sed: command garbled: error.
> Tried a few things with no luck, to escape theses.


That's bacause $NPASS contains a slash. Use a different delimiter
with sed:

sed -e "/bill/s|${NEWSTORE}|${NPASS}|g" /tmp/temp

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
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
Vieux 21/08/2006, 17h14   #3
Xicheng Jia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed problem: variables countaining special symbols

Andy B wrote:
> Hi Guys,
>
> Wonder if you can , this problem is getting me down.
>
> My script is falling over with a sed problem.
>
> NEWSTORE=JvHCdEMzDegPI
> NPASS=EoWH/Q4hds7nA
>
> sed -e '/bill/s/'"${NEWSTORE}"'/'"${NPASS}"'/g' /tmp/temp
>
> The above sed command works fine when there are no "special characters"
> but when they appear as above i get a sed: command garbled: error.
> Tried a few things with no luck, to escape theses.
>


you can change the delimiter of s/// command to some other character
which is not shown in your variables, like at sign, double quotes..

sed -e '/bill/s@'"${NEWSTORE}"'@'"${NPASS}"'@g' /tmp/temp

Xicheng

  Réponse avec citation
Vieux 21/08/2006, 17h20   #4
Jon LaBadie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed problem: variables countaining special symbols

Andy B wrote:
> Hi Guys,
>
> Wonder if you can , this problem is getting me down.
>
> My script is falling over with a sed problem.
>
> NEWSTORE=JvHCdEMzDegPI
> NPASS=EoWH/Q4hds7nA
>
> sed -e '/bill/s/'"${NEWSTORE}"'/'"${NPASS}"'/g' /tmp/temp
>
> The above sed command works fine when there are no "special characters"
> but when they appear as above i get a sed: command garbled: error.
> Tried a few things with no luck, to escape theses.
>
> Anyone out there that can plz
>


Don't use / delimiters for the substitute,
use some character that would not be in the encrypted password charset.
Maybe ':' maybe ',' maybe ?????

Looks like editing a passwd type file.
You might wish to be more specific.
What if you have a bill and billy?

sed -e "/^bill:/s,${NEWSTORE},${NPASS},g" /tmp/temp

And to make sure you are only changing the passwd field:
(maybe there is another JvHCdEMzDegPI on the line )

sed -e "/^bill:/s,^\([^:][^:]*:\)${NEWSTORE},\1${NPASS}," /tmp/temp

Why the global "g"? You expect more than one $NEWSTOR per line?

  Réponse avec citation
Vieux 21/08/2006, 17h26   #5
Andy B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed problem: variables countaining special symbols


Chris F.A. Johnson wrote:
> On 2006-08-21, Andy B wrote:
> > Hi Guys,
> >
> > Wonder if you can , this problem is getting me down.
> >
> > My script is falling over with a sed problem.
> >
> > NEWSTORE=JvHCdEMzDegPI
> > NPASS=EoWH/Q4hds7nA
> >
> > sed -e '/bill/s/'"${NEWSTORE}"'/'"${NPASS}"'/g' /tmp/temp
> >
> > The above sed command works fine when there are no "special characters"
> > but when they appear as above i get a sed: command garbled: error.
> > Tried a few things with no luck, to escape theses.

>
> That's bacause $NPASS contains a slash. Use a different delimiter
> with sed:
>
> sed -e "/bill/s|${NEWSTORE}|${NPASS}|g" /tmp/temp
>
> --
> Chris F.A. Johnson, author <http://cfaj.freeshell.org>
> 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


Hi Chris,

Thanks for the reply.

I would love to do this only problem is that the 2 variables

$NEWSTORE and $NPASS, will be generated from a file that may have
special characters in it ie !"£$%^&* etc, so what i need sed to do is
read the variable in and ignore any special character that are in that
variable.

Is there a way round this at all?

  Réponse avec citation
Vieux 21/08/2006, 18h32   #6
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed problem: variables countaining special symbols

On 2006-08-21, Andy B wrote:
>
> Chris F.A. Johnson wrote:
>> On 2006-08-21, Andy B wrote:
>> > Hi Guys,
>> >
>> > Wonder if you can , this problem is getting me down.
>> >
>> > My script is falling over with a sed problem.
>> >
>> > NEWSTORE=JvHCdEMzDegPI
>> > NPASS=EoWH/Q4hds7nA
>> >
>> > sed -e '/bill/s/'"${NEWSTORE}"'/'"${NPASS}"'/g' /tmp/temp
>> >
>> > The above sed command works fine when there are no "special characters"
>> > but when they appear as above i get a sed: command garbled: error.
>> > Tried a few things with no luck, to escape theses.

>>
>> That's bacause $NPASS contains a slash. Use a different delimiter
>> with sed:
>>
>> sed -e "/bill/s|${NEWSTORE}|${NPASS}|g" /tmp/temp

>
> I would love to do this only problem is that the 2 variables
>
> $NEWSTORE and $NPASS, will be generated from a file that may have
> special characters in it ie !"£$%^&* etc, so what i need sed to do is
> read the variable in and ignore any special character that are in that
> variable.
>
> Is there a way round this at all?


You can escape the slashes within the variables, e.g.:

NEWSTORE=$( printf "%s\n" "$NEWSTORE" | sed 's|/|\\/|g' )


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
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
Vieux 21/08/2006, 22h59   #7
johngnub
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed problem: variables countaining special symbols


Chris F.A. Johnson wrote:
> On 2006-08-21, Andy B wrote:
> >
> > Chris F.A. Johnson wrote:
> >> On 2006-08-21, Andy B wrote:
> >> > Hi Guys,
> >> >
> >> > Wonder if you can , this problem is getting me down.
> >> >
> >> > My script is falling over with a sed problem.
> >> >
> >> > NEWSTORE=JvHCdEMzDegPI
> >> > NPASS=EoWH/Q4hds7nA
> >> >
> >> > sed -e '/bill/s/'"${NEWSTORE}"'/'"${NPASS}"'/g' /tmp/temp
> >> >
> >> > The above sed command works fine when there are no "special characters"
> >> > but when they appear as above i get a sed: command garbled: error.
> >> > Tried a few things with no luck, to escape theses.
> >>
> >> That's bacause $NPASS contains a slash. Use a different delimiter
> >> with sed:
> >>
> >> sed -e "/bill/s|${NEWSTORE}|${NPASS}|g" /tmp/temp

> >
> > I would love to do this only problem is that the 2 variables
> >
> > $NEWSTORE and $NPASS, will be generated from a file that may have
> > special characters in it ie !"£$%^&* etc, so what i need sed to do is
> > read the variable in and ignore any special character that are in that
> > variable.
> >
> > Is there a way round this at all?

>
> You can escape the slashes within the variables, e.g.:
>
> NEWSTORE=$( printf "%s\n" "$NEWSTORE" | sed 's|/|\\/|g' )
>
>
> --
> Chris F.A. Johnson, author <http://cfaj.freeshell.org>
> 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


Not for me to comment on a CFAJ post, but being a lazy admin, I would
chnage the fun chars to some thing safe and simle, tilde, dash, etc. I
used tr, worked on bash, linux. The tr needs to understand the set "[
]". My older AIX was not that smart.
printf "\n%s\n" '!"£$%^&*'|tr '[!"£$%^&*]' "~"
2 cents JB

  Réponse avec citation
Vieux 22/08/2006, 00h23   #8
Jon LaBadie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed problem: variables countaining special symbols

Andy B wrote:
> Chris F.A. Johnson wrote:
>> On 2006-08-21, Andy B wrote:
>>> Hi Guys,
>>>
>>> Wonder if you can , this problem is getting me down.
>>>
>>> My script is falling over with a sed problem.
>>>
>>> NEWSTORE=JvHCdEMzDegPI
>>> NPASS=EoWH/Q4hds7nA
>>>
>>> sed -e '/bill/s/'"${NEWSTORE}"'/'"${NPASS}"'/g' /tmp/temp
>>>
>>> The above sed command works fine when there are no "special characters"
>>> but when they appear as above i get a sed: command garbled: error.
>>> Tried a few things with no luck, to escape theses.

>> That's bacause $NPASS contains a slash. Use a different delimiter
>> with sed:
>>
>> sed -e "/bill/s|${NEWSTORE}|${NPASS}|g" /tmp/temp
>>
>> --
>> Chris F.A. Johnson, author <http://cfaj.freeshell.org>
>> 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

>
> Hi Chris,
>
> Thanks for the reply.
>
> I would love to do this only problem is that the 2 variables
>
> $NEWSTORE and $NPASS, will be generated from a file that may have
> special characters in it ie !"?$%^&* etc, so what i need sed to do is
> read the variable in and ignore any special character that are in that
> variable.
>
> Is there a way round this at all?
>


The sed's I've used also accept control characters, eg. ctrl-G,
as substitute delimiters. Is there a possibility that your variables
will contain control characters too? If not, try a control char,
control X or control G or some such.

sed "/bill/s^X$NEWSTORE^X$NPASS^Xg"
  Réponse avec citation
Vieux 22/08/2006, 07h34   #9
Bill Seivert
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed problem: variables countaining special symbols



Andy B wrote:
> Hi Guys,
>
> Wonder if you can , this problem is getting me down.
>
> My script is falling over with a sed problem.
>
> NEWSTORE=JvHCdEMzDegPI
> NPASS=EoWH/Q4hds7nA
>
> sed -e '/bill/s/'"${NEWSTORE}"'/'"${NPASS}"'/g' /tmp/temp
>
> The above sed command works fine when there are no "special characters"
> but when they appear as above i get a sed: command garbled: error.
> Tried a few things with no luck, to escape theses.
>
> Anyone out there that can plz
>


The quoting is unnecessarily complex. Just use double quotes
around the whole expression:

sed -e "/bill/s/${NEWSTORE}/${NPASS}/g" /tmp/temp

Bill Seivert

  Réponse avec citation
Vieux 22/08/2006, 08h05   #10
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed problem: variables countaining special symbols

On 2006-08-22, Bill Seivert wrote:
>
>
> Andy B wrote:
>> Hi Guys,
>>
>> Wonder if you can , this problem is getting me down.
>>
>> My script is falling over with a sed problem.
>>
>> NEWSTORE=JvHCdEMzDegPI
>> NPASS=EoWH/Q4hds7nA
>>
>> sed -e '/bill/s/'"${NEWSTORE}"'/'"${NPASS}"'/g' /tmp/temp
>>
>> The above sed command works fine when there are no "special characters"
>> but when they appear as above i get a sed: command garbled: error.
>> Tried a few things with no luck, to escape theses.
>>
>> Anyone out there that can plz
>>

>
> The quoting is unnecessarily complex. Just use double quotes
> around the whole expression:
>
> sed -e "/bill/s/${NEWSTORE}/${NPASS}/g" /tmp/temp


That's a good point, but it doesn't , as there is a slash in the
value of one of the variables.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
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
Vieux 25/08/2006, 17h51   #11
Andy B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed problem: variables countaining special symbols

Many thanks for all the guys, really ed.

Chris F.A. Johnson wrote:
> On 2006-08-22, Bill Seivert wrote:
> >
> >
> > Andy B wrote:
> >> Hi Guys,
> >>
> >> Wonder if you can , this problem is getting me down.
> >>
> >> My script is falling over with a sed problem.
> >>
> >> NEWSTORE=JvHCdEMzDegPI
> >> NPASS=EoWH/Q4hds7nA
> >>
> >> sed -e '/bill/s/'"${NEWSTORE}"'/'"${NPASS}"'/g' /tmp/temp
> >>
> >> The above sed command works fine when there are no "special characters"
> >> but when they appear as above i get a sed: command garbled: error.
> >> Tried a few things with no luck, to escape theses.
> >>
> >> Anyone out there that can plz
> >>

> >
> > The quoting is unnecessarily complex. Just use double quotes
> > around the whole expression:
> >
> > sed -e "/bill/s/${NEWSTORE}/${NPASS}/g" /tmp/temp

>
> That's a good point, but it doesn't , as there is a slash in the
> value of one of the variables.
>
> --
> Chris F.A. Johnson, author <http://cfaj.freeshell.org>
> 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 10h56.


É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,19802 seconds with 19 queries