[ale] Perl or Python for editing files...

Brian Mathis brian.mathis+ale at betteradmin.com
Mon Oct 17 15:38:30 EDT 2011


On Mon, Oct 17, 2011 at 3:07 PM, Chuck Payne <terrorpup at gmail.com> wrote:
> On Mon, Oct 17, 2011 at 2:59 PM, Pete Hardie <pete.hardie at gmail.com> wrote:
>> sed -i replaces the file inline - test it well before you deploy, as
>> if it screws up, you no longer have the original to go back to
>>
>> On Mon, Oct 17, 2011 at 14:35, Chuck Payne <terrorpup at gmail.com> wrote:
>>> On Mon, Oct 17, 2011 at 2:17 PM, Jim Lynch
>>> <ale_nospam at fayettedigital.com> wrote:
>>>> On 10/17/2011 02:01 PM, Chuck Payne wrote:
>>>>> Guys,
>>>>>
>>>>> I have four files I like to be able to edit from a script. I have
>>>>> heard that it's better to use Perl or Python to this than bash.
>>>>>
>>>>> I am new to be perl and python from a scripting. What would be easy? I
>>>>> know it question that would open up a larger debate, but I really need
>>>>> help on this.
>>>>>
>>>>> If you recommend one of the two can you show me a site that explain
>>>>> how to open and write the files.
>>>>>
>>>>>
>>>>> Thanks,
>>>>>
>>>> It really depends on the complexity of the edit task.  A combination of
>>>> bash, sed and awk can do simple edits, driving ex with bash can do a bit
>>>> more complex operations, but perl or python would be best for really big
>>>> editing jobs.  So to answer your question give us a real world example
>>>> of what you want to do.  What's the input and the desired output?
>>>>
>>>> Jim.
>>>
>>> I want to build a script that would let me edit a kvm clone. I want to
>>> script to be able to edit let say /etc/hosts and couple other file, I
>>> want to replace ip and host name. So that I can make deploy easier and
>>> I don't have to touch all the files I need too.
>>>
>>> I guess if I could do with bash/sed/awk that work, the problem is how
>>> to do. I know how to use sed to change and write out a new file, but I
>>> rather just be able to edit the same file and save it.
>>>
>> --
>> Pete Hardie
>
> I know that too well.


If you are looking to do this for system deployment, you might want to
look into Puppet.  It has a bigger learning curve up front, but it
will pay off in the long run with better automation.

In general this kind of scripting can be error prone because you need
to worry about cases where you run the script and only 1/2 of it works
(maybe because a package config file changed).  The parts that did
work have to be smart enough not to run again (not an easy task).
Once you've got the logic in the script to avoid stuff like this,
you're already on your way to writing a full-blown management system,
so you might want to start there from the beginning.


-☙ Brian Mathis ❧-



More information about the Ale mailing list