ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

Delete lines in a txt files according rules

<< < (2/4) > >>

narny:
@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.

cschw:
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 http://strawberryperl.com
....

-narny (July 02, 2014, 08:31 AM)
--- End quote ---

Narny,

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?

Target:
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 http://strawberryperl.com-narny (July 02, 2014, 08:31 AM)
--- End quote ---

I know nothing about perl, but I'm curious why strawberry perl  :huh:

cschw:
I was wondering the same thing! (And--per your signature--I should probably just stop here and :grunt:)

However...this seems to be in keeping with the longstanding tech fruit fetish.  (Raspberry Pi, Strawberry Perl, etc.)  Our communications folks recently put all mobile phone users through a fruit exchange (Blackberrys exchanged for Apples).

Which leads to my final question:  How far off-topic is too far?

narny:
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?
-cschw (July 02, 2014, 03:14 PM)
--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version