Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Any Simple Examples of File::Tasks::Edit

by Preston@Large (Initiate)
on Dec 30, 2011 at 18:26 UTC ( [id://945661]=note: print w/replies, xml ) Need Help??


in reply to Re: Any Simple Examples of File::Tasks::Edit
in thread Any Simple Examples of File::Tasks::Edit

Not really. It just seemed like a logic entry point to start with. And yes, If I don't find the given entry in the file I the script will then append to the bottom or by some marker within the file.
#!/usr/bin/perl ###################################################################### ##################################################################### #################################################################### ################################################################### # Set Variable file modification #**************************************************************** $datefile = `date '+%m%d%y'`; # Copy current /etc/hosts file to the work directory (/etc/work/hostsf +ile) #************************************************************* # Search and file section for hosts file entry #Main Program format STDOUT = @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> +> "********************************************************************* +" @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> "*******************************************************************" @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> "****************************************************************" @||||||||||||||||||||||||||||||||||||||| "ETC Hosts File Inplace Edit Program" @||||||||||||||||||||||||||||||||||||||||||||||||||||| "Inplace Edit (Work Directory: /etc/work/hostfiles)" @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> "****************************************************************" @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> "*******************************************************************" @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> +>> "********************************************************************* +*" . write; ########################### Main Program Section ##################### +### print "\n"; print "This program is will copy the current etc/hosts file to the \n" +; print "working directory (/etc/work/hostfile) to perform the \"Inplace +\,\" \n"; print "edit of the new or modified directories. \n"; print "\n"; print "Should I continue with the process, or exit for another time? ( +Yes|No): "; $choice1 = <STDIN>; chomp($choice1); while ($choice1 =~ m/No|NO|no|N|n/) { print "Now exiting the program. \n"; print "Please wait . . . .\n"; `sleep 5`; exit; } print "Please enter the following the following information . . .\n"; print "New System IP Address (Example - 10.12.13.14): "; $NewIP = <STDIN>; chomp($NewIP); print "Absolute name of system or device (Example - neptune.itdo.pri): + "; $NewLgName = <STDIN>; chomp($NewLgName); print "Short name of system or device (Example - neptune): "; $NewStName = <STDIN>; chomp($NewStName); ######################## Array ####################################### +## @input = ($NewIP, $NewLgName, $NewStName); ######################## Sub Routine Call Section #################### +### &copyFile; &editPlace(*input); ########################### Sub Routine Section ###################### +### sub copyFile { `cp /etc/hosts /etc/work/hostfiles/hosts`; $TargetDir = "/etc/work/hostfiles"; $TargetFile = "/etc/work/hostfiles/hosts"; `cp $TargetFile /etc/work/hosts.workcopy`; `chmod 777 /etc/work/hosts.workcopy`; `mv $TargetFile $TargetFile.orig.$datefile`; } sub editPlace { #$datefile = `date '+%m%d%y'`; local(*thisIP) = @_; $WorkCopy = "/etc/work/hosts.workcopy"; open(HOSTS, $WorkCopy) || die "Can't open hosts.workcopy $!\n" +; LINE: while(<HOSTS>) { next LINE if /^#/; next LINE if ! /$thisIP[0]/; ($IP, $longhostname, $shorthostname, $devicealias)=split(" +"); print "$IP\t $longhostname\n"; } close(HOSTS); } It's not done, but you can see the idea arising.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://945661]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2024-04-25 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found