2

input_file.txt:

one fffds
two xxxx
three ffff
four ffde

data_file.txt:

six
seven

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:

output_file.txt:

one fffds
six
seven
three ffff
four ffde

1 Answer 1

3

This works for me:

sed -n '
  /two xxxx/{
    rdata_file.txt
    d
  }
  p'

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`'
3
  • 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, 2012 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, 2012 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 "}"). Sep 17, 2012 at 21:25

You must log in to answer this question.

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