Zeus Macro for deleting duplicate lines?
Posted: Tue Sep 27, 2016 5:11 am
I've got a file of a few thousand email addresses and I'd like to delete all the exact duplicates but I can't figure out how to do it... anyone?
Use this forum to ask for help, submit a bug report or make a suggestion.
http://www.zeusedit.com/zBB3/
Code: Select all
function key_macro()
screen_update_disable()
-- save current search settings
search_options_save()
-- save the current cursor details
cursor_save()
-- the different available scope options
SCOPE_FORWARD = 0 -- forward from current cursor
SCOPE_REVERSE = 1 -- reverse from current cursor
SCOPE_MARKED = 2 -- marked region only
SCOPE_ENTIRE = 3 -- entire contents of current document
SCOPE_ALL = 4 -- all currently open documents
-- set the required search scope option
set_search_option("Scope", SCOPE_ENTIRE)
-- set the other search options
set_search_option("UseCase" , 0)
set_search_option("WholeWord" , 0)
set_search_option("RegExpress", 1)
-- do the search and replace for the next match only
replace_all = 0
-- do the search and replace for all matches found
replace_all = 1
-- search for any empty lines and replace them with a single empty line
replace("^(\\n$)(\\1$)+" , "\\n", replace_all)
-- restore the cursor details
cursor_restore()
search_options_restore()
screen_update_enable()
screen_update()
end
key_macro() -- run the macro
Code: Select all
replace("^(\\n$)(\\1$)+" , "\\n", replace_all)
Code: Select all
SCOPE_ALL = 4 -- all currently open documents
Code: Select all
-- search for any duplicate lines with with a single line of text
replace("^(.*)(\\r?\\n\\1)+$" , "\\1", replace_all)
I would use Python to do this and since Zeus comes with a version of Python, you will not need to install anything additional to have this work.I have a need to randomize the lines in a text file
Code: Select all
from random import shuffle
def ShuffleFile(inputFileName, outputFileName):
# read in the input file
with open(inputFileName, 'r') as fileInput:
lines = [i for i in fileInput.readlines()]
# shuffle the fines
shuffle(lines)
# write the output file (change to overwrite same file)
with open(outputFileName, 'w+') as fileOutput:
for item in lines:
fileOutput.write(item)
in_file="d:/temp/input.txt"
out_file="d:/temp/output.txt"
# call the shuffle function
ShuffleFile(in_file, out_file)
Code: Select all
python.exe shuffle.py
Code: Select all
#
# Name: Random Marked Area
#
# Author: Jussi Jumppanen
#
# Language: Python
#
# Description: Using a marked region of text, randomize the marked lines
# of text and replace them with a random result set.
#
import zeus
import random
def key_macro():
if zeus.is_document():
if zeus.is_read_only() == False:
if (zeus.is_marked() == 1):
lines = []
zeus.cursor_save()
# get the marked area details
mode = zeus.get_marked_mode()
top = zeus.get_marked_top()
bottom = zeus.get_marked_bottom()
left = zeus.get_marked_left()
right = zeus.get_marked_right()
delta = bottom - top
# copy and shuffle the lines
for index in range(top, bottom + 1):
text = zeus.get_line_text(index)
lines.append(text)
random.shuffle(lines)
zeus.MarkDelete()
# insert the new lines and restore the markings
for line in lines:
zeus.line_insert(top, line)
zeus.set_marked_area(mode, top, left, bottom, right)
zeus.cursor_restore()
else:
zeus.message('This requires a marked region of text.')
zeus.beep()
else:
zeus.message('This macro only works for a writable document.')
zeus.beep()
else:
zeus.message('This macro only works for documents.')
zeus.beep()
key_macro() # run the macro