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
Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace.
'g' option is used in `sed` command to replace all occurrences of matching pattern. Create a text file named python.
The s command (as in substitute) is probably the most important in sed and has a lot of different options. The syntax of the s command is ' s/ regexp / replacement / flags '.
$ 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With