19

Suppose I have this text:

BEGIN
hello
world
how
are
you
END

How to convert it to bellow text using sed command in linux:

BEGIN
fine, thanks
END
Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
Billy The Bob
  • 311
  • 1
  • 4
  • 10

1 Answers1

39
$ cat file
BEGIN
hello
world
how
are
you
END

$ sed -e '/BEGIN/,/END/c\BEGIN\nfine, thanks\nEND' file
BEGIN
fine, thanks
END

/BEGIN/,/END/ selects a range of text that starts with BEGIN and ends with END. Then c\ command is used to replace the selected range with BEGIN\nfine, thanks\nEND.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271