moving text with sed

Hi,

I have a configuration file for solaris zones that I generate as part of another script but I need to move part of the text to the end of the file prior to execution and I'm having problems getting the syntax correct on the sed command.

The file looks like this:

create -b
set zonepath=/export/zones/fred
set autoboot=false
set ip-type=shared
add net
set address=1.1.1.1
set physical=nxge0
end
add net
set address=1.1.1.2
set physical=nxge7
end
add rctl
set name=zone.max-swap
add value (priv=privileged,limit=2147483648,action=deny)
end
add dedicated-cpu
set ncpus=2
end
add capped-memory
set physical=2G
end

The lines in bold need to be moved to the end of the file so I tried using the following command in sed but can't get it working.

sed '/rctl/,/end/{h;d};${g;p;}' /tmp/filename

It pattern matches the correct lines and deletes them but doesn't print them at the end of the file.

What have I done wrong?

Thanks

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

******************************************************

You should use the uppercase version of hold so that the line you took into hold buffer will not be overwritten by the next you take there.
Also the p is not needed when get the buffer.

sed '/rctl/,/end/{H;d};$g' infile

In the output I get an extra blank line - not sure where it comes from though.

Thanks for the reply. I tried it on my server which is running Solaris 10 and get a command garbled error. Any ideas?

sed: command garbled: /rctl/,/end/{H;d};$g

I use GNU sed - try out putting the {} around the "g" back again.

The Hold command puts a newline followed by the contents of the pattern space after the contents of the hold space.

Something like this should work:

sed '/rctl/,/end/{
H
d
}
${
p
g
s/^.//
}
' file

With awk:

awk '/add rctl/,/end/{s=s?s RS $0:$0;next}{print} END{print s}' file

Thanks got it working now

sed '/rctl/,/end/{H;d;}
${p;g;s/^.//;}' /tmp/y

Ideally wanted it on one line but if I run it as

sed '/rctl/,/end/{H;d;};${p;g;s/^.//;}' /tmp/y

it doesn't add the lines to the end of the file and seems to ignore everything past the $

if you like a one line command, give this a try :

sed -e '/rctl/,/end/{H;d;}' -e '${p;g;s/^.//;}' /tmp/y

Thats it, great.

Thanks for all your help guys :slight_smile: