-1

Original file

# cat testfile
one
two
three 
four

Changes needed on the file

# cat testfile
one
two
three mynum <-- string is added here
four

I came across file_line module which helps in appending to the end of the file.I could not find any pointers which matches my need to append to the existing line.

Is there anything to regex the string "three" and append to the same line? or any other alternatives.

chetan honnavile
  • 398
  • 1
  • 6
  • 19

1 Answers1

1

As mentioned in the comments, you can use the match option to file_line for this.

Try

file_line { 'add mynum to line starting with three':
  path   => '/fullpath/testfile',
  line   => 'three mynum',
  match  => '^three ',
}

The match option is documented at https://forge.puppet.com/puppetlabs/stdlib/reference#match.

You can factor the pattern out into a variable if needed:

$line_pattern = 'three'

file_line { 'add mynum to line starting with three':
  path   => '/fullpath/testfile',
  line   => "${line_pattern} mynum",
  match  => "^${line_pattern} ",
}
Jon
  • 3,573
  • 2
  • 17
  • 24
  • Line "three" is just a place holder on that line. Line can be very big indeed. "Mynum" must be appended at the end of line. – chetan honnavile Mar 16 '20 at 20:15
  • That's fine. The string `three` can be factored out into a variable, and Puppet can handle long lines https://stackoverflow.com/questions/11406234/puppet-how-can-i-wrap-a-command-into-two-line-if-80-characters – Jon Mar 17 '20 at 10:12
  • im looking for variable placeholder, is that possible? e.g.so i can PREPEND the line? `line => '$1 three mynum'` – blamb Feb 26 '21 at 06:15