sed scripting

sed scripting

am 15.11.2007 01:25:54 von Bec

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?

Re: sed scripting

am 15.11.2007 02:54:52 von brian_hiles

Ivan 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

Re: sed scripting

am 15.11.2007 05:14:17 von Bec

On Nov 15, 12:54 pm, bsh wrote:
> Ivan 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?

Re: sed scripting

am 15.11.2007 05:23:48 von Ed Morton

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.

Re: sed scripting

am 15.11.2007 13:33:15 von Maxwell Lol

Ivan 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

Re: sed scripting

am 16.11.2007 02:05:21 von Bec

On Nov 15, 3:23 pm, Ed Morton 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.

Re: sed scripting

am 16.11.2007 02:25:23 von Ed Morton

On 11/15/2007 7:05 PM, Ivan wrote:
> On Nov 15, 3:23 pm, Ed Morton 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.

Re: sed scripting

am 16.11.2007 02:55:05 von Bec

On Nov 16, 12:25 pm, Ed Morton wrote:
> On 11/15/2007 7:05 PM, Ivan wrote:
>
>
>
> > On Nov 15, 3:23 pm, Ed Morton 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?

Re: sed scripting

am 16.11.2007 03:42:33 von Bill Marcum

On 2007-11-16, Ivan 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.

Re: sed scripting

am 17.11.2007 12:01:52 von mallin.shetland

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

Re: sed scripting

am 18.11.2007 08:07:23 von pgas

mallin.shetland 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

Re: sed scripting

am 18.11.2007 13:44:51 von mallin.shetland

pierre.gaston@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.

Re: sed scripting

am 19.11.2007 01:41:21 von Bec

On Nov 18, 11:44 pm, "mallin.shetland"
wrote:
> pierre.gas...@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?

Re: sed scripting

am 19.11.2007 02:32:17 von Bill Marcum

On 2007-11-19, Ivan wrote:
>
>
> On Nov 18, 11:44 pm, "mallin.shetland"
> wrote:
>> pierre.gas...@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