Cfengine configuration directory

Using ReplaceLinesMatchingField

Relevant classes: any

Replacing lines of text, in field separated files can be assisted with the editfiles command ReplaceLinesMatchingField.

One might typically use this to replace lines in a Unix password file, based on the first field (which is the user name). e.g.


#
# Example to replace two lines of /tmp/test with 2 lines from /tmp/cmp
# by matching with the first field.
#

control:

 actionsequence = ( editfiles )

editfiles:

 ###  Reference file

{ /tmp/compare

AutoCreate
AppendIfNoSuchLine "mark:male:unix"
AppendIfNoSuchLine "vera:female:mac"
}

 ### File to be edited by field.

{ /tmp/test

AutoCreate
AppendIfNoSuchLine "mark:..................."
AppendIfNoSuchLine "vera:..................."
AppendIfNoSuchLine "sally:.................."

   ResetSearch "1"
   SplitOn ":"
   ForEachLineIn "/tmp/compare"
      ReplaceLinesMatchingField "1"
   EndLoop
}

Back to documentation