topbanner_forum
  *

avatar image

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

Login with username, password and session length
  • Thursday December 12, 2024, 10:55 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

Author Topic: Delete lines in a txt files according rules  (Read 9840 times)

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Delete lines in a txt files according rules
« on: July 02, 2014, 05:41 AM »
Delete lines in a txt file according rules

By example :
with a script with a hotkey or similar delete in an open txt file the pairs lines.

Or delete all lines except the multiples of 5.

And so on

Do you know a good application for this ?

Best Regards
 :-*

narny

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 4
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #1 on: July 02, 2014, 08:31 AM »
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

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!

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #2 on: July 02, 2014, 09:31 AM »
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

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!


Thanks Narny . I would like a tool more easy, not a new language.

Best Regards

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,914
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #3 on: July 02, 2014, 10:25 AM »
Welcome to the site, Narny!
Contro -- the nice thing about Narny's solution is you can modify and extend it to do other things later.  Since you frequently need to do text manipulation, why not invest some time and learn a little perl so you can write your own scripts for such things?

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,287
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #4 on: July 02, 2014, 11:23 AM »
Delete lines in a txt file according rules
By example :
with a script with a hotkey or similar delete in an open txt file the pairs lines.
Or delete all lines except the multiples of 5.

Didn't I already write you a tool to do this?

https://www.donationcoder.com/forum/index.php?topic=36898.0

narny

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 4
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #5 on: July 02, 2014, 11:43 AM »
@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

  • Supporting Member
  • Joined in 2012
  • **
  • default avatar
  • Posts: 50
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #6 on: July 02, 2014, 03:14 PM »
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,

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

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,832
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #7 on: July 02, 2014, 05:42 PM »
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

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

cschw

  • Supporting Member
  • Joined in 2012
  • **
  • default avatar
  • Posts: 50
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #8 on: July 02, 2014, 05:55 PM »
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

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 4
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #9 on: July 03, 2014, 07:11 AM »
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.

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #10 on: July 04, 2014, 08:08 AM »
Welcome to the site, Narny!
Contro -- the nice thing about Narny's solution is you can modify and extend it to do other things later.  Since you frequently need to do text manipulation, why not invest some time and learn a little perl so you can write your own scripts for such things?

Understood Mouser.
Welcome Narny!

Best Regards
 :-*

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #11 on: July 04, 2014, 08:11 AM »
Delete lines in a txt file according rules
By example :
with a script with a hotkey or similar delete in an open txt file the pairs lines.
Or delete all lines except the multiples of 5.



Didn't I already write you a tool to do this?

https://www.donationcoder.com/forum/index.php?topic=36898.0

Ejem. Sure. But remember i don't remember very well.....
I solved this problem yesterday with pspad and a macro consisting - in that particular case - in delete navigating the lines 2, 3, 4 from the first line. Execute one. Then execute again. Or finally executing the macro x times.

But Mouser is right . I have to document the phrases to find quickly answers. Or program by myself. But the last option..... Aarrrgghhhhh.
I would like to have a powerful mind !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 :P

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #12 on: July 04, 2014, 08:14 AM »
@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.


Ejem. I must try this to correspond you.
I will inform you of the answers and keep this url to study

Best Regards

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #13 on: July 04, 2014, 08:18 AM »
Delete lines in a txt file according rules
By example :
with a script with a hotkey or similar delete in an open txt file the pairs lines.
Or delete all lines except the multiples of 5.

Didn't I already write you a tool to do this?

https://www.donationcoder.com/forum/index.php?topic=36898.0

Skwire i have to apologyze.

I am studying phrases to find quickly my rememberings.
I put some post in the forum about this.
And other solution is reindexing in the background to follow any string too.
I am trying.
So forgive me.
Your program is very nice and a need to remember for the next time.

narny

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 4
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #14 on: July 04, 2014, 09:30 AM »
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 site:donationcoder.com/forum

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.

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #15 on: July 05, 2014, 08:58 AM »
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 site:donationcoder.com/forum

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.

I would reindexing in the local place. In my PC.
But it's a nice idea to try to reindex in the web.
 :-*

Nevertheless I continue searching for a way to reindex in the background or similar for my old DBF files
 ;D

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Delete lines in a txt files according rules
« Reply #16 on: July 06, 2014, 04:13 PM »
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 site:donationcoder.com/forum

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.

This afternoon - i have spent more than a week studying concepts like synonymizers, phrasing, ontology, semantic, thesaurus,,......


I found the ontology concept and the possibility of recopile in the web history or similar all the work we made in web sessions.
I would to find something for this with future.
A powerful tool to complement my notes i take to goldmine 5.5.
Then I will have a specialyzed memory of the browsing by the web.
The problem may be i used several browsers.
So i would like.....

continues in this post :

https://www.donation...?topic=38357.new#new

 :-*