Find/ replace multi-line segments?

J

JButkus

Hey, I'm using Word 2007 on WinXP, and I was wondering if there's any
way to use the "Replace word" feature to change multi-line blocks of
text quickly and easily. And, if there's no way to do that with
Replace, is there another method I could try?

What I'm looking to do is something like this. Let's say we have this
block of text:
Pico
Pico
Pico
Fermi
Pico
Pico
Fermi
Fermi

I need to insert the word "Porkchop", as a separate line, in between
each occurence of "Pico(linebreak)Fermi", so we arrive at something
like this:
Pico
Pico
Pico
Porkchop
Fermi
Pico
Pico
Porkchop
Fermi
Fermi

Doing it by hand is right out, as I'm dealing with something like 3,000
lines here (and each file needs to be given three or four different
revisions) so any suggestions would be appreciated. Thanks!
 
S

Suzanne S. Barnhill

Replace Pico^lFermi with Pico^lPorkchop^lFermi (assuming what you have
really are line breaks; if they're paragraph breaks, use ^p instead of ^l).
 
K

Klaus Linke

Suzanne S. Barnhill said:
Replace Pico^lFermi with Pico^lPorkchop^lFermi

You can also check "Match wildcards", and replace (Pico^l)(Fermi) with
\1Porkchop^l\2

\1 inserts the first (bracketed expression), \2 the second.
(assuming what you have really are line breaks; if they're paragraph
breaks, use ^p instead of ^l).

^p does not work in "Find what" with wildcards. You'd need to use
Find what: (Pico^13)(Fermi) with
Replace with: \1Porkchop^p\2

(Don't use ^13 in "Replace with"! It won't insert "real" paragraph marks)

Using wildcards may have advantages:
-- You may save some typing (especially if the texts were a bit longer than
in your example).
-- You can't make typos in the replacement text.
-- Character formatting that was applied is more likely to be preserved.

Regards,
Klaus
 
J

JButkus

OK, thanks! That's actually really useful, and it should have saved me
tons of time- I tried that technique out with a "practice file" and it
worked fine. Unfortunately, it doesn't work with the -actual- file. I
finally got a chance to sit down with it a couple minutes ago, and I
noticed a few things:

1. It's not 3,000 lines, it's actually more like 3,000 pages.
2. It's not a native Word file; it's some funky Plain Text format.

When I open it, Word asks me to convert the format so it's readable-
I've tried Windows and US-ASCII so far. The problem now is I can't for
the life of me figure out what the line breaks are! They aren't just
white space, they aren't paragraph breaks, they aren't line breaks, and
it looks like they might be line breaks followed by tabs, except that
doesn't work either... is there any way to get Word to check the
formating data for me? Or should I be using ASCII control characters
here?
 
K

Klaus Linke

First save as a Word document. As long as you haven't done so, the text file
might contain Ascii ^10 characters that are hard to deal with.
Saving in *.doc format removes that complication.

After that, you shouldn't have much trouble telling what's there if you
display the formatting characters:
See Suzanne's article http://word.mvps.org/FAQs/Formatting/NonPrintChars.htm

If that still doesn't work, you could select the characters one by one, and
run the following code in the VBA immediate window (Alt+F11, Ctrl+G):
? AscW(Selection.Text)

If you hit the Return key at the end of that line, it'll show the code of
the selected character:
9 for a tab, 11 for a manual line break, and 13 for a paragraph mark... or
possibly something else if you have a weird text file.

In "Find what" you can search for the character by code if necessary if you
preceed the code by a caret ^ or by "^u" (...so ^11 or ^u11 would find
manual line breaks just as ^l would).
The "u" in ^u stands for "Unicode"... it's optional if the code is below
256, but necessary if the code is greater than that.

Regards,
Klaus
 
J

JButkus

Aha, that worked like a charm. Of course, come to find out I was wron
again... the whole 3,000 pages thing was just Word being wonky and no
giving the right data. The actual total, once all was converted to
.doc, came to a mind-blowing 17,238 pages, give or take a couple lines
It's been saving for about an hour now, still not finished. I'm going t
have a hell of a time tomorrow trying to get that thing reformatted, bu
at least the values are changed (for the first one) and I'll be set fo
the rest of this project. Thanks
 
K

Klaus Linke

How large is that text file? Sounds as if some character (maybe ^12) is
interpreted as a "new page".

Maybe Word is the wrong tool.
Word is designed to work well with your typical text file.

If it's a huge binary file with lots of control characters -- and especially
if there aren't paragraph marks every couple of characters -- Word a hell
of a time dealing with it.

You could maybe still use VBA (from Word) to open the file and deal with the
content (either to process the contents directly, or to strip out weird
stuff so Word can then deal with it)... say using

Dim vFile as Variant
Dim myFSO
Set myFSO = CreateObject("Scripting.FileSystemObject")
Dim myTextStream
Set myTextStream = myFSO.OpenTextFile("C:\myFolder\myFile.txt",
ForReading, True)
vFile = myTextStream.ReadAll()

(add a reference to the "Microsoft Scripting Runtime" in the VBA editor,
"Tools > References....", for this example)

.... and then use the VBA string functions on vPool.

I used a Variant so it's easy to use Split/Filter/Join directly.

After you've filtered/parsed/massaged the text, you can insert the result
into a new document.

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top