Replace first occurrence with term 1 and second with term 2

Help with writing and playing macros

Replace first occurrence with term 1 and second with term 2

Postby HansFink » Fri Jun 09, 2006 9:46 am

Hello,

how can I replace the same expression on the first occurrence with term1 and on the second occurrence with term2?

Example:

Input
blah blah blah
blah blah TEST blah blah blah
blah blah TEST blah blah
blah blah blah

Output
blah blah blah
blah blah TERM1 blah blah blah
blah blah TERM2 blah blah
blah blah blah

I want to replace TEST the first time with TERM1 and the second time with TERM2. How can I accomplish that?
Any help appreciated.
User avatar
HansFink
Basic User
Basic User
 
Posts: 17
Joined: Fri Jan 27, 2006 12:00 am

Re: Replace first occurrence with term 1 and second with term 2

Postby Bego » Fri Jun 09, 2006 10:12 am

hi,

Why use regexp ?

Take that macro:

Code: Select all
InsertMode
ColumnModeOff
HexOff
UnixReOn
Top
Find MatchCase "TEST"
Replace "TERM1"
Find MatchCase "TEST"
Replace "TERM2"

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Replace first occurrence with term 1 and second with term 2

Postby HansFink » Fri Jun 09, 2006 10:28 am

Thank you, but I forgot to mention I need it for multiple files.
I have several files open want to replace all open files.
Is that possible too?
User avatar
HansFink
Basic User
Basic User
 
Posts: 17
Joined: Fri Jan 27, 2006 12:00 am

Re: Replace first occurrence with term 1 and second with term 2

Postby Mofi » Fri Jun 09, 2006 10:41 am

Are the 2 terms to be replaced always on consecutive lines or at least is the number of lines between term1 and term2 constant?

If this is true, you can do it with a regular expression replace. If the number of lines differ, you have to run Bego's macro on all open files.

For example see How do you run a Macro on open files? or use this macro which closes every file after the 2 replaces.

Loop
IfNameIs ""
ExitLoop
EndIf
Top
Find MatchCase "TEST"
Replace "TERM1"
Find MatchCase "TEST"
Replace "TERM2"
CloseFile Save
EndLoop
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Replace first occurrence with term 1 and second with term 2

Postby HansFink » Fri Jun 09, 2006 10:46 am

Thanks again. That was what I was looking for.
User avatar
HansFink
Basic User
Basic User
 
Posts: 17
Joined: Fri Jan 27, 2006 12:00 am

Re: Replace first occurrence with term 1 and second with term 2

Postby HansFink » Mon Jun 12, 2006 4:48 am

I still have some problems. Unfortunately not all files have the expression and I just want to skip these files go to the next window.

I tried this, but that does not work

Code: Select all
InsertMode
ColumnModeOff
HexOff
Top
Loop
IfNameIs ""
ExitLoop
Find MatchCase RegExp MatchWord "TEST"
Replace "TERM1"
Find MatchCase RegExp MatchWord "TEST"
Replace "TERM2"
NextWindow
IfNotFound
Loop
NextWindow
Top
Find MatchCase MatchWord "D4"
IfFound
EndLoop
EndLoop
ExitMacro

Also there are some things I don't quite understand:

How do I do the exit condition for a loop? I guess IfNameIs "" means if there are no files open or left exit the loop. But how do I do other conditions for example if the search expression is not found?

ExitLoop / EndLoop
What is the difference?
User avatar
HansFink
Basic User
Basic User
 
Posts: 17
Joined: Fri Jan 27, 2006 12:00 am

Re: Replace first occurrence with term 1 and second with term 2

Postby Mofi » Mon Jun 12, 2006 5:51 am

EndLoop is the marker where the code block of the loop ends. The macro code between Loop and EndLoop has to be executed until the loop exits. ExitLoop breaks the loop immediately at this position and the macro continues with the command after EndLoop. So ExitLoop is just a JUMP to command after EndLoop.

It's also possible to run a loop without ExitLoop command if you need a defined number of loop execution. For example a simple loop macro to place the cursor at column 70 in the current line:

Key HOME
IfCoNumGt 1
Key HOME
EndIf
Loop 70
Key RIGHT ARROW
EndLoop

Your macro will also fail because you have nested loops - loop inside an existing loop. This is not possible in UE macro language.

IfNameIs "" means if the current file name is NULL which is true if no file is open or the current file is a new file which does not have a name until first save. So if you have only named files open, my macro exits after all files were modified and closed.

Well, this method will not work if you don't close a file if it does not contain the search term. You will produce an endless loop in this case because there will be always at least one file opened which has a name.

IfFound and IfNotFound can be used after a Find or Replace. It is possible to have other commands between Find/Replace and IfFound/IfNotFound although usually the condition commands are immediately after the Find/Replace command. The Replace command must be always immediately after the Find command. The IfFound/IfNotFound command must be below the Replace command and can be never between Find and Replace.

You can use ExitLoop as often as you want inside a loop.

ExitMacro at the end of the macro is useless because if there are no more macro commands the macro automatically ends. ExitMacro is only for exiting the macro execution anywhere inside the macro.

If you use IF conditions you have to write also the EndIf command for every IF condition or UE does not know which commands are for the current IF condition. An Example:

Code: Select all
IfSel
 command 1
 command 2
Else
 Find "..."
 IfFound
  command 3
  command 4
 Else
  Find "xxx"
  IfNotFound
   command 5
   command 6
  EndIf
 EndIf
 command 7
EndIf


In comparison to loops nesting of IF conditions is possible as you can see. Every command in this example can be also an ExitLoop command if this code block is inside a loop.

Note: Indenting the commands in the edit macro dialog is not possible. This example here is just for better understanding.

If you check the macro property Continue if a Find with Replace not found my macro will also work for files which does not contain the search term. Those files are also saved and closed although nothing is changed by the macro because the string was not found.

If you must know which files do not contain the search string and you want those files open after the macro execution use following macro with macro property Continue if a Find with Replace not found checked. It collects the file names with path of the files without the search string in user clipboard 9 and after first loop has finished and all open files are closed the macro runs a second loop to open the files without the search string. Make sure there is no space after first " at the 2 lines with only " before you copy the code into the edit macro dialog.

"
"

is the macro code to insert a line break to have later only one file name with path per line.

Here is the extended macro code which reopens the files which does not contain the search string. Make sure all files are saved before starting the macro or use SaveAll macro command before first loop because files without the search string are temporarily modified and so closed without saving (what you could also change) to not change the file time.


InsertMode
ColumnModeOff
HexOff
Clipboard 9
ClearClipboard
Loop
IfNameIs ""
ExitLoop
EndIf
Top
Find MatchCase MatchWord "TEST"
IfFound
"TERM1"
Find MatchCase MatchWord "TEST"
Replace "TERM2"
CloseFile Save
Else
Clipboard 8
CopyFilePath
Paste
"
"

Clipboard 9
SelectToTop
CutAppend
CloseFile NoSave
EndIf
EndLoop
NewFile
Clipboard 8
ClearClipboard
Clipboard 9
Paste
Top
Key END
Clipboard 9
IfColNum 1
CloseFile NoSave
ClearClipboard
Clipboard 0
ExitMacro
EndIf
Key HOME
Loop
StartSelect
Key END
Cut
EndSelect
Key DEL
Open "^c"
NextWindow
IfEof
ExitLoop
EndIf
EndLoop
CloseFile NoSave
ClearClipboard
Clipboard 0
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Replace first occurrence with term 1 and second with term 2

Postby HansFink » Mon Jun 12, 2006 6:46 am

Thank you Mofi,

now the UE macro language becomes more clear. And thanks again for the macro.
User avatar
HansFink
Basic User
Basic User
 
Posts: 17
Joined: Fri Jan 27, 2006 12:00 am


Return to Macros