When you are working with text files, you will most likely need to find and replace strings in the file. The sed command is used to replace text in a file. In Linux this can be done using the sed command and the awk command.

In this tutorial, we’ll show you how to do this using the sed command, and then discuss the awK commands.

What is the sed command

The sed command represents a Stream Editor used to perform basic text operations on Linux. It can perform various functions, such as searching, finding, modifying, inserting, or deleting files.

In addition, it can perform complex regular expression matching.

It can be used for the following purposes:

  • Find and replace content that matches the given format.
  • Finds and replaces content that matches the given format on the specified line.
  • Find and replace content that matches the given format on all rows.
  • Search for and replace two different patterns simultaneously.

The 15 examples listed in this article will help you master sed.

To use the sed command to delete lines from a file, go to the following article.

Note: Since this is a demo article, we use sed without the -i option, which deletes lines and prints the file contents on the Linux terminal.

However, in the real world if you want to delete lines from the source file, use sed with the -i option.

The common syntax for replacing strings with sed.

sed -i 's/Search_String/Replacement_String/g' Input_File
Copy the code

First we need to understand sed syntax to do this. Please see related details.

  • sedThis is a Linux command.
  • -i: this issedAn option of the command. What does it do? By default,sedPrints the result to standard output. When you usesedWhen this option is added, it modifies the file in the appropriate place. When you add a suffix (e.g.,-i.bak), a backup of the original file is created.
  • s: the lettersIs a replacement command.
  • Search_String: Searches for a given string or regular expression.
  • Replacement_String: Replacement string.
  • g: Global replacement flag. By default,sedThe command replaces the first occurrence of a pattern in each line. It does not replace other matches in the line. However, when the replacement flag is provided, all matches will be replaced.
  • /: delimiter.
  • Input_File: Specifies the name of the file to perform the operation.

Let’s take a look at some common examples of files that use sed to search and convert text.

We have created the following files for the demo.

# cat sed-test.txt

1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
Copy the code

1) How to find and replace a “first” pattern match in a row

The following sed command replaces Unix in the file with Linux. This only changes the first instance of the pattern in each row.

# sed 's/unix/linux/' sed-test.txt

1 Unix linux unix 23
2 linux Linux 34
3 linuxlinux UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
Copy the code

2) How to find and replace the “NTH” pattern in each line

Use /1, /2 in lines… /n flags instead of corresponding matches.

The following sed replaces the second instance of Unix mode with Linux on one line.

# sed 's/unix/linux/2' sed-test.txt1 Unix unix linux 23 2 linux Linux 34 3 linuxunix UnixLinux linux /bin/bash CentOS Linux OS Linux is free and opensource  operating systemCopy the code

3) How do I search and replace all pattern instances in a row

The following sed command replaces all instances of the Unix format with Linux because g is a global replacement flag.

# sed 's/unix/linux/g' sed-test.txt

1 Unix linux linux 23
2 linux Linux 34
3 linuxlinux UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
Copy the code

4) How do I find and replace all matching pattern instances starting with the “NTH” in a row

The following sed replaces the matching instance starting with the “NTH” of the pattern on a single line.

# sed 's/unix/linux/2g' sed-test.txt1 Unix unix linux 23 2 linux Linux 34 3 linuxunix UnixLinux linux /bin/bash CentOS Linux OS Linux is free and opensource  operating systemCopy the code

5) Search and replace patterns at specific line numbers

You can replace strings in specific line numbers. The following sed replaces only the Unix mode on the third line with Linux.

# sed '3 s/unix/linux/' sed-test.txt1 Unix unix unix 23 2 linux Linux 34 3 linuxlinux UnixLinux linux /bin/bash CentOS Linux OS Linux is free and opensource  operating systemCopy the code

6) Search and replace patterns between specific range line numbers

You can specify a range of line numbers to replace strings.

The following sed command replaces Unix mode with Linux between lines 1 and 3.

# sed '1, 3s/Unix/Linux /' sed-test.txt

1 Unix linux unix 23
2 linux Linux 34
3 linuxlinux UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
Copy the code

7) How to find and modify the pattern of the last line

The following sed command allows you to replace the matching string only on the last line.

The following sed replaces the Linux mode with Unix only on the last line.

# sed '$ s/Linux/Unix/' sed-test.txt

1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Unix is free and opensource operating system
Copy the code

8) How do I find and replace only correct pattern matches in a line

You may have noticed that the substring LinuxUNIX was replaced with LinuxLinux in the sixth example. If you only want to change the correct match, use the boundary character \b at both ends of the search string.

# sed '1, 3s /\bunix\b/ Linux /' sed-test.txt1 Unix linux unix 23 2 linux Linux 34 3 linuxunix UnixLinux linux /bin/bash CentOS Linux OS Linux is free and opensource  operating systemCopy the code

9) How to search and replace patterns case insensitive

As you all know, Linux is case sensitive. To match a case-insensitive pattern, the I flag is used.

# sed 's/unix/linux/gI' sed-test.txt

1 linux linux linux 23
2 linux Linux 34
3 linuxlinux linuxLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
Copy the code

10) How do I find and replace strings containing delimiters

When you search and replace delimited strings, we need to use a backslash \ to unescape.

In this example, we will replace /bin/bash with /usr/bin/fish.

# sed 's/\/bin\/bash/\/usr\/bin\/fish/g' sed-test.txt

1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /usr/bin/fish CentOS Linux OS
Linux is free and opensource operating system
Copy the code

The above sed command works as expected, but it looks terrible. In order to simplify, most people will use a vertical bar | locator as a regular expression. So, I suggest you use it.

# sed 's|/bin/bash|/usr/bin/fish/|g' sed-test.txt

1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /usr/bin/fish/ CentOS Linux OS
Linux is free and opensource operating system
Copy the code

11) How to find and replace numbers in a given pattern

Similarly, numbers can be replaced by patterns. The following sed command replaces all numbers with [0-9].

# sed 's/[0-9]/number/g' sed-test.txt

number Unix unix unix numbernumber
number linux Linux numbernumber
number linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
Copy the code

12) How do I use patterns to find and replace only two numbers

If you want to use patterns instead of two-digit numbers, use the sed command below.

# sed 's/\b[0-9]\{2\}\b/number/g' sed-test.txt

1 Unix unix unix number
2 linux Linux number
3 linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
Copy the code

13) How to print only replaced lines with sed

If you want to display only changed lines, use the sed command below.

  • p– It prints the replacement line twice on the terminal.
  • -n– It inhibits bypThe duplicate lines generated by the flag.
# sed -n 's/Unix/Linux/p' sed-test.txt

1 Linux unix unix 23
3 linuxunix LinuxLinux
Copy the code

14) How to run multiple sed commands at the same time

The following sed command detects and displaces two different modes simultaneously.

The following sed command searches for LinuxUNIX and CentOS modes and replaces them once with LinuxUNIX and RHEL8.

# sed -e 's/linuxunix/LINUXUNIX/g' -e 's/CentOS/RHEL8/g' sed-test.txt

1 Unix unix unix 23
2 linux Linux 34
3 LINUXUNIX UnixLinux
linux /bin/bash RHEL8 Linux OS
Linux is free and opensource operating system
Copy the code

The following sed command searches and replaces two different patterns with a single string at once.

The following sed command searches for linuxUNIX and CentOS modes and replaces them with Fedora30.

# sed -e 's/\(linuxunix\|CentOS\)/Fedora30/g' sed-test.txt1 Unix unix unix 23 2 linux Linux 34 3 Fedora30 UnixLinux linux /bin/bash Fedora30 Linux OS Linux is free and opensource  operating systemCopy the code

15) How to find and replace an entire row if a given pattern matches

If the pattern matches, you can use sed to replace the entire line with a new line. This can be done by using the C flag.

# sed '/OS/ c\
New Line
' sed-test.txt 1 Unix unix unix 23 2 linux Linux 34 3 linuxunix UnixLinux New Line Linux is free and opensource operating systemCopy the code

16) How to search and replace matching pattern rows

In the sed command you can specify the appropriate mode for the line. In case the pattern matches, the sed command searches for the string to be replaced.

The following sed command first looks for lines with an OS pattern and then replaces the word Linux with ArchLinux.

# sed '/OS/ s/Linux/ArchLinux/' sed-test.txt

1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /bin/bash CentOS ArchLinux OS
Linux is free and opensource operating system
Copy the code

Via: www.2daygeek.com/linux-sed-t…

Magesh Maruthamuthu (Lujun9972

This article is originally compiled by LCTT and released in Linux China