Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Searching And Replacing *paragraphs* across files in a directory

0 views
Skip to first unread message

Steve

unread,
Feb 16, 2009, 5:52:03 AM2/16/09
to
Hi;

I'm in the process of moving a web site for a friend. The site has
over 100 files. There is a chunk/paragraph of code that needs to
replaced in each. The various search and replace tools I have seen
focus on one line strings. Is there a tool that would allow you in
a non cumbersome way to specify an entire "paragraph" to be searched
and replaced on?

Thanks in advance for any info

Mark Hobley

unread,
Feb 16, 2009, 7:08:01 AM2/16/09
to
Steve <before...@gmail.com> wrote:
> Is there a tool that would allow you in
> a non cumbersome way to specify an entire "paragraph" to be searched
> and replaced on?

You probably want to use a set of ed commands to do this.

ed *.txt < commands.ed

Do the line breaks within the paragraph fall in the same place? Are the
original paragraphs all identical? What about the replacements?

The editor will probably do what you want, whatever the case.

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

Allodoxaphobia

unread,
Feb 16, 2009, 11:19:37 AM2/16/09
to
On Mon, 16 Feb 2009 12:08:01 GMT, Mark Hobley wrote:
> Steve <before...@gmail.com> wrote:
>> Is there a tool that would allow you in
>> a non cumbersome way to specify an entire "paragraph" to be searched
>> and replaced on?
>
> You probably want to use a set of ed commands to do this.
>
> ed *.txt < commands.ed
>
> Do the line breaks within the paragraph fall in the same place? Are the
> original paragraphs all identical? What about the replacements?
>
> The editor will probably do what you want, whatever the case.

I think the embedded markup will be a bitch to work around.

If the paragraphs are all identical in content, it might be a little
easier to delete all the paragraphs and use SSI to pull in a single copy
of the paragraph from a file.

Jonesy

Bill Marcum

unread,
Feb 16, 2009, 2:35:55 PM2/16/09
to

Awk may be able to do it. How do you define a paragraph in this
context? Is it a series of lines separated by a blank line? A block of
code in braces, where you have to count the left and right braces, not
including those that are quoted or commented?

Vilmos Soti

unread,
Feb 16, 2009, 4:35:37 PM2/16/09
to
Steve <before...@gmail.com> writes:

> Hi;

Hi.

Awk. As someone mentioned, awk can help. Look for the "RS" (Record Separator)
builtin var.

Perl. Look for the $/ ($RS, $INPUT_RECORD_SEPARATOR) builtin var.

Shell. Possibly setting IFS to something like

IFS="

"

might help, but I haven't checked (and it won't handle more than two
empty lines).

You can also do some horrible solutions, like replace the eol char and
the first char in the next line with something "string-doesnt-exist_$x".
Then when done, replace "string-doesnt-exist_$x" with "\n$x".
For this, you need perl or something like that. Once you do this
conversion, you can use line oriented programs to do your massage.

Vilmos

Rahul

unread,
Feb 20, 2009, 4:01:49 AM2/20/09
to
Steve <before...@gmail.com> wrote in news:8fac1e53-9681-4841-bd55-
799257...@f3g2000vbf.googlegroups.com:

Maybe I am totally off the mark but wouldn't a diff+patch solve this
automatically? Just a thought. Perhaps I misunderstand your problem.

--
Rahul

Chris Davies

unread,
Feb 20, 2009, 1:04:45 PM2/20/09
to
Steve <before...@gmail.com> wrote in news:8fac1e53-9681-4841-bd55-
799257...@f3g2000vbf.googlegroups.com:
> I'm in the process of moving a web site for a friend. The site has over
> 100 files. There is a chunk/paragraph of code that needs to replaced
> in each. The various search and replace tools I have seen focus on one
> line strings. Is there a tool that would allow you in a non cumbersome
> way to specify an entire "paragraph" to be searched and replaced on?

Rahul <nos...@invalid.invalid> wrote:
> Maybe I am totally off the mark but wouldn't a diff+patch solve this
> automatically? Just a thought. Perhaps I misunderstand your problem.

Another thought is to use perl to slurp in each page in one go, and
match the start/end of the paragraph (i.e. ignoring line breaks) for
the substitution. Pretty much a one-liner I would have thought:

perl -i.bak -e 'local $/; $page = <>; $page =~ s/^Start of para .*? to
its end$/replacement text/m; print $page' *.html

(Untested)

Chris

0 new messages