How to delete every second line?

Find, replace, find in files, replace in files, regular expressions

How to delete every second line?

Postby YanHan » Mon Nov 27, 2006 3:39 am

Does anyone have any ideas how to do this automatically?
User avatar
YanHan
Newbie
 
Posts: 5
Joined: Sun Nov 26, 2006 12:00 am

Re: Delete every second line?

Postby Mofi » Mon Nov 27, 2006 3:39 pm

A "simple" regular expression replace can do it:

In UltraEdit style:

Find What: %^(*^p^)*^p
Replace With: ^1

In Unix/Perl style:

Find What: ^(.*\p).*\p
Replace With: \1

Because of ^p or \p this works only for files with DOS line terminations (CR+LF). See in help of UE/UES the pages Find command or Replace command and Regular Expressions to understand it.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4051
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Delete every second line?

Postby YanHan » Tue Nov 28, 2006 4:56 am

Thanks, works perfecty, expect that the last line doesn't remove but it's easy to remove. :)

How about:
- Remove every last/first line?
- Remove every third line?
- Duplicate every line?
User avatar
YanHan
Newbie
 
Posts: 5
Joined: Sun Nov 26, 2006 12:00 am

Re: How to delete every second line?

Postby Bego » Tue Nov 28, 2006 10:22 am

The last line should always have a crlf to work properly.
1.: Use "TOP", "BOTTOM" and "DELETELINE" in an UE macro to do this job.
2.: Also a UE macro with key arrow down + deleteline + "EOF loop". Easy.
3.: Also a trivial UE macro with arrow down and duplicateline.

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

Re: How to delete every second line?

Postby YanHan » Tue Nov 28, 2006 3:35 pm

Thanks, Bego. But I don't get non of your advices working. :?

Is it possible to do these with the 'regular expression' feature?
User avatar
YanHan
Newbie
 
Posts: 5
Joined: Sun Nov 26, 2006 12:00 am

Re: How to delete every second line?

Postby Bego » Wed Nov 29, 2006 8:43 am

No. regexps are good for "inLINE-searching/replacing", not for navigating in a file.

Try this:
Create a macro called "delTopBottom"
Paste in those lines:

Code: Select all
HexOff
Top
DeleteLine
Bottom
DeleteLine

Let it run on your example file. It should do your issue no. 1

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

Re: How to delete every second line?

Postby YanHan » Wed Nov 29, 2006 2:10 pm

Thanks once again, Bego. But it's not even works now. :?

I figured out these which can do only at the top of the document also 'Play macro to End of File' have to use:

1. Remove every last/first line
Code: Select all
HexOff
Key DOWN ARROW
DeleteLine


2. Remove every third line
Code: Select all
HexOff
Key DOWN ARROW
Key DOWN ARROW
DeleteLine


3. Duplicate every line (does not works - does not duplicate first line and the macro loops infinitely :?)
Code: Select all
HexOff
Key DOWN ARROW
DupeLine
User avatar
YanHan
Newbie
 
Posts: 5
Joined: Sun Nov 26, 2006 12:00 am

How to delete every second line in a text file?

Postby Doug E » Fri Dec 05, 2008 11:28 am

It's a small macro that is supposed to delete every second line in a file but stalls in the beginning.

Code: Select all
InsertMode
ColumnModeOff
HexOff
Top
Loop 0
MoveLineDown
MoveLineDown
DeleteLine
ExitLoop
EndLoop
Top


What have I missed?
Doug E
Newbie
 
Posts: 5
Joined: Fri Nov 07, 2008 8:09 pm

Re: How to delete every second line in a text file?

Postby Mofi » Fri Dec 05, 2008 1:00 pm

The command MoveLineDown does not only move the cursor down, it moves the entire line down. And you exit your endless loop after deleting 1 line because ExitLoop is inside the loop without any condition.

You don't really need a macro for that job because a single regular expression replace can do that too. However, here is the macro.

InsertMode
ColumnModeOff
HexOff
UnixReOff
Bottom
IfColNumGt 1
"
"
IfColNumGt 1
DeleteToStartofLine
EndIf
EndIf
Top
Find RegExp "%^(*^r++^n^)*^r++^n"
Replace All "^1"

This macro should work for files with DOS or UNIX line terminations, but definitely not for MAC files.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4051
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to delete every second line in a text file?

Postby Doug E » Fri Dec 05, 2008 4:48 pm

Ohh man, I can't believe how complicated a macro that turned out to be 8O

Thanks Mofi!

Keywords for others: remove delete every 2nd other line macro script
Doug E
Newbie
 
Posts: 5
Joined: Fri Nov 07, 2008 8:09 pm


Return to Find/Replace/Regular Expressions