×
sed replace string in file using regex recursively

Sed also known as “stream editor” is used to filters and transforms text in Linux-based operating systems.

It is a very powerful utility and mainly used to find & replace the string but it can also able to perform some other tasks including, insertion, deletion, search, etc. sed also supports that makes it a more powerful test manipulation tool.

In this tutorial, we will show you how to replace a string in file through various examples.

Basic Syntax

The basic syntax of the sed command is shown below:

sed OPTIONS [SCRIPT] [INPUTFILE]

Or

sed -i 's/Find/Replace/g' Filename

Where :

  • sed : Command we’ll be using.
  • -i : This option will create a backup of the original file.
  • s : substitute command.
  • Find : It is used to search a given string.
  • Replace : It is used to replace with a given string.
  • g : This option will replace all occurrences in the line.
  • Filename : Name of the file you want to perform the action.

You can see all options available with sed command using the following command:

sed --help

You should see the following screen:

Create a Sample file for Testing

For testing purposes, you will need to create a sample file in your system. You can create it with the following command:

nano file.txt

Add the following content into the file:

1 Ubuntu ubuntu ubuntu 12
2 windows Windows 56
3 windowsubuntu UbuntuWindows
4 windows RedHat Windows OS
5 Windows is commercial operating system

Save and close the file when you are finished.

Find and Replace the Specific Occurrence of the Pattern on a Line

By default, the sed command replaces only the first occurrence of a pattern in each line. You can also replace the first, second and Nth occurrence of a pattern in each line.

For example, find and replace the first occurrence the word ubuntu with windows in the file.txt as shown below:

sed 's/ubuntu/windows/' file.txt

You should see that sed command only changes the first instance of the pattern on each line.

1 Ubuntu windows ubuntu 12
2 windows Windows 56
3 windowswindows UbuntuWindows
4 windows RedHat Windows OS
5 Windows is commercial operating system

To find and replace the second occurrence the word ubuntu with windows in the file.txt as shown below:

sed 's/ubuntu/windows/2' file.txt

You should see the following output:

1 Ubuntu ubuntu windows 12
2 windows Windows 56
3 windowsubuntu UbuntuWindows
4 windows RedHat Windows OS
5 Windows is commercial operating system

To find and replace All the occurrence of the word ubuntu with windows in the file.txt as shown below:

sed 's/ubuntu/windows/g' file.txt

You should see the following output:

1 Ubuntu windows windows 12
2 windows Windows 56
3 windowswindows UbuntuWindows
4 windows RedHat Windows OS
5 Windows is commercial operating system

To replace the word Windows with ubuntu with case insensitive use the flag I as shown below:

sed 's/windows/ubuntu/I' file.txt

You should see the following output:

1 Ubuntu ubuntu ubuntu 12
2 ubuntu Windows 56
3 ubuntuubuntu UbuntuWindows
4 ubuntu RedHat Windows OS
5 ubuntu is commercial operating system

Find and Replace the Pattern on a Specific Line Number

You can also replace the specific string on a specific line number.

For example, replace the word windows with ubuntu on the 3rd line as shown below:

sed '3 s/windows/ubuntu/' file.txt

You should see the following output:

1 Ubuntu ubuntu ubuntu 12
2 windows Windows 56
3 ubuntuubuntu UbuntuWindows
4 windows RedHat Windows OS
5 Windows is commercial operating system

To replace the word windows with ubuntu on 2nd and 4th line as shown below:

sed '2,4 s/windows/ubuntu/' file.txt

You should see the following output:

1 Ubuntu ubuntu ubuntu 12
2 ubuntu Windows 56
3 ubuntuubuntu UbuntuWindows
4 ubuntu RedHat Windows OS
5 Windows is commercial operating system

Find and Replaces Digits with a Given Pattern

You can also find the digits [0-9] and replaces it with any string.

For example, find the single digit numbers and replace it with string Linux as shown below:

sed 's/[0-9]/Linux/g' file.txt

You should see the following output:

Linux Ubuntu ubuntu ubuntu LinuxLinux
Linux windows Windows LinuxLinux
Linux windowsubuntu UbuntuWindows
Linux windows RedHat Windows OS
Linux Windows is commercial operating system

You can also find the two digit numbers and replace it with string Linux as shown below:

sed 's/\b[0-9]\{2\}\b/Linux/g' file.txt

You should see the following output:

1 Ubuntu ubuntu ubuntu Linux
2 windows Windows Linux
3 windowsubuntu UbuntuWindows
4 windows RedHat Windows OS
5 Windows is commercial operating system

Find and Replaces Different Patterns at Once

The sed command also allows you to find and replace patterns simultaneously.

For example, find the word windows and RedHat, and replacing them with linux and centos as shown below:

sed -e 's/windows/linux/g' -e 's/RedHat/centos/g' file.txt

You should see the following output:

1 Ubuntu ubuntu ubuntu 12
2 linux Windows 56
3 linuxubuntu UbuntuWindows
4 linux centos Windows OS
5 Windows is commercial operating system

You can also find two different words (windows, ubuntu) and replace them with one common word (fedora) as shown below:

sed -e 's/\(windows\|ubuntu\)/fedora/g' file.txt

You should see the following output:

1 Ubuntu fedora fedora 12
2 fedora Windows 56
3 fedorafedora UbuntuWindows
4 fedora RedHat Windows OS
5 Windows is commercial operating system

Find and Delete the Lines that Matches Pattern

The sed command also allows you to delete the specific line and pattern matching line.

To delete the 2nd line in the file.txt, run the following command:

sed '2d' file.txt

You should see the following output:

1 Ubuntu ubuntu ubuntu 12
3 windowsubuntu UbuntuWindows
4 windows RedHat Windows OS
5 Windows is commercial operating system

To delete the line from range 2 to 4, run the following command:

sed '2,4d' file.txt

You should see the following output:

1 Ubuntu ubuntu ubuntu 12
5 Windows is commercial operating system

To delete the line that matches the pattern ubuntu, run the following command:

sed '/ubuntu/d' file.txt

You should see the following output:

2 windows Windows 56
4 windows RedHat Windows OS
5 Windows is commercial operating system

You can also modify a specific line using the c flag.

For example, modify 4th line with with “This is a modified line” as shown below:

sed '4c\This is a modified line.' file.txt

You should see the following output:

1 Ubuntu ubuntu ubuntu 12
2 windows Windows 56
3 windowsubuntu UbuntuWindows
This is a modified line.
5 Windows is commercial operating system

Conclusion

In this article, we learned how to find and replace string with sed command through different examples. I hope this guide will help you to perform a daily task. Feel free to ask me if you have any questions.