how to replace all lines between two points and su

2020-07-03 04:27发布

问题:

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

回答1:

$ 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.



回答2:

sed -e 's/BEGIN hello world how are you END/BEGIN fine, thanks END/g'


标签: linux sed