T
Ted Zlatanov
This question was recently posted to the cfengine mailing list by a
cfengine user. Cfengine's 'editfiles' functionality in this
particular case is quite succinct. Can anyone do this in Perl to meet
the challenge? Of course it can be done in one line, but will it be
as elegant?
The actual sequence of commands is pretty obvious - the outer
BeginGroup/EndGroup is an if() statement, and
LocateLineMatching/IncrementPointer move the editing cursor; -3 means
to move 3 lines back in the file.
My own attempt used an array-file tie and was definitely not
as elegant.
Ted
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
How can I write a script that would perform something like the
following and would remain as elegant?
BeginGroupIfNoLineMatching "# Addition by EMC ended."
LocateLineMatching ".*. /etc/init.d/functions.*"
IncrementPointer "-3"
InsertLine "# Addition by EMC started."
InsertLine 'DHCP_HOSTNAME=${dollar}{HOSTNAME}'
InsertLine "# Addition by EMC ended."
InsertLine ""
EndGroup
Off course, this can be done, but I do not know of any scripting tool
that allows one to express the above changes so elegantly. Anybody
can translate this into sed/awk in about the same number of lines?
cfengine user. Cfengine's 'editfiles' functionality in this
particular case is quite succinct. Can anyone do this in Perl to meet
the challenge? Of course it can be done in one line, but will it be
as elegant?
The actual sequence of commands is pretty obvious - the outer
BeginGroup/EndGroup is an if() statement, and
LocateLineMatching/IncrementPointer move the editing cursor; -3 means
to move 3 lines back in the file.
My own attempt used an array-file tie and was definitely not
as elegant.
Ted
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
How can I write a script that would perform something like the
following and would remain as elegant?
BeginGroupIfNoLineMatching "# Addition by EMC ended."
LocateLineMatching ".*. /etc/init.d/functions.*"
IncrementPointer "-3"
InsertLine "# Addition by EMC started."
InsertLine 'DHCP_HOSTNAME=${dollar}{HOSTNAME}'
InsertLine "# Addition by EMC ended."
InsertLine ""
EndGroup
Off course, this can be done, but I do not know of any scripting tool
that allows one to express the above changes so elegantly. Anybody
can translate this into sed/awk in about the same number of lines?