Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question

1 Answer 1

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`'
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

 
discard

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.