|
Home > Archive > Unix Shell > November 2007 > sed scripting
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| 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?
| |
|
| Ivan <find.i...@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
| |
|
| On Nov 15, 12:54 pm, bsh <brian_hi...@rocketmail.com> wrote:
> Ivan <find.i...@gmail.com> wrote:
>
> 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/...roup/sed-users/
>
>
> What, ignore the first instance in the sentence, or the
> file? The two cases require very different solutions.
>
>
> 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?
>
>
> "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?
| |
| Ed Morton 2007-11-15, 1:29 am |
|
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.
| |
| Maxwell Lol 2007-11-15, 7:36 am |
| Ivan <find.ivan@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
| |
|
| On Nov 15, 3:23 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 11/14/2007 6:25 PM, Ivan wrote:
>
>
>
>
>
>
>
>
>
>
> 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.
| |
| Ed Morton 2007-11-15, 8:43 pm |
|
On 11/15/2007 7:05 PM, Ivan wrote:
> On Nov 15, 3:23 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
>
> 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.
| |
|
| On Nov 16, 12:25 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 11/15/2007 7:05 PM, Ivan wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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?
| |
| Bill Marcum 2007-11-16, 2:59 am |
| On 2007-11-16, Ivan <find.ivan@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.
| |
| mallin.shetland 2007-11-17, 7:36 am |
| 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
| |
| pierre.gaston@gmail.com 2007-11-18, 1:35 am |
| mallin.shetland <mallin.shetland@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
| |
| mallin.shetland 2007-11-18, 7:45 am |
| 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.
| |
|
| On Nov 18, 11:44 pm, "mallin.shetland" <mallin.shetl...@aol.com>
wrote:
> pierre.gas...@gmail.com scrisse:
>
>
> 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?
| |
| Bill Marcum 2007-11-19, 1:41 am |
| On 2007-11-19, Ivan <find.ivan@gmail.com> wrote:
>
>
> On Nov 18, 11:44 pm, "mallin.shetland" <mallin.shetl...@aol.com>
> wrote:
>
> 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
|
|
|
|
|