0
red    
blue    
water    
gray    
white

I want to use

sed '/blue/,/gray/!b;//!d;/blue/r file2' file1

As mentioned in this Replace text between two lines with contents of a file stored in a variable in sed thread

But instead of file2 i want to enter new string directly in command. Can any one show me how to do that.

2
  • You mean you want to apply the sed operation on a output of a command? Can you explain more? Commented Dec 8, 2017 at 8:00
  • @Inian No. I don't want to get new content from file2 but instead directly put string(new text) in this sed '/blue/,/gray/!b;//!d;/blue/r file2' file1 I dont know the syntax for that Commented Dec 8, 2017 at 8:04

1 Answer 1

1

You can use this sed:

sed '/blue/,/gray/!b;//!d;/blue/a SOMEDATA' file

From man sed:

a \
  text

Append text, which has each embedded newline preceded by a backslash.

Another example:

$ sed '/blue/,/gray/!b;//!d;/blue/a \
> LINE 1 \
> LINE 2 \
> LINE 3' file

red    
blue    
LINE 1 
LINE 2 
LINE 3
gray    
white
Sign up to request clarification or add additional context in comments.

1 Comment

yes it worked. so, Is it /a for append and /r for read from file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.