 |
|
 |
|
11-15-07 12:21 AM
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?
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
11-15-07 06:29 AM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
 |
|
 |
|
11-15-07 06: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/{fou
nd=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.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
11-15-07 12:36 PM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
11-16-07 01:43 AM
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/{f
ound=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.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
11-16-07 01:43 AM
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 actu
ally
want to do?
Ed.
[ Post a follow-up to this message ]
|
|
|
 |
|
|
11-16-07 01:43 AM
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 ac
tually
> 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?
[ Post a follow-up to this message ]
|
|
|
 |
|
|
11-16-07 07: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.
[ Post a follow-up to this message ]
|
|
|
 |
|
|
11-17-07 12:36 PM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 08:08 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|