Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to use the r (read command to append the contents of data2 to data. I want to append the contents after the line containing 'two'. I want to delete the line containing 'two'.

But I've hit a brick wall. It feels like a bug, but perhaps it's just my own stupidity.

Ok this command works.

% sed -r '/two/ r /tmp/data2' <  /tmp/data
one
two
five
six
seven
three
four

Sed seems a little finiky about spaces.. Not obvious why, but I'll live with it.

% sed -r '/two/ r /tmp/data2  ' <  /tmp/data
one
two
three
four

My goal is to run a (d) delete after the read command to remove the original line. This is as far as I get.

% sed -r '/two/{ r /tmp/data2}' <  /tmp/data
sed: -e expression #1, char 0: unmatched `{'

I'm pretty sure that '{' is being matched, it's not the longest command in the world.

What I actually want to run...

% sed -r '/two/{ r /tmp/data2; d }' <  /tmp/data

The system is Ubuntu 12. GNU SED.

share|improve this question
    
Are the curly braces ({}) intended to be literal strings? – EBGreen Aug 1 '12 at 12:15
up vote 5 down vote accepted

r /file/path must not have anything after it on its line.

You may find this site helpful as a Command Summary for sed

echo 'one
two
five
six
seven
three
four' >inputfile

echo 'contents of readfile' >readfile

sed  '/two/{r readfile
         d}' inputfile

Note: You can utilize the shell to parameterize sed, by using "double quotes". They enable shell variable expansion. r takes all spaces literally... so don't quote the filename, and don't have any trailing whitespace (whitespace between r and /file/path is ok).

rfile='/tmp/readfile with   multiple spaces in name'
sed  "/two/{r $rfile
       d}" inputfile

output

one
contents of readfile
five
six
seven
three
four

On a side note: There is really no value using < for your input file. Use sed's input file parameter (it keeps things simpler).

share|improve this answer
    
Thanks, shame it has to be on it's own line, I was really hoping to get it down to a single line. I was trying stuff like {r $rfile;d} but to no avail. – binarytemple.co.uk Aug 1 '12 at 14:08
    
I know what you mean about not being able to keep it on the same line, but you can still use it at the command line. just press Enter after r /file/path to continue the command on the next line... It continues because of the quotes... (most one-liners aren't really one command, but this sed is.. You can put a sizeable C program onto one long line, but that doesn't make it a "one-liner" (to me). – Peter.O Aug 1 '12 at 14:18
    
;-) I know, it's an obsession... – binarytemple.co.uk Aug 1 '12 at 14:28
    
My original plan was to embed it inside something like this... for i in $( print -C1 ./**/pom.xml(:a:h) );; do cd $i; <SED SUBSTITUTION> ; done – binarytemple.co.uk Aug 1 '12 at 14:31
2  
Here is the one-liner (for those who must :)... sed -f <(sed 's/\\n/\n/'<<<"/two/{r $rfile\nd}") inputfile .. If you like it, give me an upvote :) – Peter.O Aug 1 '12 at 14:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .