Perl Programming help
Hello, linuxquestion users. I have the following Perl problem I have been banging away at and have been stuck with for a long time. The issue is with a some basic search and replacement within a file.
Here is the code I got so far. First I tried opening the file then I slurped it in for experimentation. My goal was to patter match "flushing-" store it into an array then loop through the file and replace "Windows" in Windows-service with the stored "location" number. Code:
$IN = "$INFILE"; Here is what my data looks like originally: Code:
This is the service file for or Nagios.cfg. I basically need to strip out the location such as flushing, alabama, kentucky and replace the "Windows-service" with "location-service." My final Services.cfg file with look like so: Code:
define service { Thanks for all the help |
There is too much confusion in your code. For example:
Code:
local $/; # Set input to "slurp" mode. And in Perl you can do almost anything and much more than in sed/awk. And this: Code:
if (@string =~ /(Windows-)/) |
awk
Code:
awk ' |
Quote:
I'm talking about giving fish or teaching to fish. |
Thanks for the help guys let me stumble around with it some more and I will try to post back with a updated solution.
|
I will give you one tip. I don't think you really want slurp mode. That is, you don't want to save the entire file to a single scalar. That would make it very unwieldy to work with. Consider reading by paragraphs and then splitting on newlines, as a contrast:
Code:
#!/usr/bin/env perl Code:
hektor ~/practice $ perl fixer |
Double post. Sorry.
|
Quote:
Oh, also i would also expect you to be a role model and only give hints to answers next time for every post you make? :) |
All times are GMT -5. The time now is 07:05 PM. |