moving text with sed

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting moving text with sed
# 1  
Old 09-21-2009
moving text with sed


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=
set physical=nxge0
add net
set address=
set physical=nxge7
add rctl
set name=zone.max-swap
add value (priv=privileged,limit=2147483648,action=deny)
add dedicated-cpu
set ncpus=2
add capped-memory
set physical=2G

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?

# 2  
Old 09-21-2009
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 [code] and [/code] 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.
# 3  
Old 09-21-2009
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

# 4  
Old 09-21-2009
I use GNU sed - try out putting the {} around the "g" back again.
# 5  
Old 09-21-2009
Originally Posted by zaxxon
In the output I get an extra blank line - not sure where it comes from though.
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/{
' file

With awk:

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

# 6  
Old 09-21-2009
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 $
# 7  
Old 09-21-2009
if you like a one line command, give this a try :
sed -e '/rctl/,/end/{H;d;}' -e '${p;g;s/^.//;}' /tmp/y

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

2. Shell Programming and Scripting

Use sed to append text to filenames if text not already present

I have some html with hrefs that contain local links to pdf filenames. These filenames should have standardised names, i.e. there should be a label prior to the ".pdf" filename suffix. There can be many of these links on a single line of text and some may already have the label. For example ... (13 Replies)
Discussion started by: adb
13 Replies

3. UNIX for Dummies Questions & Answers

deleting text with sed

Hi There! I've got a tab delimited text file (output from a software) to which I would like to delete specific strings from one of the columns. I have tried several sed codes, but they do not seem to work for me. I can manage to delete a specific word, but this is of no use, as I what I want to... (3 Replies)
Discussion started by: alfredman
3 Replies

4. Shell Programming and Scripting

deleting text records with sed (sed paragraphs)

Hi all, First off, Thank you all for the knowledge I have gleaned from this site! Deleting Records from a text file... sed paragraphs The following code works nearly perfect, however each time it is run on the log file it adds a newline at the head of the file, run it 5 times, it'll have 5... (1 Reply)
Discussion started by: Festus Hagen
1 Replies

5. Shell Programming and Scripting

moving text within file

I want to mvoe lines around in a file. Say I have 30 lines in a file and want to move the last 5 lines to the top of the can this be done? i thought of awk and sed but was not sure context. please assist thanks (5 Replies)
Discussion started by: sigh2010
5 Replies

6. Shell Programming and Scripting

Moving a part of the text in a file

*************** #some other text ***************** *************** #some other text ***************** address1= address2= address3= I have a file where i need to push all the text starting from address1 till end of file to, below . Can anyone of you... (6 Replies)
Discussion started by: srikanthgoodboy
6 Replies

7. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

8. Shell Programming and Scripting

deleting particular lines and moving a line up using perl/sed

Hi, I need convert a dump file in the following format : (please note that line numbers are provided for easy look) Original file: 1 2007-10-2482.90 No trade 0 0.00 100000.00 2 100000.00 3 0.00 4 HOLD 5 2007-10-2589.75 Bought 1114 1114 100000.00 0.00 ... (5 Replies)
Discussion started by: sabyasm
5 Replies

9. Shell Programming and Scripting

sh: Inserting tabs and moving text to 1 line

I trying to extract certain text from a csv file and then placing it into another csv file, but having problems getting the data to placed in one line with tab separated fields. Basically would like to have text sent to interfaces.csv in one line seperated by tabs. As it currently places files... (6 Replies)
Discussion started by: 00000008
6 Replies

10. Shell Programming and Scripting

Moving part of Text in a file

Hi, I have this text in a file where I need to move part of the text.... <Relation1 OriginatingObjectID="Holding_1" RelatedObjectID="Party_1" id="Relation_1"> <OriginatingObjectType tc="4">Holding</OriginatingObjectType> <RelatedObjectType tc="6">Party</RelatedObjectType>... (4 Replies)
Discussion started by: mgirinath
4 Replies
Login or Register to Ask a Question