Operating System - Linux
1753726 Members
4881 Online
108799 Solutions
New Discussion юеВ

Re: Perl: How to delete a paragraph fromtext file

 
SOLVED
Go to solution
Junior C.
Frequent Advisor

Perl: How to delete a paragraph fromtext file

I'm looking for the syntax to delete a paragraph using perl.

Example:

file.txt

1. This forum is for discussing both general and technical issues regarding Linux OS.TXT distributions running on or utilizing Hewlett-Packard products.

2. forum is for discussing both general and technical issues regarding Linux OS distributions running on or utilizing Hewlett-Packard products.

3. The forum is for discussing both general and technical issues regarding Linux OS distributions running on or utilizing Hewlett-Packard products.TXT

If any line contain .TXT I want to delete that paragraph and print the remaining paragraph to newfile.txt

from above newfile.txt should only have (prag 2.)

while (<>) {
print unless m/\b.TXT/i;

not working.


-JC.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Perl: How to delete a paragraph fromtext file

Hi JC:

# perl -nle 'BEGIN{$/=""};next if m{\.TXT};print' file

Regards!

...JRF...
Junior C.
Frequent Advisor

Re: Perl: How to delete a paragraph fromtext file

James,

Thanks for the reply.

I'm not sure how do I open/read the file, and then apply perl -nle 'BEGIN{$/=""};next if m{\.TXT};print' file

Thanks,

-JC.
Sergejs Svitnevs
Honored Contributor
Solution

Re: Perl: How to delete a paragraph fromtext file

Hi,

may be this script can solve your problem?

----file.pl----
#!/usr/bin/perl -w
open (LIST,"file.txt") || die "where is file?\n";
$^I = '';
$/ = '';
while () {
next if /^.*\.TXT.*\n/;
print;
}
close (LIST);
--------------

# ./file.pl > newfile.txt

BR
Sergejs
James R. Ferguson
Acclaimed Contributor

Re: Perl: How to delete a paragraph fromtext file

Hi (again) JC:

> I'm not sure how do I open/read the file, and then apply perl -nle 'BEGIN{$/=""};next if m{\.TXT};print' file

Well, my command-line script did it all for you. Adding your requirement for case-insensitive matching and a boundry (\b):

# perl -nle 'BEGIN{$/=""};next if m{\b\.TXT}i;print' file

Simply paste the line above and substitute the name of your 'file' for 'file', or drop the 'file' argument and pipe an output stream to Perl.

Notice that a "." signifies any character unless escaped.

Regards!

...JRF...
Junior C.
Frequent Advisor

Re: Perl: How to delete a paragraph fromtext file

Thread Close