Help with macro creating

Help with writing and playing macros

Help with macro creating

Postby wrznet » Thu Nov 16, 2006 2:01 am

Hi,
I have txt file with lines like:

First name:John,info:asfsdfdsfsdg,Last Name:Smith

I would like to create a macro which will sore the txt file and give me output
into the new window like:

John Smith

As much as I understand, I can search for 'First name:' then search for 'Last Name:'
But the problem is, how do I select word 'John' or maybe yet complicated 'Tommy Le' ? Good strategy can be to select all characters after 'First name:' and until the next comma (just before info:asfsdfdsfsdg in my example). But how I can create a macro to select characters until comma ?
This is my question :)
Thanks.
User avatar
wrznet
Basic User
Basic User
 
Posts: 11
Joined: Sun Aug 29, 2004 11:00 pm

Re: Help with macro creating

Postby mrainey56 » Thu Nov 16, 2006 2:23 am

To get you started.


InsertMode
ColumnModeOff
HexOff
UnixReOn
Top
Find RegExp "^First name:(.*),.*Last Name:(.*)$"
Replace All "\1 \2"
User avatar
mrainey56
Master
Master
 
Posts: 212
Joined: Tue Jul 27, 2004 11:00 pm
Location: Spartanburg, South Carolina

Re: Help with macro creating

Postby Mofi » Mon Nov 20, 2006 3:40 pm

If you are running Mike's macro on an empty file, it is not suprising that you will get a "String not found" message. Step by step:

  1. Copy following lines (or any other macro code posted) from your browser window to Windows clipboard:

    InsertMode
    ColumnModeOff
    HexOff
    UnixReOn
    SelectAll
    Copy
    NewFile
    Paste
    Top
    Find RegExp "^\s*First name:(.*),.*Last Name:(.*)$"
    Replace All "\1 \2"

  2. Start UltraEdit and open your file with the data. AND OPEN ONLY THIS FILE!!!

  3. Click on Macro - Record, enter a macro name, disable the "Cancel" option, but enable the "Find and Replace" option and press button OK.

  4. Click on Macro - Stop Recording.

  5. Click on Macro - Edit Macro....

  6. Select in the left macro code edit field the first 3 macro commands. Do not select the last line which contains the macro command for your configured regular expression engine.

  7. Press Ctrl+V to replace the first 3 lines with the macro code copied from the browser window.

  8. Press the button Close and confirm updating the macro with Yes.

  9. Click on Macro - Play Again.

  10. Finished!
You can omit step 3 + 4 and instead press button New Macro after opening the edit macro dialog and enter there the name for the new macro and set the macro properties.

Don't forget to save all the currently loaded and created macros into a macro file when you need them again.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4062
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Help with macro creating

Postby MrBillG » Mon Nov 20, 2006 3:41 pm

Hi wrznet

Yeah you're missing something.

First the help file is your friend, read "Macro menu"
"Help" pull down menu --> "Index" enter "Macro menu"

This is not related to your problem but is a good exercise to understand what a Macro is.
Under the pull down menu "Macro" --> "Record", UltraEdit will record your actions you take while editing your file.
So move your cursor, copy some text and open a new file and paste something in it.
Once you had some fun, go to pull down menu "Macro" --> "Stop Recording".

Ok you created your first macro, to see it goto "Macro" --> "Edit Macro..."
Here is where you will see your first macro you just created.

This is where Mike wants you to paste his macro.

But I'm jumping ahead of myself, you need to play the macro you just recorded, you have two options "Macro" --> "Play Again" or "Play Any/Multiple Times"
With your data, play around and see how these options work, remember the help file explains each option.
Now that you have a feel for what and how macros work, lets work on your problem.

Goto "Macro" --> "Edit Macro..." when the window open there is a button
"New Macro", click it, give it a name (notice where it is saved, also it will be saved with a suffix of .MAC for MACro) now paste Mike's macro into the left hand box.
You know how to "Play" the macro.

Good Luck, hope this helps!
User avatar
MrBillG
Advanced User
Advanced User
 
Posts: 65
Joined: Sat Aug 07, 2004 11:00 pm

Re: Help with macro creating

Postby wrznet » Tue Nov 21, 2006 6:55 am

Im sorry to disappoint all, but STILL does not work for me.
I would say, im not a noob. And this is not my first macro :)
So what im doing and what I get ?

SelectAll
Copy
NewFile
Paste
Top
Find RegExp "^\s*First name:(.*),.*Last Name:(.*)$"
Replace All "\1 \2"

SelectAll,Copy,NewFile,Paste just select the first line copy it into clipboard and paste into a new window.
Then :

Find RegExp "^\s*First name:(.*),.*Last Name:(.*)$"
Replace All "\1 \2"

Is suppose to find name after First name: and find surname after Last Name: and replace the line (format) with the actual first and last name, but this is where I get String not found.
So ultimately, the line:

First name:John,info:asfsdfdsfsdg,Last Name:Smith

should be formated and I should see John Smith, but it not happens.

1. I open ultraedit and copy one line "First name:John,info:asfsdfdsfsdg,Last Name:Smith" to make it easy
2. Macro>Edit Macro>New macro and copy/paste following code:

InsertMode
ColumnModeOff
HexOff
UnixReOn
SelectAll
Copy
NewFile
Paste
Top
Find RegExp "^\s*First name:(.*),.*Last Name:(.*)$"
Replace All "\1 \2"

3.ctrl+m. I see new window with pasted line "First name:John,info:asfsdfdsfsdg,Last Name:Smith" and nice "string not found" window.

Now I feel myself really stupid :)
User avatar
wrznet
Basic User
Basic User
 
Posts: 11
Joined: Sun Aug 29, 2004 11:00 pm

Re: Help with macro creating

Postby Mofi » Wed Nov 22, 2006 7:11 am

I think remote help is not possible anymore until you upload your file as zip-archive anywhere and post a link to it here.

Note: The forum upload feature is disabled, although you can see the form for file uploads. Use one of the thousands sites where free uploads are possible.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4062
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Help with macro creating

Postby Bego » Wed Nov 22, 2006 3:49 pm

downloaded it and opened file in UE.
Looks like this:
\uFEFFFirst name bla
seems to be unicode-file and that SEEMS the problem.
look also here: http://unicode.org/unicode/faq/utf_bom.html
Sry, I'm no expert here ...
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Help with macro creating

Postby wrznet » Thu Nov 23, 2006 4:23 am

How I can format the string into regular txt (not unicode)
Also even if it have \uFEFFFirst name:, is not the macro suppose to find 'First name:' ?
User avatar
wrznet
Basic User
Basic User
 
Posts: 11
Joined: Sun Aug 29, 2004 11:00 pm

Re: Help with macro creating

Postby Mofi » Thu Nov 23, 2006 8:28 am

It's the first time that I see an ASCII Escaped Unicode file. If the config option Detect ASCII/ANSI file with Escaped Unicode at Configuration - File Handling - Unicode/UTF-8 Detection is not checked, it is loaded as pure ASCII file. Looks like Mike and Bego and I too have not checked it because we never edit ASCII Escaped Unicode files.

In your case you better let it checked. With this option the file is loaded as ASCII Escaped Unicode file which you can see in the status bar at bottom of the UltraEdit window. The second box contains the file format identifier which is here UESC-DOS. Next time please post this identifier so we know what format your file has.

Additionally UltraEdit (v11.20b and v12.20a+3) seems to have a bug with ASCII Escaped Unicode files because the UTF-16 LE BOM (byte order marker) is twice at top of the file after loading the file with the appropriate config option enabled. Please report this bug to IDM support by email.

This twice BOM is now the reason, why the macro did not work.

^\s*First name: at start of the search string means following:

^ ...... at start of a line
\s* ...  find 0 or more occurencies of white-space characters (spaces, tabs)
First name: ... followed by the string "First name:"

The BOM is not a white-space character and so the regex string is not found.

Here is the modified macro which now converts the ASCII Escaped Unicode file to a pure ASCII file and contains a workaround for the double BOM bug.

InsertMode
ColumnModeOff
HexOff
UnixReOn
SelectAll
Copy
NewFile
ASCIIToUnicode
UnixMacToDos
Paste
UnicodeToASCII
Top
IfCharIs "?"
Key DEL
EndIf

Find RegExp "^\s*First name:(.*),.*Last Name:(.*)$"
Replace All "\1 \2"


Of course, you could also use this macro which creates a real Unicode result file.

InsertMode
ColumnModeOff
HexOff
UnixReOn
SelectAll
Copy
NewFile
ASCIIToUnicode
UnixMacToDos
Paste
HexOn
Top
Find "FFFEFFFE"
Replace "FFFE"
HexOff

Top
Find RegExp "^\s*First name:(.*),.*Last Name:(.*)$"
Replace All "\1 \2"
Top

Add UnixReOff or PerlReOn (v12+ of UE) at the end of the macro you use if you do not use Unix style regular expressions by default - see search configuration. Macro command UnixReOn sets the regular expression option to Unix style.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4062
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Help with macro creating

Postby wrznet » Thu Nov 23, 2006 8:16 pm

Finally its works!!!
Now the only question I have is...
I open UltraEdit and just copy/paste from browser into my ultraedit window, and the copied string seems unicoded already (so simple script does not work).
I was wondering, what settings I should enable/disable with ultraedit to be like a 'normal' guy who wont need to use ASCIIToUnicode commands into macro editor ?
I tried to play with Configuration - File Handling - Unicode/UTF-8 Detection settings, but it did not help me out.
User avatar
wrznet
Basic User
Basic User
 
Posts: 11
Joined: Sun Aug 29, 2004 11:00 pm

Re: Help with macro creating

Postby Mofi » Fri Nov 24, 2006 6:06 pm

Look at Configuration - Editor - New File Creation if you have enabled Always create new files as UNICODE. If you have, uncheck it.

Additionally save a new file and look in the Save As dialog which file format is specified there. This file format is used whenever you save a file using the Save As dialog until you change it (up to v15.20). The value Default means according to the current format of the new file which is specified for new files with the configuration option, but can be changed at any time also with File - Conversions.

Note: Using (again) ASCII/ANSI (normally also Default) by default as most UE users do, will cause an automatical Unicode to ASCII conversion with current Code Page setting when pasting from applications which copied Unicode characters to the clipboard. So some language specific characters can look different after pasting because of wrong code page specification or there is simply no ANSI equivalent for the Unicode character.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4062
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Help with macro creating

Postby wrznet » Sun Nov 26, 2006 7:46 pm

The thing is, I use default configuration. And 'Always create new files as UNICODE' is not checked here.
I just open UltraEdit and copy/paste from browser (this page) and once the line is pasted into UltraEdit, it seems unicoded.
Just not sure why it happiens to me. Looks like once the line into clipboard, its already corrupted somehow, which is caused the problems.
User avatar
wrznet
Basic User
Basic User
 
Posts: 11
Joined: Sun Aug 29, 2004 11:00 pm

Re: Help with macro creating

Postby wrznet » Thu Dec 07, 2006 1:23 pm

I still try to figure out of the script can be done. I have another question.

My lines with information have a lot of garbage like:

someinfo,moreinfo,garbage,moregarbage,FirstName=John,garbageagain,andagain,LastName=Smith,info,Address=SomeStree,garbage

I want to extract FirstName,LastName,Street,City,etc.
Tried to make some modification of this script, but was unable to do that.
Plus this unicode problem make me crazy :)
Anyone can suggest how this part of the script:

Find RegExp "^\s*First name:(.*),.*Last Name:(.*)$"
Replace All "\1 \2"

could be redone, so i can get the need info from file?
User avatar
wrznet
Basic User
Basic User
 
Posts: 11
Joined: Sun Aug 29, 2004 11:00 pm

Re: Help with macro creating

Postby Mofi » Fri Dec 08, 2006 9:13 am

Come on and read the help article about regular expressions. It's not too difficult to extract data form a CSV file with a regular expression replace.

The following Unix/Perl regex string will work in your macro for the example you posted. Red highlighted are the modifications needed for your new example.

Find RegExp "^.*FirstName=(.*),.*LastName=(.*),.*Address=(.*),.*$"
Replace All "\1 \2 \3"
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4062
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Help with macro creating

Postby wrznet » Fri Dec 08, 2006 11:14 am

Mofi, thank you very much for your help.
I do understand about what you said here, but my question was in fact a bit different.
With your examples above, I have no problems extracting data like that,
but what if FirstName=,etc is not the first letters in the line ?
I have some GaRbAgE before FirstName=,then many garbage i need to skip before LastName= and so on.
Now if I try this macro above, I still have string not found error. Even with your example to fix that unicode bug.
And once again, if FirstName=, first in the line, I have no problems using the macro.
User avatar
wrznet
Basic User
Basic User
 
Posts: 11
Joined: Sun Aug 29, 2004 11:00 pm

Next

Return to Macros