Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find and replace strings in a file with the sed command

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Introduction to linux

Linux, whose full name is GNU/Linux, is a set of Unix-like operating system that is free to use and spread freely. It is a multi-user, multi-task, multi-thread and multi-CPU operating system based on POSIX and Unix. With the development of the Internet, Linux has received support from software enthusiasts, organizations and companies all over the world. In addition to maintaining a strong momentum of development in servers, it has made great progress in personal computers and embedded systems. Users can not only intuitively obtain the implementation mechanism of the operating system, but also modify and improve Linux according to their own needs to maximize meet the needs of users.

Introduction to sed command

The sed command stands for Stream Editor (stream editor), which is 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 lines.

Search and replace two different modes at the same time.

The fifteen examples listed in this article can help you master sed commands.

If you want to delete lines from a file using the sed command, go to the following article.

Note: since this is a demo article, we use the sed command without the-I option, which deletes lines and prints the contents of the file in the Linux terminal.

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

The syntax of the common sed replacement string.

Sed-I's Input_File Searchy string

First, we need to understand the sed syntax to do this. Please refer to the relevant details.

Sed: this is a Linux command.

-I: this is an option of the sed command. What does it do? By default, sed prints the results to standard output. When you use sed to add this option, it will modify the file in place. When you add a suffix (for example,-i.bak), a backup of the original file is created.

S: the letter s is a replacement command.

Search_String: searches for a given string or regular expression.

Replacement_String: the string to replace.

G: global replacement flag. By default, the sed command replaces the pattern that occurs for the first time on each line, and it does not replace other matches in the line. However, when the replacement flag is provided, all matches are replaced.

/: delimiter.

Input_File: the name of the file to perform the operation.

Let's take a look at some common examples of searching and converting text with the sed command in a file.

We have created the following files for the demonstration.

# cat sed-test.txt1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system

1) how to find and replace the "first" pattern match in a row

The following sed command replaces unix in the file with linux. This only changes the first instance of each line pattern.

# sed's UnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUni

2) how to find and replace the pattern of "the Nth time" in each line

Use / 1, / 2 in the line. / n and other flags to replace the corresponding matching.

The following sed command replaces the second instance of the unix pattern with linux on one line.

# sed's Unix linux Linux linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system 2 'sed-test.txt1 Unix unix linux

3) how to search and replace all schema 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 linux Linux Unix and Linux Unix G' sed-test.txt1 Unix linuxlinux 232 linuxlinux UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system

4) how to find and replace all matching pattern instances starting from the "Nth" in a row

The following sed command replaces the matching instance starting with the "Nth" of the pattern on one line.

# sed's linux Linux Unix linux Linux linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system 2g 'Unix

5) search and replace patterns at specific line numbers

# sed'3 linux Linux linux Linux linuxlinux UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system

6) search and replace patterns between specific range line numbers

You can specify a range of line numbers to replace the string.

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

# sed'1 linux Linux 3 linux Linux linuxlinux UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system

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

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

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

# sed'$sUnix sed-test.txt1 Unix unix unix 'linux Linux linux Linux 343 linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSUnix is free and opensource operating system

8) how to find and replace only the correct pattern match in one line

You may have noticed that the substring linuxunix is replaced with linuxlinux in the sixth example. If you just want to change the correct match, use this boundary character\ b at both ends of the search string.

# sed '1linuxunix UnixLinuxlinux 3s /\ bunix\ bUnix sed-test.txt1 Unix linuxunix' linux Linux 343 linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system

9) how to search for and replace patterns with case insensitivity

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

# sed's sed-test.txt1 linuxlinux linux Unix and Linux sed-test.txt1 linuxlinux linux gi'Unix 232 linuxlinux linuxLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system

10) how to find and replace strings containing delimiters

When you search for and replace a delimited string, we need to use a backslash\ to cancel the escape.

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

# sed's /\ / bin\ / bash/\ / usr\ / bin\ / fish/g' sed-test.txt1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxlinux / usr/bin/fish CentOS Linux OSLinux is free and opensource operating system

The above sed command works as expected, but it looks bad. To simplify, most people use vertical bars | as locators for regular expressions. So, I suggest you use it.

# sed's | / bin/bash | / usr/bin/fish/ | g 'sed-test.txt1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxlinux / usr/bin/fish/ CentOS Linux OSLinux is free and opensource operating system

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 number with [0-9].

# sed's / [0-9] / number/g' sed-test.txtnumber Unix unix unix numbernumbernumber linux Linux numbernumbernumber linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system

12) how to find and replace only two numbers with a pattern

If you want to use patterns instead of two digits, use the following sed command.

# sed's /\ b [0-9]\ {2\}\ bUniple numberg'sed-test.txt1 Unix unix unix number2 linux Linux number3 linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system

13) how to print only the replaced lines with the sed command

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

P-it outputs the replacement line twice on the terminal.

-n-it suppresses duplicate lines generated by the p flag.

# sed- n's Unix and Linux linuxunix LinuxLinux p 'sed-test.txt1 Linux unix unix 233

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

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

The following sed command searches for linuxunix and CentOS patterns and replaces them with LINUXUNIX and RHEL8 at once.

# sed- e's linux Linux LinuxUnix LINUXUNIX UnixLinuxlinux LINUXUNIX UnixLinuxlinux / bin/bash RHEL8 Linux OSLinux is free and opensource operating system

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

The following sed command searches for linuxunix and CentOS patterns and replaces them with Fedora30.

# sed- e's /\ (linuxunix\ | CentOS\) / Fedora30/g' sed-test.txt1 Unix unix unix 232 linux Linux 343 Fedora30 UnixLinuxlinux / bin/bash Fedora30 Linux OSLinux is free and opensource operating system

15) if a given pattern matches, how to find and replace the entire row

If the patterns match, you can use the sed command 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.txt1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxNew LineLinux is free and opensource operating system

16) how to search and replace matching pattern lines

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

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

# sed'/ OS/ linux Linux linuxunix UnixLinuxlinux linuxunix UnixLinuxlinux / bin/bash CentOS ArchLinux OSLinux is free and opensource operating system

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report