Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Printing Fixed Width Strings and Spliting String into Multiple Linesby mmartin (Monk) |
on Feb 07, 2012 at 22:05 UTC ( [id://952367]=perlquestion: print w/replies, xml ) | Need Help?? |
mmartin has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks, I was hoping someone could lend me a hand. I have a Perl script that works alongside MRTG building cfg files. This isn't really relevant to know what I want to do but I thought it couldn't hurt. MRTG has a Perl script called "cfgmaker" which as the name suggests builds cfg files. At the top of these config files it automatically generates a very small comment block. What I would like to do is open that file read in it's contents, then print back out to the file with a modified comment block at the top. I have it pretty much where I want it except for making some of the strings a fixed width so that it doesn't span all the way to the end of the string on the same line. Because for example, one of the strings (which is the actual command executed from the command line) could be anywhere from 250-300+ characters. This is what I have so far. (The variable $module_cmd will be gotten dynamically later when I add this to the full/production script, using ($0 and @ARGV): Also it will only print out to the screen right now. After I get it working I will print it back to the file. All I really am trying to figure out is how to make the strings $module_cmd and $cfgmaker_cmd be split into multiple lines depending on it's length. I know this can be done using printf/sprintf. But not really sure where to begin with it. Here is the output from the code above (printing only $comment) ############################################################################################################### ## Date: Feburary 7, 2012 ## Module CMD: ./add_userDefined --cfg=Router-Memory_and_Storage.cfg --host-template=In-Progress/Memory-and-Storage.htp --if-template=In-Progress/Memory.template --ip=10.10.10.10 ## cfgmaker CMD: /jwpmrtg/bin/cfgmaker --nointerfaces --host-template=/jwpmrtg/mrtgwww/cfg/templates/In-Progress/NetTools_Memory-and-Storage.htp --global "routers.cgi*Icon: router2-sm.gif" --output /jwpmrtg/mrtgwww/cfg/build_userDefined/NetTools-Percentage.cfg jwpadmin!@192.168.5.6 ############################################################################################################### What I would like it to look like is to have it print with each line ending with the same length as the "#####...####" border lines, with closing "#" at the end of each line): I tried printing an example of what I want it to look like but can't seem to get it to look right on here. I'm pretty sure I would be using something like... printf "%-100s #", $module_cmd; Any help would be much appreciated. Thanks, Matt
Back to
Seekers of Perl Wisdom
|
|