Sunday, April 4, 2010

Find/Replace, is this possible?

Hello, I have a directory of company names, which all look like this%26lt;br /%26gt;%26lt;br /%26gt;Each line has it's own paragraph style%26lt;br /%26gt;%26lt;br /%26gt;Company Name %26lt;br /%26gt;Address%26lt;br /%26gt;Phone%26lt;br /%26gt;Fax%26lt;br /%26gt;Toll Free%26lt;br /%26gt;URL%26lt;br /%26gt;%26lt;br /%26gt;here is the thing, I forgot to put the words ''Phone:'' ''Fax:'' and ''Toll Free:'' before the actual numbers.%26lt;br /%26gt;%26lt;br /%26gt;for example I have:%26lt;br /%26gt;%26lt;br /%26gt;Some Company%26lt;br /%26gt;12 someplace dr.%26lt;br /%26gt;Some City, ST xxxxxxx%26lt;br /%26gt;123-456-7890%26lt;br /%26gt;123-465-7809%26lt;br /%26gt;800-123-4565%26lt;br /%26gt;%26lt;br /%26gt;and I need it to look like this%26lt;br /%26gt;%26lt;br /%26gt;Some Company%26lt;br /%26gt;12 someplace dr.%26lt;br /%26gt;Some City, ST xxxxxxx%26lt;br /%26gt;Phone: 123-456-7890%26lt;br /%26gt;Fax: 123-465-7809%26lt;br /%26gt;Toll Free: 800-123-4565%26lt;br /%26gt;%26lt;br /%26gt;Can someone tell how I can do a find replace so that I don't have to reformat the original list of companies?%26lt;br /%26gt;%26lt;br /%26gt;I tried the GREP Find: Format + %26lt;Start of Paragraph%26gt; and tried to replace with %26lt;Start of Paragraph%26gt;Phone: %26lt;br /%26gt;%26lt;br /%26gt;But nothing happens, it seems that I cannot use text to replace a variable in GREP find/replace...and I have not found the variable: %26lt;start of paragraph%26gt; on the text tab.%26lt;br /%26gt;%26lt;br /%26gt;Any help would be greatly appreciated. Thank you very much%26lt;br /%26gt;%26lt;br /%26gt;KevinFind/Replace, is this possible?
This is more likely to need a script to do a complete replacement. Not being an ID script whiz myself, I'd go back to the original document - or export the ID content to RTF or XML - and then use Word or a good text editor to do the more complex find and replace operations.
Find/Replace, is this possible?
%26gt;it seems that I cannot use text to replace a variable in GREP find/replace...and I have not found the variable: %26lt;start of paragraph%26gt; on the text tab.%26lt;br /%26gt;%26lt;br /%26gt;The mark for 'start of paragraph' in GREP is%26lt;br /%26gt;%26lt;br /%26gt;%26gt;^%26lt;br /%26gt;%26lt;br /%26gt;but you are right, you cannot 'replace' this with text. Also, if it %26lt;br /%26gt;i did%26lt;br /%26gt;work, you'd add ''Phone'' to %26lt;br /%26gt;i every %26lt;br /%26gt;line.%26lt;br /%26gt;%26lt;br /%26gt;Let's go over this step by step. You want to search for numbers at the start of a line:%26lt;br /%26gt;%26lt;br /%26gt;%26gt;^[0-9]%26lt;br /%26gt;%26lt;br /%26gt;or, more exact, a phone number:%26lt;br /%26gt;%26lt;br /%26gt;%26gt;^[0-9]+[-0-9]+$%26lt;br /%26gt;%26lt;br /%26gt;(this'll skip ''21 Jump Street''). Be sure there are no stray spaces at the end of the paragraphs. Now, how to add something %26lt;br /%26gt;i before %26lt;br /%26gt;the 'found' text? ''Found text'' is a GREP replace variable. In the 'Replace with' field, enter%26lt;br /%26gt;%26lt;br /%26gt;%26gt;Phone: $0%26lt;br /%26gt;%26lt;br /%26gt;So, hey, every phone number gets a ''Phone: '' added. We're halfway there.%26lt;br /%26gt;Undo this :-) so you can add different texts before each number in one go.%26lt;br /%26gt;A new line in GREP is marked with '\r', and you want to look for (a phone number) newline (a phone number) newline (a phone number). Searching for%26lt;br /%26gt;%26lt;br /%26gt;%26gt;^([0-9]+[-0-9]+)\r([0-9]+[-0-9]+)\r([0-9]+[-0-9]+)%26lt;br /%26gt;%26lt;br /%26gt;will do this for you. The parentheses are important! They mark placeholders for the Replace expression, which is%26lt;br /%26gt;%26lt;br /%26gt;%26gt;Phone: $1\rFax: $2\rToll Free: $3%26lt;br /%26gt;%26lt;br /%26gt;How can this %26lt;br /%26gt;i not%26lt;br /%26gt;work?%26lt;br /%26gt;* What if you have a phone and fax number, but no Toll free? You'll gonna have to do those by hand (S%26amp;R cannot know what numbers are what).%26lt;br /%26gt;* What if there are spaces somewhere at the start or end of the paragraphs, or inside the phone numbers? Those shouldn't be there.%26lt;br /%26gt;* What if there is some more text on a line, such as ''Phone: 123-456-7890 (office hours only)''? Nope -- doesn't work.%26lt;br /%26gt;%26lt;br /%26gt;But it's probably more work to avoid/incorporate every possible odd instance than merely correcting them by hand after doing the replace.
I think there's an easier way to do this.



Since each type of data has its own style, you can just search for



(.)



in the ''Phone'' style (for example), and replace it with



Phone: $1



Then repeat for ''Fax'' style, etc.

Dom,



I missed that. You are right: the styles are already in place. No problem at all, except the GREP should be refined to



%26gt;.+



with



%26gt;Phone: $0



as replacement.



The parenthesis aren't necessary, because the entire 'found' text is used (that's '$0'). But the '+'

i is

necessary, because the period matches any one single character. ''123-456-7890'' would become ''Phone: 1Phone: 2Phone: 3Phone: -'' (etc.).

An

i unnecessary

refinement would be to 'force' an entire line, using something like



%26gt;^.+$



It's unnecessary, because GREP is greedy by default, and picks up the entire line with ''.+''. And it doesn't matter if it picks up the carriage return at the end, because (a) it does not (I think!), and (b) even if it does -- because it's the same paragraph style --, it doesn't matter because '$0' puts everything right back where it was found.

On the other hand, it's

i not

necessary to 'find' the entire paragraph! The mere first character in the selected style is enough:



%26gt;^.



-- but you need the exact same replacement expression.

No comments:

Post a Comment