I've noticed that, if I add \n to a pattern for substituting using sed, it does not match. Example:$ cat > alpha.txtThis isa testPlease do notbe alarmed$ sed -i'.original' 's/a test\nPlease do not/not a test\nBe/' alpha.txt$ diff alpha.txt{,.original}$ # No differences printed outHow can I get this to work?
How can I use sed to replace a multi-line string?
sed;regular expression;utilities
In the simplest calling of sed, it has one line of text in the pattern space, ie. 1 line of \n delimited text from the input. The single line in the pattern space has no \n... That's why your regex is not finding anything.   You can read multiple lines into the pattern-space and manipulate things surprisingly well, but with a more than normal effort..  Sed has a set of commands which allow this type of thing...  Here is a link to a Command Summary for sed. It is the best one I've found, and got me rolling.  However forget the one-liner idea once you start using sed's micro-commands. It is useful to lay it out like a structured program until you get the feel of it... It is surprisingly simple, and equally unusual. You could think of it as the assembler language of text editing.   Summary: Use sed for simple things, and maybe a bit more, but in general, when it gets beyond working with a single line, most people prefer something else...I'll let someone else suggest something else.. I'm really not sure what the best choice would be (I'd use sed, but that's because I don't know perl well enough.)     sed '/^a test$/{       $!{ N        # append the next line when not on the last line         s/^a test\nPlease do not$/not a test\nBe/                    # now test for a successful substitution, otherwise                    #+  unpaired a test lines would be mis-handled         t sub-yes  # branch_on_substitute (goto label :sub-yes)         :sub-not   # a label (not essential; here to self document)                    # if no substituion, print only the first line         P          # pattern_first_line_print         D          # pattern_ltrunc(line+nl)_top/cycle         :sub-yes   # a label (the goto target of the 't' branch)                    # fall through to final auto-pattern_print (2 lines)       }         }' alpha.txt  Here it is the same script, condensed into what is obviously harder to read and work with, but some would dubiously call a one-liner sed '/^a test$/{$!{N;s/^a test\nPlease do not$/not a test\nBe/;ty;P;D;:y}}' alpha.txtHere is my command cheat-sheet :  # label=  # line_numbera  # append_text_to_stdout_after_flushb  # branch_unconditional             c  # range_change                     d  # pattern_delete_top/cycle          D  # pattern_ltrunc(line+nl)_top/cycle g  # pattern=hold                      G  # pattern+=nl+hold                  h  # hold=pattern                      H  # hold+=nl+pattern                  i  # insert_text_to_stdout_now         l  # pattern_list                       n  # pattern_flush=nextline_continue   N  # pattern+=nl+nextline              p  # pattern_print                     P  # pattern_first_line_print          q  # flush_quit                        r  # append_file_to_stdout_after_flush s  # substitute                                          t  # branch_on_substitute              w  # append_pattern_to_file_now         x  # swap_pattern_and_hold             y  # transform_chars                   