How to Replace a Variable in a File Using SED

How to Replace a Variable in a File Using SED

Want to know the tricks of replacing a variable in a file using the SED command?

This article will give you an overview of replacing a variable value in a file using SED. Before replacing a variable in a file using SED, you need to understand what SED is, the syntax of SED, and how SED works.

I’ll also show how to perform delete operations using SED. This will come after the variable value replacement part. If you’re looking for that, you can directly jump onto that, and skip the rest.

So, let’s begin the guide.

 

What is SED?

So, what is  SED?

SED command in Linux stands for Stream Editor. It performs searching, insertion, find and replace, deletion. In the Linux world, SED is mainly popular for its find and replace functionality.

With the help of SED, coders can edit files without even opening them.

In a nutshell,

  • SED is a text stream editor. It can be used to do find and replace, insertion, and delete operations in Linux.

  • You can modify the files as per your requirements without having to open them.

  • SED is also capable of performing complex pattern matching.

 

Syntax of SED

Here we’ll see the syntax of SED used in a simple string replacement. This will help understand the command better.

So the syntax is:

sed -i 's/old-string/new-string/g' file_name

 

How SED Works

In the syntax, you only need to provide a suitable “new string” name that you want to be placed with the “old string”. Of course, the old string name needs to be entered as well.

Then, provide the file name in the place of “file_name” from where the old string will be found and replaced.

Here’s a quick example to clear the concept.

Suppose, we have a random text “Welcome to Linux Channel” in a text file called “file.txt”.

Now, we want to replace “Channel” with “Family”. How can we do that?

First, write the below-given command in the terminal to create the file.

cat file.txt

Press enter, then type:

Welcome to Linux Channel

Let’s alter “Channel” with “Family” now. So, go to the next line, and type:

sed -i 's/Channel/Family/g' file.txt

After running the command, to view the file again, type:

cat file.txt

You’ll see “Channel” has been replaced with “Family”. In this way, you can replace a string using the SED command. Let’s learn how to replace a variable using SED, now.

 

How to Replace a Variable in a File Using SED

The syntax to find and replace a variable value using SED is pretty much the same as replacing a string.

sed -i 's/var=.*/var=new_value/' file_name

We’ll look at an example, now, to understand the implementation of the command.

Let’s create a Python code file using:

cat code.py

After writing the above command in the terminal, write:

# This program adds two numbers

num1 = 1.5

num2 = 6.3


# Add two numbers

sum = num1 + num2


# Display the num

print(‘The sum of {0} and {1} is {2}’.format(num1, num2, sum))

Now, type the following line:

sed -i 's/num1 =.*/num1 =200/' code.py

This SED command will find the variable “num1” and then will enter the new value as “200”.

After executing the command, display the file content by typing:

cat code.py

And you’ll notice the “num1” variable’s value has been replaced with the new input given.

 

Deleting Lines Using SED Command

There are different SED syntaxes for deleting lines in a file. If you ever want to delete lines, use the SED commands. This also can be done without having to open the file.

  • The syntax to delete a line, say n

sed 'nd' filename.txt

Example:

sed ‘44d’ filename.txt
  • The syntax for deleting the last line

sed '$d' filename.txt
  • For deleting a line from x to y range

sed 'x,yd' filename.txt

Example:

sed '3,6d' filename.txt
  • The syntax to delete from the nth line to the last line

sed 'nth,$d' filename.txt

Example:

sed '12,$d' filename.txt

 

The Conclusion

And this is the end of this article. Hope this article has presented all the answers you’re looking for. In this article, you have learned what SED is, how SED works, how to replace a string in a file, how to replace a variable in a file, and how to perform the deletion.

To learn more about Linux, follow the Linux Journal blog.

Suparna is a freelance writer who writes about Linux including tips, tricks, and how-tos.

Load Disqus comments