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

(1/4) > >>

Contro:
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:
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:
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!

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

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

Best Regards

mouser:
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:
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.-Contro (July 02, 2014, 05:41 AM)
--- End quote ---

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

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

Navigation

[0] Message Index

[#] Next page

Go to full version