Count specific character in a line (check CSV file)

Help with writing and playing macros

Count specific character in a line (check CSV file)

Postby phelan » Tue Dec 11, 2007 12:14 am

I need to write couple of macros to do the following.

1. count the number of | in 1st line of file and somehow let me know the total (display window?)

2. search remainder of document for lines with more than the total returned in step 1.

currently I count by hand and then use a macro searching for

Code: Select all
*|*|*|......


with *| being used for each | I need to find.

Some of the files have a large number of | though and manual counting is prone to error.

so far my few skill have proved unable to create a macro that will count a character in a single line

I know I can run a search for whole file and get it to "count all"... but how do i specify for a single line? unless something like

Code: Select all
Loop
while EOL = false
Find "|"
End Loop


Thanks in advance

Version of UE is 11.10a
User avatar
phelan
Newbie
 
Posts: 9
Joined: Mon Jul 02, 2007 11:00 pm

Re: Count specific character in a line

Postby Mofi » Tue Dec 11, 2007 9:14 am

So you want a CSV file check macro as requested also at forum topic Counting TABs in a line and others would like to have too. I have already downloaded the CSV specification as described at Wikipedia - Comma-separated values, but did not start to write the macro because it is really not easy, especially in the macro environment. It's much easier with a script, but still not simple.

According to the macro I have posted at Counting TABs in a line here is something similar for you using | as delimiter instead of tabs and works on the number of | in the first line instead of a fixed number.

It works only for files with DOS line terminations. Replace all ^p with ^n if you have a UNIX file opened in UNIX mode.

The line with wrong number of columns will start after macro execution with !!! . It is a quick and dirty solution which works only for simple CSV files with no specials like | inside "...".

The macro was only tested with UE v13.20a on a small example I have quickly created.

The macro property Continue if a Find with Replace not found or Continue if search string not found must be checked for this macro.

InsertMode
ColumnModeOff
HexOff
UnixReOff
Bottom
IfColNum 1
Else
"
"
EndIf
Top
Find RegExp "%»"
Replace All "MaRkErChAr1"
Find "«"
Replace All "MaRkErChAr2"
Find "^p"
Replace All "«^p"
DupeLine
Top
SelectLine
Find RegExp "[~|^p]"
Replace All SelectText ""
Top
SelectLine
Find "|"
Replace All SelectText "[~|«]++|"
Top
"%^("
Key END
"«^)"
Clipboard 9
StartSelect
Key HOME
Cut
EndSelect
Find RegExp "^c"
Replace All "»^1"
Delete
Paste
Key LEFT ARROW
Key LEFT ARROW
Key LEFT ARROW
"[~|«]+"
Key END
StartSelect
Key HOME
Cut
EndSelect
Delete
Find RegExp "^c"
Replace All "»^1"
Find RegExp "%^([~»]*^)«"
Replace All "!!!^1«"
Find RegExp "%»"
Replace All ""
Find RegExp "«$"
Replace All ""
Find MatchCase RegExp "%MaRkErChAr1"
Replace All "»"
Find MatchCase "MaRkErChAr2"
Replace All "«"
ClearClipboard
Clipboard 0

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

Re: Count specific character in a line

Postby phelan » Thu Dec 20, 2007 3:55 am

thank you...

your a life saver
User avatar
phelan
Newbie
 
Posts: 9
Joined: Mon Jul 02, 2007 11:00 pm


Return to Macros