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 > Replace multiple occurrences of a word with a single one
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Replace multiple occurrences of a word with a single one

Réponse
 
LinkBack Outils de la discussion
Vieux 04/09/2007, 15h06   #1
Sashi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Replace multiple occurrences of a word with a single one

Hi all, I'm trying to use sed to replace multiple occurrences of a
word with a single one.

I have a line as follows:

..*Comment:Comment:Comment:.* where the .* is text that is irrelevant
to this post.

I want to replace the multiple occurrences of "Comment:" with a single
"Comment:".

I can't do it within Vim 7.0. Apparently the quantifiers apply to
repetitions of characters and not words.
So :%s/Comment:\{2,}/Comment:/ doesn't work in Vim.

Er, yes, I know this is a shell forum but I'm just posting what I
tried.

I tried it in sed but I'm a sed newbie and the command 's/Comment://2'
will delete only the second occurrence. I couldn't find out how to
delete all the occurrences from the second one onwards.

Can someone out with the right command using sed, vim or awk (in
that order?)

Thanks,
Sashi

  Réponse avec citation
Vieux 04/09/2007, 15h20   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Replace multiple occurrences of a word with a single one

Sashi wrote:
> Hi all, I'm trying to use sed to replace multiple occurrences of a
> word with a single one.
>
> I have a line as follows:
>
> .*Comment:Comment:Comment:.* where the .* is text that is irrelevant
> to this post.
>
> I want to replace the multiple occurrences of "Comment:" with a single
> "Comment:".

<snip>

$ echo ".*Comment:Comment:Comment:.*" |
sed 's/Comment:\(Comment:\)*/Comment:/'
..*Comment:.*

Regards,

Ed.
  Réponse avec citation
Vieux 04/09/2007, 15h53   #3
Sashi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Replace multiple occurrences of a word with a single one

On Sep 4, 10:20 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> <snip>
>
> $ echo ".*Comment:Comment:Comment:.*" |
> sed 's/Comment:\(Comment:\)*/Comment:/'
> .*Comment:.*
>
> Regards,
>
> Ed.


Thank you, Ed.
I'm using sed on Solaris 10 and it didn't work with default /usr/bin/
sed but worked with /usr/xpg4/bin/sed.

Regards,
Sashi

  Réponse avec citation
Vieux 04/09/2007, 18h10   #4
hyperboogie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Replace multiple occurrences of a word with a single one

On Sep 4, 5:06 pm, Sashi <small...@gmail.com> wrote:
> Hi all, I'm trying to use sed to replace multiple occurrences of a
> word with a single one.
>
> I have a line as follows:
>
> .*Comment:Comment:Comment:.* where the .* is text that is irrelevant
> to this post.
>
> I want to replace the multiple occurrences of "Comment:" with a single
> "Comment:".
>
> I can't do it within Vim 7.0. Apparently the quantifiers apply to
> repetitions of characters and not words.
> So :%s/Comment:\{2,}/Comment:/ doesn't work in Vim.
>
> Er, yes, I know this is a shell forum but I'm just posting what I
> tried.
>
> I tried it in sed but I'm a sed newbie and the command 's/Comment://2'
> will delete only the second occurrence. I couldn't find out how to
> delete all the occurrences from the second one onwards.
>
> Can someone out with the right command using sed, vim or awk (in
> that order?)
>
> Thanks,
> Sashi


This works just fine ... :-)
>echo 'Comment:Comment:Comment:' | sed 's/\(Comment:\)\{1,\}/\1/'

Comment:

  Réponse avec citation
Vieux 05/09/2007, 09h15   #5
Rakesh Sharma
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Replace multiple occurrences of a word with a single one

The reason this does not work with /bin/sed is that that version of
'sed' doesn't support
a multi-atom RE followed by \{m,n\} . The GNU version of 'sed' can
handle that though.

In order to do it with the /bin/sed version you would have to iterate,
for example:

echo '.....' |
sed -e '
:a
s/\(Comment:\)\1/\1/
ta
'



On Sep 4, 7:06 pm, Sashi <small...@gmail.com> wrote:
> I have a line as follows:
>
> .*Comment:Comment:Comment:.* where the .* is text that is irrelevant
> to this post.
>
> I want to replace the multiple occurrences of "Comment:" with a single
> "Comment:".
>
> I can't do it within Vim 7.0. Apparently the quantifiers apply to
> repetitions of characters and not words.
> So :%s/Comment:\{2,}/Comment:/ doesn't work in Vim.
>


  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 09h26.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12891 seconds with 13 queries