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.

I want to insert the contents of file1 into file2 after a matching PATTERN. I want to do it only after the first occurrence of the PATTERN.

I would like to know the modification I need to make to the following command for my needs.

sed -i "/PATTERN/r file1" file2

share|improve this question
add comment

1 Answer 1

sed '/PATTERN/{r file1
:a;n;ba}' file2

:a;n;ba is just a cycle that printouts the whole file content after the PATTERN till the end. and note, that this two lines are just one command, but newline is needed to delimit the next sed command after r.

additional info from info sed :

`n'
     If auto-print is not disabled, print the pattern space, then,
     regardless, replace the pattern space with the next line of input.
     If there is no more input then `sed' exits without processing any
     more commands.

`: LABEL'
     [No addresses allowed.]

     Specify the location of LABEL for branch commands.  In all other
     respects, a no-op.

`b LABEL'
     Unconditionally branch to LABEL.  The LABEL may be omitted, in
     which case the next cycle is started.
share|improve this answer
    
Could you explain what does the label (:a) actually do there and what the value of n is? –  Gopal Sep 25 '12 at 20:58
    
yes. now some explanation is in the answer. –  rush Sep 25 '12 at 21:05
add comment

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.