Finding Text Within The Current Line?

Help with writing and playing macros

Finding Text Within The Current Line?

Postby golddave » Thu Apr 06, 2006 7:32 pm

I'm writing a macro to edit some files and need to be able to search for a particular character within the current line. I don't want to search the whole file just the current line. How would I do this???
User avatar
golddave
Newbie
 
Posts: 3
Joined: Wed Apr 05, 2006 11:00 pm

Re: Finding Text Within The Current Line?

Postby Mofi » Fri Apr 07, 2006 11:55 am

Either copy the selected line to a new file and search for the character there:

Clipboard 9
SelectLine
Copy
NewFile
Paste
Top

Find "x"
IfFound
CloseFile NoSave
:
:
Else
CloseFile NoSave
:
:
EndIf
ClearClipboard
Clipboard 0

or run a replace all on the selected line without really replacing something:

SelectLine
Find MatchCase "x"
Replace All SelectText "x"
IfFound
:
:
Else
:
:
EndIf

Third method would be to temporarily modify the selected line so it contains at start of the line a string which surely does not exist anywhere else in the file and use a regular expression search with this specific string inside the regular expression search string.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Finding Text Within The Current Line?

Postby golddave » Mon Apr 10, 2006 5:20 pm

This worked like a charm. Thank you verry much. Now I need a way to capture the filename and chop off the last few characters so I can use it in a replace command. For instance if the filename is Schedule2006.txt I want to cut off the 2006.txt part. But the filename could also be Results2006.txt and I'd want to cut off the same part of the filename so I couldn't just hard code the filename. Any thoughts?

After I get this one done I should be finished with this macro for now.

Thanks.
User avatar
golddave
Newbie
 
Posts: 3
Joined: Wed Apr 05, 2006 11:00 pm

Re: Finding Text Within The Current Line?

Postby Mofi » Mon Apr 10, 2006 5:36 pm

This macro is recorded quick and dirty but it works without regular expression search for your get filename part task.

InsertMode
ColumnModeOff
HexOff
Top
Clipboard 9
CopyFilePath
Paste
Loop 8
Key BACKSPACE
EndLoop
"
"
Key UP ARROW
Key END
Find Up "\"
Key DEL
SelectToTop
Key DEL
StartSelect
Key END
Cut
EndSelect
Key DEL
Find "...."
Replace "
^c"
ClearClipboard
Clipboard 0

The additional questions you wrote for this problem are deleted. Hopefully it is okay.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Finding Text Within The Current Line?

Postby golddave » Tue Apr 11, 2006 1:55 pm

You rock, Mofi. I wrote a slight variation on what you wrote and it works great. This is going to save me alot of time in the future.

Thanks,
Dave
User avatar
golddave
Newbie
 
Posts: 3
Joined: Wed Apr 05, 2006 11:00 pm

Re: Finding Text Within The Current Line?

Postby Hownow » Wed Jul 19, 2006 5:02 am

Hi Mofi,
I haven't been able to make this macro work. Your help would be appreciated. Here is my variation:

Code: Select all
InsertMode
ColumnModeOff
HexOff
UnixReOff
TrimTrailingSpaces
Top
Loop
SelectLine
Find "Trade"
Replace All SelectText "Trade"
IfNotFound
Key END
" "
Key DEL
Else
ExitLoop
EndIf

I am trying to concatenate a section of a file with a varying number of line items stopping when it encounters a particular word or phrase.

It is not working, it ignores the first line and then misses the Find command. What does the search & replace for the same string accomplish? Your thoughts would be appreciated.
Thank you.
User avatar
Hownow
Newbie
 
Posts: 5
Joined: Thu Apr 20, 2006 11:00 pm

Re: Finding Text Within The Current Line?

Postby Mofi » Wed Jul 19, 2006 6:08 am

Hownow wrote:What does the search & replace for the same string accomplish?

It does nothing or exactly in your case without Find option MatchCase it converts the word trade in any case to "Trade". It's just for testing if a specific string is within the current line. It simply restricts the find on a single line.

I guess your loop is not working because there is missing the command EndLoop. And in the IfNotFound branch you have forgotten
EndSelect
Key UP ARROW

before Key END. SelectLine selects the whole line including the line terminator and the cursor is at start of the next line.

If you are a macro beginner you should test your macro step by step by inserting the macro command ExitMacro after every important command sequence and look what happens when executing the macro to this point. Then reload the file to restore all changes, edit the macro and move the ExitMacro command down, run the macro again, look again, reload the file, ...

But I think for your problem there is a much faster solution.

InsertMode
ColumnModeOff
HexOff
TrimTrailingSpaces
Top
StartSelect
Find Select "Trade"
IfSel
Key UP ARROW
Key END
Find "^p"
Replace All SelectText " "
EndIf
EndSelect
Top

What this macro does?

From top of the file it selects everything till word "Trade". If the word "Trade" (or "trade" or "tRaDe" or ...) is found and so a selection exists, it removes from the selection the current line by setting the cursor to the end of the previous line (or current line if "Trade" is found at first line of the file). Because of the StartSelect command all cursor moves modify the selection until EndSelect is used. Then it replaces in the whole selected text all CRLF by a space to concatenate the lines. If your file is a Unix file opened in Unix mode you have to use ^n instead of ^p.

I'm not sure if the line with "Trade" should be also added to the concatenate block or not. The macro above will not add it. Remove the macro commands
Key UP ARROW
Key END

and the text of the line with "Trade" is at the end of the single line after the macro execution.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Finding Text Within The Current Line?

Postby Hownow » Wed Jul 19, 2006 4:19 pm

Mofi,
Thank you for this elegant solution! I "stepped" through the code to figure out how you accomplished this. I was wondering how I could set breakpoints, maybe this will get me beyond the beginners level.

How does this command - Replace All SelectText " " know to remove the CR/LF at the end of each individual line?
User avatar
Hownow
Newbie
 
Posts: 5
Joined: Thu Apr 20, 2006 11:00 pm

Re: Finding Text Within The Current Line?

Postby Mofi » Thu Jul 20, 2006 5:21 am

Hownow wrote:How does this command - Replace All SelectText " " know to remove the CR/LF at the end of each individual line?

The Replace command (not the ReplInFiles command) works always and only in combination with the Find command in the line above. A Replace without Find is not possible. This is exactly as in the Replace dialog where you have to enter also always a search string.

Find "^p" searches for CRLF. See help of UE about the Find command where the special characters for non visible control characters are listed and explained in a table. This is not a regular expression character!
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Finding Text Within The Current Line?

Postby karljunker » Thu Aug 24, 2006 2:14 pm

I have a long textfile with references, see below. Before importing that properly into Endnote (a bibliography software), I want to clean the Lines "PG:" In each line "PG: ..." the space between " - " should be removed, so that it results, e.g. in "PG: 150-167".
Can anybody tell me the grep code?

TI: A role for individuality and mystery in "managing"
User avatar
karljunker
Newbie
 
Posts: 2
Joined: Wed Aug 23, 2006 11:00 pm

Re: Finding Text Within The Current Line?

Postby Mofi » Fri Aug 25, 2006 5:08 am

Can be done with following UltraEdit style regular expression replace:

Find: %^(PG: [0-9]+^) - ^([0-9]+^)
Replace: ^1-^2

Same with Unix/Perl regular expression style

Find: ^(PG: \d+) - (\d+)
Replace: \1-\2
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Finding Text Within The Current Line?

Postby karljunker » Mon Aug 28, 2006 8:16 pm

Thanks for this! I checked for a solution at www.regular-expressions.info, however I did not come to a successful result. This helps, great!
User avatar
karljunker
Newbie
 
Posts: 2
Joined: Wed Aug 23, 2006 11:00 pm


Return to Macros

cron