- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Perl script for paragraph formatting
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-04-2007 10:19 PM
тАО07-04-2007 10:19 PM
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....
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-05-2007 05:49 AM
тАО07-05-2007 05:49 AM
SolutionMaking 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-05-2007 06:23 PM
тАО07-05-2007 06:23 PM
Re: Perl script for paragraph formatting
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-06-2007 12:43 PM
тАО07-06-2007 12:43 PM
Re: Perl script for paragraph formatting
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-09-2007 12:37 AM
тАО07-09-2007 12:37 AM
Re: Perl script for paragraph formatting
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.