While working any script or any configuration file in development env, We need to replace a string/worlds, We can achieve this by using sed command.
The sed represents stands for stream editor. It peruses the given record, adjusting the contribution as indicated by a rundown of sed orders. Naturally, the info is kept in touch with the screen, yet you can power to refresh document.
In this post, We will use sed command to find and replace text in files on linux.
Step 1: Find and replace text Globally
In my case, I have a file called file.txt, Which has text oldtext and we will replace it to newtext by using the sed command.
sed -i 's/oldtext/newtext/g' file.txt
Here the “s” indicates the replacement activity. The “/” are delimiters. The “unix” is the pursuit design and the “linux” is the substitution string.
Of course, the sed order replaces the main event of the example in each line and it will not supplant the second, third… event in the line.
Step 2: Find and replace text on Specific lines
We are going replace the text on specific line number by following sed command.
sed -i '10s/oldtext/newtext/' file.txt
Orders in sed acknowledge a location range. For this situation, to restrict the substitution to simply the principal line, the reach ought to be the single location 1 for example
sed '1 s/oldUser/newUsers/' file.txt
We have successfully replace the text globally and on specific line number using sed command on ubuntu machine, Still you have any issue leave a comment.