Technology and General Blog

Steam editor, shortened as sed, is a command applied to locate and swap any text in a file making use of several techniques and syntaxes. In this publish, we will grasp the ideas of sed and discover how you can switch a variable or its benefit in a file employing sed.

The Syntax for Replacing String

For replacing a variable value using sed, we initially need to recognize how sed is effective and how we can change a very simple string in any file working with sed.

To change any string, the syntax is really straightforward and supplied down below:

$ sed -i ‘s/old-string/new-string/g’ file_identify

In this syntax, you just need to give the string you want to change at the aged string and then the new string in the inverted commas. Following that, give the file in which you want to locate and exchange the stated string.


Suppose we have a text file “file.txt” in which we have some random text like “Welcome to Linuxhint’s channel”, and in this file, we want to exchange the word channel to the internet site working with the sed command.

The command for altering the channel to the internet site would go like this:

$ sed -i ‘s/channel/website/g’ file.txt

Following jogging the command, we acquire a look again at the file:

The string was changed using the sed command. So, this is how you can come across and exchange any string in any file of the Linux operating procedure utilizing the sed command.

Now, let’s understand to change a variable price in a file.

Switch a Variable

The syntax for obtaining and replacing the worth of a variable in a file employing sed is the very same as obtaining and replacing a string in a file. The only tricky section is producing a typical expression to discover anything in a file to substitute that section. So, for altering a variable’s benefit, the syntax will go like this:

$ sed -i ‘s/var=.*/var=new_worth/’ file_name

Let us search at an instance to see the correct implementation and understand it with far more clarity.


Suppose we have a Python code file in which we have a few of variables. Individuals variables have some values assigned to them.

Now, to change any variable, we can lookup for it by its identify and present a new benefit to it using the sed command provided down below:

$ sed -i ‘s/num1 =.*/num1 =200/’

Right after executing the over sed command, we display the file material once more:

You can see that the price of the “num1” variable was changed as for each our requirement.

Making use of this simple trick, you can swap any variable or its worth in any file working with sed.


This publish gives a very simple and straightforward way to come across and substitute a variable in any file working with sed. We have figured out to change a string in a file and switch a variable’s price utilizing sed.

Leave a Reply

Your email address will not be published. Required fields are marked *