General
cancel
Showing results for 
Search instead for 
Did you mean: 

Perl script for paragraph formatting

SOLVED
Go to solution
Dodo_5
Frequent Advisor

Perl script for paragraph formatting

i have a perl script which takes file.txt as input.then it searches for .TXT in that and if it finds that it deletes the whole paragraph containing it...(files and codes are attached)

but i want to take a input pattern as per user requirement (say i want to search for word "production" or "on").And want to delete the paragraph containing the word.

Please can you help me to generalise the script like that....
4 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl script for paragraph formatting

Hi:

Making the minimum changes yields this:

#!/usr/bin/perl
use strict;
use warnings;
my $pattern = shift or die "pattern expected\n";
open (LIST,"file.txt") || die "where is file?\n";
$^I = '';
$/ = '';
while () {
next if /^.*$pattern.*\n/;
print;
}
close (LIST);

...By "minimum" changes, please note that 'use strict' will save you countless hours of debugging needlessly. Using 'use warnings' is far more robust than the older '-w' switch.

Regards!

...JRF...
Dodo_5
Frequent Advisor

Re: Perl script for paragraph formatting

The script is very usefull and my purpose has been solved..

another thing i want to know that if i put like that

print "which pattern want to check";
my $pattern=;

then also it is working...but how to check that if wrong pattern has been given by user then to give output like "Pattern Not found" in the file
James R. Ferguson
Acclaimed Contributor

Re: Perl script for paragraph formatting

Hi (again):

OK, based upon your new requirements, see if this fits your need:

#!/usr/bin/perl
use strict;
use warnings;
my $file = 'file.txt';
my $pattern = '';
my $matches = 0;

while ( $pattern eq '' ) {
print "Enter pattern to check\n";
chomp( $pattern = );
if ( $pattern =~ m{^\s+$} ) {
$pattern = '';
next;
}
}
$^I = '';
$/ = '';
open( LIST, "<", $file ) or die "Can't open $file: $!\n";
while () {
$matches++, next if /.*$pattern.*\n/;
print;
}
warn "\n> Pattern '$pattern' wasn't found in '$file' <\n" if $matches == 0;
close(LIST);

=snip=

As you requested, the pattern for which to search is read from user input. Input is solicited by prompting the user until something is provided. Whitespace (\s)-only lines are rejected.

If *no* matches are found for the pattern provided, the script terminates with a message indicating that. This warning is written to STDERR giving you the ability to redirect STDERR to /dev/null when you execute the script if you desire.

I've enhanced the open() to explicitly specify an open for input only and to report the file name and the reason for any open failure.

Regards!

...JRF...
Dodo_5
Frequent Advisor

Re: Perl script for paragraph formatting

hi,
Thanks very much for ur help regarding my problem...the script is very much usefull.

I have also done a script regarding my requirement,but it's a bit lenghthy..
check it out that in the attachment.