sed scripting

Hi all,

I know this might not be appropriate for this forum, but i'm hoping
someone might be able to help.

I'm trying to replace instances of a word in a file using sed.

The task is easy enough, using
$ sed 's/originalstring/newstring/' file

However, I want to ignore the first instance of (in this case)
originalstring, and replace the rest of the occurrences.

Is this possible?

Could someone shed some light upon my quest?
Bec [ Do, 15 November 2007 01:25 ] [ ID #1871019 ]

Re: sed scripting

Ivan <find.i... [at] gmail.com> wrote:
> I know this might not be appropriate for this forum,
> but i'm hoping someone might be able to help.

It's appropriate. You may eventually wish to look
through the "Seders" archive, and its "Seders' Grab
Bag" Web frontend at:

"The Seder's Grab Bag": (not currently accessible)
http://sed.sourceforge.net/grabbag/scripts/
http://sed.sourceforge.net/grabbag/seders/
http://groups.yahoo.com/group/sed-users/

> I'm trying to replace instances of a word in a
> file using sed. The task is easy enough, using:
> $ sed 's/originalstring/newstring/' file
> However, I want to ignore the first instance of (in
> this case) originalstring, and replace the rest of
> the occurrences.

What, ignore the first instance in the sentence, or the
file? The two cases require very different solutions.

> Is this possible?

Anything is possible -- I've seen sed(1) a script do
arbitrary-precision floating-point arithmetric. The
question is, is it worth the extra effort to create the
solution?

> Could someone shed some light upon my quest?

"That which can be overthought, will be."
-- Sog's First Law Of Technologists.

=Brian
brian_hiles [ Do, 15 November 2007 02:54 ] [ ID #1871020 ]

Re: sed scripting

On Nov 15, 12:54 pm, bsh <brian_hi... [at] rocketmail.com> wrote:
> Ivan <find.i... [at] gmail.com> wrote:
> > I know this might not be appropriate for this forum,
> > but i'm hoping someone might be able to help.
>
> It's appropriate. You may eventually wish to look
> through the "Seders" archive, and its "Seders' Grab
> Bag" Web frontend at:
>
> "The Seder's Grab Bag": (not currently accessible)http://sed.sourceforge.net/grabbag/scripts/http:/ /sed.sourceforge.net/grabbag/seders/http://groups.yahoo.com/ group/sed-users/
>
> > I'm trying to replace instances of a word in a
> > file using sed. The task is easy enough, using:
> > $ sed 's/originalstring/newstring/' file
> > However, I want to ignore the first instance of (in
> > this case) originalstring, and replace the rest of
> > the occurrences.
>
> What, ignore the first instance in the sentence, or the
> file? The two cases require very different solutions.
>
> > Is this possible?
>
> Anything is possible -- I've seen sed(1) a script do
> arbitrary-precision floating-point arithmetric. The
> question is, is it worth the extra effort to create the
> solution?
>
> > Could someone shed some light upon my quest?
>
> "That which can be overthought, will be."
> -- Sog's First Law Of Technologists.
>
> =Brian


Here is what I mean:
On my svn server, the file where the user name and password to each
repo is located under a file called 'passwd'.

if I want to comment out Michael's access, I can use the command:
# sed 's/michael/#michael/' passwd

This will comment every instance of michael in the file.

How do I get sed to skip instances of michael?
Bec [ Do, 15 November 2007 05:14 ] [ ID #1871021 ]

Re: sed scripting

On 11/14/2007 6:25 PM, Ivan wrote:
> Hi all,
>
> I know this might not be appropriate for this forum, but i'm hoping
> someone might be able to help.
>
> I'm trying to replace instances of a word in a file using sed.
>
> The task is easy enough, using
> $ sed 's/originalstring/newstring/' file
>
> However, I want to ignore the first instance of (in this case)
> originalstring, and replace the rest of the occurrences.
>
> Is this possible?
>
> Could someone shed some light upon my quest?
>

For anything other than simple substitutions, use awk or perl or ruby or...

In this case, this'll do what you want:

awk 'found{sub(/originalstring/,"newstring")} /originalstring/{found=1} 1' file

or, if you prefer "cute" solutions:

awk 'BEGIN{o=n="originalstring"} sub(o,n){n="newstring"} 1' file

The "1" at the end is a true constant condition which invokes awks default
action of printing the current line.

Regards,

Ed.
Ed Morton [ Do, 15 November 2007 05:23 ] [ ID #1871022 ]

Re: sed scripting

Ivan <find.ivan [at] gmail.com> writes:

> The task is easy enough, using
> $ sed 's/originalstring/newstring/' file
>
> However, I want to ignore the first instance of (in this case)
> originalstring, and replace the rest of the occurrences.

sed 's/originalstring/newstring/2g' file
Maxwell Lol [ Do, 15 November 2007 13:33 ] [ ID #1871024 ]

Re: sed scripting

On Nov 15, 3:23 pm, Ed Morton <mor... [at] lsupcaemnt.com> wrote:
> On 11/14/2007 6:25 PM, Ivan wrote:
>
>
>
> > Hi all,
>
> > I know this might not be appropriate for this forum, but i'm hoping
> > someone might be able to help.
>
> > I'm trying to replace instances of a word in a file using sed.
>
> > The task is easy enough, using
> > $ sed 's/originalstring/newstring/' file
>
> > However, I want to ignore the first instance of (in this case)
> > originalstring, and replace the rest of the occurrences.
>
> > Is this possible?
>
> > Could someone shed some light upon my quest?
>
> For anything other than simple substitutions, use awk or perl or ruby or...
>
> In this case, this'll do what you want:
>
> awk 'found{sub(/originalstring/,"newstring")} /originalstring/{found=1} 1' file
>
> or, if you prefer "cute" solutions:
>
> awk 'BEGIN{o=n="originalstring"} sub(o,n){n="newstring"} 1' file


Yeah that works - cheers.
I don't suppose you know how to suppress the printing to screen?
>
> The "1" at the end is a true constant condition which invokes awks default
> action of printing the current line.
>
> Regards,
>
> Ed.
Bec [ Fr, 16 November 2007 02:05 ] [ ID #1872086 ]

Re: sed scripting

On 11/15/2007 7:05 PM, Ivan wrote:
> On Nov 15, 3:23 pm, Ed Morton <mor... [at] lsupcaemnt.com> wrote:
>
>>On 11/14/2007 6:25 PM, Ivan wrote:
>>
>>
>>
>>
>>>Hi all,
>>
>>>I know this might not be appropriate for this forum, but i'm hoping
>>>someone might be able to help.
>>
>>>I'm trying to replace instances of a word in a file using sed.
>>
>>>The task is easy enough, using
>>>$ sed 's/originalstring/newstring/' file
>>
>>>However, I want to ignore the first instance of (in this case)
>>>originalstring, and replace the rest of the occurrences.
>>
>>>Is this possible?
>>
>>>Could someone shed some light upon my quest?
>>
>>For anything other than simple substitutions, use awk or perl or ruby or...
>>
>>In this case, this'll do what you want:
>>
>>awk 'found{sub(/originalstring/,"newstring")} /originalstring/{found=1} 1' file
>>
>>or, if you prefer "cute" solutions:
>>
>>awk 'BEGIN{o=n="originalstring"} sub(o,n){n="newstring"} 1' file
>
>
>
> Yeah that works - cheers.
> I don't suppose you know how to suppress the printing to screen?

Yes - don't run the script. Sorry, but could you explain what it is you actually
want to do?

Ed.
Ed Morton [ Fr, 16 November 2007 02:25 ] [ ID #1872087 ]

Re: sed scripting

On Nov 16, 12:25 pm, Ed Morton <mor... [at] lsupcaemnt.com> wrote:
> On 11/15/2007 7:05 PM, Ivan wrote:
>
>
>
> > On Nov 15, 3:23 pm, Ed Morton <mor... [at] lsupcaemnt.com> wrote:
>
> >>On 11/14/2007 6:25 PM, Ivan wrote:
>
> >>>Hi all,
>
> >>>I know this might not be appropriate for this forum, but i'm hoping
> >>>someone might be able to help.
>
> >>>I'm trying to replace instances of a word in a file using sed.
>
> >>>The task is easy enough, using
> >>>$ sed 's/originalstring/newstring/' file
>
> >>>However, I want to ignore the first instance of (in this case)
> >>>originalstring, and replace the rest of the occurrences.
>
> >>>Is this possible?
>
> >>>Could someone shed some light upon my quest?
>
> >>For anything other than simple substitutions, use awk or perl or ruby or...
>
> >>In this case, this'll do what you want:
>
> >>awk 'found{sub(/originalstring/,"newstring")} /originalstring/{found=1} 1' file
>
> >>or, if you prefer "cute" solutions:
>
> >>awk 'BEGIN{o=n="originalstring"} sub(o,n){n="newstring"} 1' file
>
> > Yeah that works - cheers.
> > I don't suppose you know how to suppress the printing to screen?
>
> Yes - don't run the script. Sorry, but could you explain what it is you actually
> want to do?
>
> Ed.

i'm trying to run the script, though when I do it prints the contents
of the file I am modifying.
Is there a way to stop awk from doing so?
Bec [ Fr, 16 November 2007 02:55 ] [ ID #1872088 ]

Re: sed scripting

On 2007-11-16, Ivan <find.ivan [at] gmail.com> wrote:
> i'm trying to run the script, though when I do it prints the contents
> of the file I am modifying.
> Is there a way to stop awk from doing so?

Redirect the output to a file.
Bill Marcum [ Fr, 16 November 2007 03:42 ] [ ID #1872089 ]

Re: sed scripting

Ed Morton scrisse:

> ...
> For anything other than simple substitutions, use awk or perl or ruby
> or...

Yes, right said!

But this case is very simple and can be solved very easily in sed:

sed '1,/string/! s/string/replace/g' $FILE
mallin.shetland [ Sa, 17 November 2007 12:01 ] [ ID #1872995 ]

Re: sed scripting

mallin.shetland <mallin.shetland [at] aol.com> wrote:
> sed '1,/string/! s/string/replace/g' $FILE
>
simple but fails if the first occurence of string is on the first line
of $FILE
--
pgas
SDF Public Access UNIX System - http://sdf.lonestar.org
pgas [ So, 18 November 2007 08:07 ] [ ID #1873420 ]

Re: sed scripting

pierre.gaston [at] gmail.com scrisse:

> simple but fails if the first occurence of string is on the first line
> of $FILE

Sorry.

sed '0,/string/! s/string/replace/g' $FILE

It's a GNU sed extension.
mallin.shetland [ So, 18 November 2007 13:44 ] [ ID #1873426 ]

Re: sed scripting

On Nov 18, 11:44 pm, "mallin.shetland" <mallin.shetl... [at] aol.com>
wrote:
> pierre.gas... [at] gmail.com scrisse:
>
> > simple but fails if the first occurence of string is on the first line
> > of $FILE
>
> Sorry.
>
> sed '0,/string/! s/string/replace/g' $FILE
>
> It's a GNU sed extension.

This is great and outputs exactly what I want, but doesn't actually
modify the file I'm trying to alter -- is there a way for it to do so?
Bec [ Mo, 19 November 2007 01:41 ] [ ID #1873831 ]

Re: sed scripting

On 2007-11-19, Ivan <find.ivan [at] gmail.com> wrote:
>
>
> On Nov 18, 11:44 pm, "mallin.shetland" <mallin.shetl... [at] aol.com>
> wrote:
>> pierre.gas... [at] gmail.com scrisse:
>>
>> > simple but fails if the first occurence of string is on the first line
>> > of $FILE
>>
>> Sorry.
>>
>> sed '0,/string/! s/string/replace/g' $FILE
>>
>> It's a GNU sed extension.
>
> This is great and outputs exactly what I want, but doesn't actually
> modify the file I'm trying to alter -- is there a way for it to do so?

If you have GNU sed:

sed -i '0,/string/! s/string/replace/g' $FILE

Otherwise:

sed '0,/string/! s/string/replace/g' $FILE > newfile
mv newfile $FILE
Bill Marcum [ Mo, 19 November 2007 02:32 ] [ ID #1873832 ]
Linux » comp.unix.shell » sed scripting

Vorheriges Thema: Top 10 posters comp.unix.shell
Nächstes Thema: ignoring directories in find