|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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: |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. > |
|
![]() |
| Outils de la discussion | |
|
|