Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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


one fffds
two xxxx
three ffff
four ffde



I want to replace the line containing two xxxx in input_file.txt with the content of data_file.txt. The output file should look like this:


one fffds
three ffff
four ffde
share|improve this question

This works for me:

sed -n '
  /two xxxx/{

If the line matches the expression, the content of the file is printed and the next cycle is started. Otherwise, the second command is invoked that prints the line.

A bit more readable solution doing a similar thing in Perl:

perl -pe '/two xxxx/ and $_=`cat data_file.txt`'
share|improve this answer
the perl statement works at the command line. For the sed command, what could be the issue as it is not working for me. i am getting the following error when i type this. sed -n '/two xxxx/{r data_file.txt d};p' input_file.txt > output_file.txt sed: 0602-413 There are too many '{'. – Ramesh Sep 13 '12 at 19:56
@Ramesh: The end of line after the file name is crucial. Do you have it in your command? What version of sed do you run? – choroba Sep 14 '12 at 8:27
The syntax was not POSIX (you need ";" or NL before "}", and you can't have anything (other than NL or a new expression) after "}"). – Stéphane Chazelas Sep 17 '12 at 21:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.