I have a big text file (URL.txt) and I wish to perform the following using a single sed command:
Find and replace text 'google' with 'facebook' between line numbers 19 and 33.
Display the output on the terminal without altering the original file.
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. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.
By default, when sed reads a line in the pattern space, it discards the terminating newline (\n) character. Nevertheless, we can handle multi-line strings by doing nested reads for every newline.
By using N and D commands, sed can apply regular expressions on multiple lines (that is, multiple lines are stored in the pattern space, and the regular expression works on it): $ cat two-cities-dup2.
You can use sed addresses:
sed '19,33s/google/facebook/g' file
This will run the substitution on lines between and including 19 and 33.
The form of a sed command is as follows:
[address[,address]]function[arguments]
Where 19,33
is the addreses,s
ubstitute is function
and g
lobal is the argument
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