avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Sunday August 9, 2020, 1:01 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Messages - narny [ switch to compact view ]

Pages: [1]
And other solution is reindexing in the background to follow any string too.

Google indexes every word in the background!
You can restrict the search to donationcoder's forum like so:

delete lines

This thread is the first hit for the above search term.

For my own notes, I use MyLifeOrganized. It must index every word too, because it narrows search results as fast as you can type. It scales nicely: opens my 16,000+ entry file in less than a second. The "light edition" is free. No Linux/Mac version might be a downside for some.

Thanks for the heads-up on Strawberry Perl.  I've downloaded and installed on an old laptop...will take a closer look at it later today.

Do you have any experience using the portable version of this?

I've got a copy of it on my USB stick, and I've run a quick "hello world" to make sure it works. Never used it in anger though - I just install Perl on any machine I use regularly so it's there if I need it. I don't think you need to do anything clever if you just want to run a quick script, as long as your command processor can see the perl.exe when you call it. So if you're running a batch file that calls perl.exe, you have to replace that perl.exe call with {path_to_the_portable_installation_on_your_USB_stick}\perl\bin\perl.exe. You don't have to do that if either Perl is installed, or you've first run portableshell.bat (see next para.)

The portable version ships with a portableshell.bat file that opens a command window and temporarily sets up various paths and environment variables so that you can use Perl it as if it's installed. The README.portable.txt file explains it more detail.

@mouser: Thanks. Long-time user of FARR.

@Contro: I can try to make it more enticing for you!

Download and install Perl. It plonks itself into a folder somewhere and updates the %path%, I think. I don't think it touches the registry. You can just leave it there and forget it and never use it for anything else. All it costs you is ~340 MB of disk space.

Open a text editor, and type (or copy/paste) the following line, saving as "DelExcept.bat"

for %%f in (%2) do perl -i.bak -ne "print unless $. %% %1" "%%f"

Now, from a command line in the folder you want to delete things, type:

DelExcept 5 MyFile.txt

It'll delete all lines except every 5th in MyFile.txt

DelExcept 3 *.txt

which will delete all lines except every 3rd in all files in the folder with .txt extension.

It will take a copy of the original files first, adding .bak to the original name and extension.

I haven't read any of your previous posts, but if you're doing text processing and haven't discovered Perl then you're missing out big time! It'll repay every minute you invest 100 times over. But you can use batch file without having to learn anything new - it'll still do the job.

Perl would do it. It's overkill, but you'll have a tool that can do just about anything.
If you're on Windows, download the latest from

To delete all lines except multiples of 5, open a command line and type:

perl -i.bak -ne "print unless $. % 5" myfile.txt

Replace myfile.txt with your filename, or a space-separated list of filenames.

The "-i.bak" tells Perl to do an inplace edit when it prints. It creates a backup before starting, adding ".bak" to the original filename.
"-n" runs a little program that iterates through each line of the file(s).
"-e" is the flag that is followed by your code (within quotes) that executes on each iteration.
The program itself ("print unless $. % 5") prints lines (i.e. puts them into the new file) unless the current line number ("$.") modulus 5 ("% 5") is true.
Any non-zero number is True in Perl. So when you hit a line that's exactly divisible by 5, the modulus becomes 0 and the expression becomes false and the line prints.
To reverse the logic, use "print if $. % 5". This keeps all lines except the ones exactly divisible by 5.

To work with wildcards, all *.txt files for example, you could type this:

for %f in (*.txt) do perl -i.bak -ne "print unless $. % 5" "%f"

You could put the one-liner into a batch file, or re-write the program as .pl Perl script file. You could modify the script to take a command line argument for the line number to skip (every 2, every 5, etc), or prompt for that value, or... You get the idea!

Pages: [1]