CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Taking input, then deleting block of text

 



tbone587
User

Feb 16, 2011, 7:09 AM

Post #1 of 9 (1056 views)
Taking input, then deleting block of text Can't Post

Hey everyone, I am new to Perl and trying to figure out how to take a variable that is inputted to locate a block of text, and then remove the block that contains that information. Below is what I am trying to do:

1. User enters TSMITH
2. Script locates that text within a file:

define service{
use generic-service ; Name of service template to use

host_name TSMITH
service_description PING
is_volatile 0
check_period 24x7
max_check_attempts 3
normal_check_interval 5
retry_check_interval 1
contact_groups customer
notification_interval 120
notification_period 24x7
notification_options c,r
check_command check_ping!100.0,20%!500.0,60%
}
3. Once it identifies that text it deletes everything from define service{ to }.


Is that possible?


BillKSmith
Veteran

Feb 16, 2011, 1:33 PM

Post #2 of 9 (1035 views)
Re: [tbone587] Taking input, then deleting block of text [In reply to] Can't Post


Quote


Is that possible?



Yes. Start by setting $INPUT_RECORD_SEPARATOR to '}'. (Refer perldoc perlvar)

Read file record by record.

If record does not contain input string, write it back out.

If it does, delete the unwanted text and write the remainder.

You can save code by using the -p and -i flags. (Refer perldoc perlrun)
Good Luck,
Bill


tbone587
User

Feb 17, 2011, 6:07 AM

Post #3 of 9 (1027 views)
Re: [BillKSmith] Taking input, then deleting block of text [In reply to] Can't Post

Bill,

What would be the code for this be to open the file as writable, use the input separator, and then clear the block of text?


BillKSmith
Veteran

Feb 17, 2011, 8:50 AM

Post #4 of 9 (1024 views)
Re: [tbone587] Taking input, then deleting block of text [In reply to] Can't Post

It really is not possible to delete text from a file. You must print a new file with the corrections. My previous reference explains how the -i option takes care of the file opening and renaming. If you do not want to use the option, you should still use its method.

The -p option takes care of the looping and printing. Set $_ to the null string to prevent printing of a line. There may be some advantage in writing all you own code, but you still have to do much the same thing.
Good Luck,
Bill


tbone587
User

Mar 25, 2011, 6:49 AM

Post #5 of 9 (977 views)
Re: [BillKSmith] Taking input, then deleting block of text [In reply to] Can't Post

I am still unsure on how to do this. I deleted the text a different way using sed, but its not reliable if there are similar names its locating. Can you please provide an example on how to locate the variable inputted and delete that block of text?


Karazam
User

Mar 25, 2011, 3:29 PM

Post #6 of 9 (967 views)
Re: [tbone587] Taking input, then deleting block of text [In reply to] Can't Post


Code
#!/usr/bin/perl 
use warnings;
use strict;

my $match = shift or die "Am I supposed to read your mind?!"; # user input

$/ = '}'; # same as $INPUT_RECORD_SEPARATOR
open my $fh, '<', '/name/of/input/file' or die $!;

while (<$fh>) {
print unless /$match/; # might need some adjustment
}


The default value of $/ is newline, hence we usually get one line at a time when reading a file.
By setting it to another value we can chunkify the input differently, in this case '}' becomes
the end-of-chunk marker (more correctly, input record separator).

The /$match/ part might need to be adjusted if you want to match whole words only,
beginning/end of line, etc.


tbone587
User

Mar 25, 2011, 4:41 PM

Post #7 of 9 (963 views)
Re: [Karazam] Taking input, then deleting block of text [In reply to] Can't Post

Thank you Karazam. Prior to your post I just figured out a very similar method. My code is below if you feel like there are ways to simplify. Also what does the my $match = shift or die "Am I supposed to read your mind?!"; # user input



Code
open SERVICEFILE, "<services.cfg" or die $!; 

open TMP, ">tmpfile" or die $!;

my ($Separator) = "}";
$INPUT_RECORD_SEPARATOR = $Separator;

my $DeleteService = "$Host_Name";
$DeleteService =~ m/^${Host_Name}$/;

while ( <SERVICEFILE> )
{
next if /$DeleteService/;# Skips line if it matches Host
print TMP;
}

close(SERVICEFILE);
close(TMP);

my $CopyTMP = "cp tmpfile services.cfg";
system $CopyTMP; #Copies Temporary File to hosts.cfg

my $RemoveTMP = "rm tmpfile";
system $RemoveTMP; #Removes Tempory File



BillKSmith
Veteran

Mar 25, 2011, 6:22 PM

Post #8 of 9 (960 views)
Re: [tbone587] Taking input, then deleting block of text [In reply to] Can't Post

The short form uses the -pi flags.


Code
#!perl -pi~ 
$/ = '}';
if (/TSMITH/) {
$_ = '';

Good Luck,
Bill


Karazam
User

Mar 26, 2011, 12:23 AM

Post #9 of 9 (954 views)
Re: [tbone587] Taking input, then deleting block of text [In reply to] Can't Post


Quote
what does the my $match = shift or die "Am I supposed to read your mind?!"; # user input


In your first post you said


Quote
1. User enters TSMITH


So that line captures user input, i.e. if the user calls the program with


Code
myscript.pl TSMITH


then TSMITH will be saved in $match.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives