ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello,
I'm in need of some help with this script I've written. Any assistance in getting it working would be much appreciated! I want the script to read all filename arguments and do the following to them:
1) Match something in the current file that looks like a website name and store it in a variable. I know there's only one unique website name per file. Should look like $sitename = www.sitename.com
2) Add several lines to the the current file (preferably, but not necessarily, the end-of-file) using regex substitute, including the $sitename var.
Here is the script with comments:
Code:
#!/usr/bin/perl -w
#this script will add action URLs to the nagios config files, right after the first '}' it encounters
use strict;
$^I = ".bak"; #use .bak backup extension
my $sitename; #relative sitename for each file
& get_the_sitename($sitename)
& write_the_action_url()
sub get_the_sitename { # loop thru the file and look for something like a sitename, store it in $sitename and end the IF loop if successful
while (<>) {
if (/host_name\s+((?:\w+\.\w+\.\w+))/) {
$sitename = $1;
last if (1==1);
}
}
}
#the problem with the ABOVE block of code is that it writes a blank file, but I only wanted to get the $sitename var to use in the substitution below.
sub write_the_action_url{ #add several new lines to the file using $sitename so new URL additions are relative
while(<>){
if (/}/) { #match the first right curly
#use substitute to add all these lines after the first } curly is found. Since There are several } curlys in the file, stop after it finds the first one and makes the substitutions.
s:(}):}\n\n\#\# BEGIN EXTINFO\ndefine hostextinfo{\n\thost_name\t$sitename\n\taction_url\t$sitename/mrtg\n\ticon_image\torb2.gif\n\ticon_image_alt\tMRTG\n\n}\n\ndefine serviceextinfo{\n\thost_name\t$sitename\n\tservice_description\tServerStatus\n\tnotes\tLinks to ServerStatus page\n\taction_url\t$sitename/xr/status.xml\n\ticon_image\torb1.gif\n\ticon_image_alt\tStatus Page\n}\n\ndefine serviceextinfo{\n\thost_name\t$sitename\n\tservice_description\tHTTP\n\tnotes\t$sitename\n\taction_url\t$sitename\n\ticon_image\thome.gif\n\ticon_image_alt\tHomepage\n}\n\n$1\n\n\#\# END EXTINFO:;
#print $sitename;
last if (1==1);
}
}
}
Sorry for the confusion. I've cut out all the non-essentials just to show what I'm trying to do. To reiterate, I just want to batch edit a bunch of files: 1) match the sitename and store it in a variable, then 2) add new lines with the URL provided from $sitename to maintain relativity.
Code:
#!/usr/bin/perl -w
#this script will, for each file, match a sitename www.sitename.com then add a new
#relative URL to the file. Adds the new lines after the first '}' it matches.
use strict;
$^I = ".bak"; #use .bak backup extension
my $sitename; #relative sitename for each file
& get_the_sitename($sitename)
& write_the_action_url()
sub get_the_sitename { # loop thru the file and look for something like a sitename, store it in $sitename and end the IF loop if successful
while (<>) {
if (/host_name\s+((?:\w+\.\w+\.\w+))/) { #match "host_name www.sitename.com"
$sitename = $1; # put the results of the match in $sitename
last if (1==1); # there are multiple instances of www.sitename.com in the file, so stop the loop after the first one is encountered, then proceed.
}
}
}
#the problem with the ABOVE block of code is that it writes a blank file, but I only wanted to get the $sitename var to use in the substitution below.
sub write_the_action_url{ #add several new lines to the file using $sitename so new URL additions are relative
while(<>){
if (/}/) { #match the first right curly
#use substitute to add all these lines after the first } curly is found. Since There are several } curlys in the file, stop after it finds the first one and makes the substitutions.
s/}/}\nline1\t$sitename\nline2\t$sitename/ #after the first curly, add a few lines of text with the contents of $sitename from the previous loop.
last if (1==1); #there are muliple } curly files, so end the script after this to keep from adding redundant information.
}
}
}
Yes, I just have to say thank you very much. That is much more eloquent form and worked great. undef $/ to change the read var method was what I needed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.