0

I have a file which contains the below

hosts:      files
ipnodes:    files
networks:   files
protocols:  files
rpc:        files
ethers:     files
netmasks:   files

I need to append "dns" at the end of only this two lines

hosts:      files dns
ipnodes:    files dns

Solaris servers, having very limited options in sed sed -i is not available looking for a workaround

judi
  • 89
  • 1
  • 1
  • 10
  • 3
    Possible duplicate of [Add at the end of the line with sed](http://stackoverflow.com/questions/2516343/add-at-the-end-of-the-line-with-sed) – Jose Ricardo Bustos M. Mar 01 '17 at 15:43
  • I have edited the question, having more lines in file, need to append the string next to a pattern matching, sed in solaris servers , -i is not available – judi Mar 01 '17 at 15:58
  • then [Add to the end of a line containing a pattern - with sed or awk](http://stackoverflow.com/questions/9591744/add-to-the-end-of-a-line-containing-a-pattern-with-sed-or-awk) – Jose Ricardo Bustos M. Mar 01 '17 at 16:12
  • The `-i` option in GNU `sed` is equivalent, roughly, to saving the output of `sed` in a temporary file, and then copying or moving the temporary file back over the original. The main difference is that `sed` does the copying automatically. – Jonathan Leffler Mar 02 '17 at 05:20

2 Answers2

2

sed without any fancy settings or in place updates would work:

sed 's/.*/& dns/' file

If you need to make this edit to ONLY lines beginning with lines beginning with hosts and ipnodes, then add a filter to the command:

sed -e '/^hosts/ s/.*/& dns/' -e  '/^ipnodes/ s/.*/& dns/' file

Note: I am assuming you'll redirect output to a new file, and replace it with the old.

gregory
  • 10,969
  • 2
  • 30
  • 42
  • I have edited the question, having more lines in file, need to append the string next to a pattern matching, sed in solaris servers , -i is not available – judi Mar 01 '17 at 15:57
  • 1
    @judi I've updated my answer. And, yes, I understand GNU options are not available on Solaris by default. – gregory Mar 01 '17 at 16:04
  • it works thank you very much ----- > **sed -e '/^hosts/ s/.*/& dns/' -e '/^ipnodes/ s/.*/& dns/' file** – judi Mar 01 '17 at 16:07
0

I'm surprised how minimal the Solaris sed really is. I would say use awk in that case but that seems broken as well. My recommendation is to not shell script on Solaris at all. Stop using Solaris and replace it by a reasonable OS if you want to work with shell scripting. Otherwise use a programming language like Python for scripting.


Former Answer:

The command should look like this:

sed 's/^\(hosts\|ipnodes\).*/& dns/' file

On Solaris sed the argument to -i is mandatory (unlike GNU sed)

sed -i '.backup' 's/^\(hosts\|ipnodes\).*/& dns/' file

That will create a file file.backup. Check man sed!

hek2mgl
  • 152,036
  • 28
  • 249
  • 266