How to search and replace a word in file

1,336 views
Skip to first unread message

Ayyanar

unread,
Jul 23, 2015, 3:27:03 AM7/23/15
to Puppet Users
1."       sed -i 's/\$\$HISTORY\$\$/puppet/'                     /etc/system.com      "


I want to replace a word $$HISTORY$$ with puppet text into /etc/system.com file.


Thanks for your commands.
Ayyanar.

Ayyanar

unread,
Jul 23, 2015, 4:05:54 AM7/23/15
to Puppet Users, ayyan...@gmail.com


On Thursday, 23 July 2015 12:57:03 UTC+5:30, Ayyanar wrote:
1."       sed -i 's/\$\$HISTORY\$\$/puppet/'                     /etc/system.com      "


I want to replace a word $$HISTORY$$ with puppet text into /etc/system.com file.

Search $$HISTORY
Replace puppet

Christopher Wood

unread,
Jul 23, 2015, 8:51:37 AM7/23/15
to puppet...@googlegroups.com
Look into templates.

https://docs.puppetlabs.com/guides/templating.html

https://docs.puppetlabs.com/puppet/latest/reference/lang_template.html
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [1]puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> [2]https://groups.google.com/d/msgid/puppet-users/5ba5c95e-b91b-43a6-84bb-26c3f9c030d9%40googlegroups.com.
> For more options, visit [3]https://groups.google.com/d/optout.
>
> References
>
> Visible links
> 1. mailto:puppet-users...@googlegroups.com
> 2. https://groups.google.com/d/msgid/puppet-users/5ba5c95e-b91b-43a6-84bb-26c3f9c030d9%40googlegroups.com?utm_medium=email&utm_source=footer
> 3. https://groups.google.com/d/optout

jcbollinger

unread,
Jul 23, 2015, 9:43:46 AM7/23/15
to Puppet Users, ayyan...@gmail.com


On Thursday, July 23, 2015 at 2:27:03 AM UTC-5, Ayyanar wrote:
1."       sed -i 's/\$\$HISTORY\$\$/puppet/'                     /etc/system.com      "


I want to replace a word $$HISTORY$$ with puppet text into /etc/system.com file.



You have to take care to perform proper quoting/escaping for sed, possibly to quote/escape the result for the shell (only if you are running the command via the shell), and then to quote/escape that result for Puppet.  From sed's perspective, the program you want to run is

s/$$HISTORY$\$/puppet/

(The '$' is special in a sed pattern only when it appears at the end of the pattern, or, for GNU sed, at the end of a sub-expression.)

It will save you some trouble to cut the shell out of it, so supposing you are going to use a Puppet Exec resource to run sed, you should rely on the 'posix' provider (the default where it is available), and avoid the 'shell' provider.

If you don't need Puppet to interpolate any variables into the command string (as in your example) then it's not much harder than that:

exec { 'Fill system.com':
  command
=> '/bin/sed -i \'s/$$HISTORY$\\$/puppet/\' /etc/system.com'
}

Note that the backslash you want passed through to sed needs to be escaped to Puppet.  Quoting the whole command string with single quotes prevents Puppet from interpreting any of the dollar signs as introducing a variable interpolation.  You might not need -- and even might not want -- the internal single quotes around the sed program, but if you include them then they must be escaped as shown.

You can trick that out a bit, if you like.  Personally, I'd probably do this:

exec { 'Fill system.com':
  command
=> '/bin/sed -i \'s/$$HISTORY$\\$/puppet/\' /etc/system.com',
  onlyif
=> '/usr/bin/test -e /etc/system.com',
  refresh
=> '/bin/true',
  provider
=> 'posix'
}

Or in truth, if at all possible I would manage the whole file via Puppet.  A Puppet template might be helpful for that, but for the specific example you provided it would be overkill (no need to use a template to sub a constant string into a template at run time when you could just edit it into the master's copy of the file in the first place).


John

ayy...@orzota.com

unread,
Jul 27, 2015, 2:10:58 AM7/27/15
to Puppet Users, ayyan...@gmail.com, John.Bo...@stjude.org

Thanks John It is works for me.

 
Reply all
Reply to author
Forward
0 new messages