In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "linux using the sed command to find and replace the file string method", the article explained the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "linux use the sed command to find and replace the file string method" it!
What is the 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.txt 1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system1) 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 each line pattern.
# sed's Unix sed-test.txt 'sed-test.txt 1 Unix linux unix 232 linux Linux 343 linuxlinux UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system2) how to find and replace patterns that appear "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.
How to search and replace all pattern instances in a row: # sed's UnixUnixUnixUnixUnixUnixUnixUnixUnix sed-test.txt 2 'sed-test.txt 1 Unix unix linux 232 linux Linux 343 linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system3)
The following sed command replaces all instances of the unix format with linux because g is a global replacement flag.
How to find and replace all matching pattern instances starting with the "Nth" in a row? # sed's UnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnixUnix linuxlinux UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system4) how to find and replace all matching pattern instances starting with "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 Unix sed-test.txt 2g 'sed-test.txt 1 Unix unix linux 232 linux Linux 343 linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system5) search and replace patterns in specific line numbers
You can replace the string in a specific line number. The following sed command replaces only the unix mode on the third line with linux.
# sed'3 Unix unix unix Unix Unix unix unix Linux linuxlinux UnixLinuxlinux 'linuxlinux UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system6) search for 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.
How to find and modify the pattern of the last line # sed'1 Unix linux unix 332 linux Linux 343 linuxlinux UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system7)
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.
How to find and replace only the correct pattern match in one line: # sed'$sed-test.txt Linux. Unix linuxunix UnixLinuxlinux 'sed-test.txt 1 Unix unix unix 232 Linux 343 linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSUnix is free and opensource operating system8)
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 '1linux Linux 3s /\ bunix\ b sed' sed-test.txt 1 linux Linux 343 linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system9) 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.
How to find and replace strings containing delimiters: # sed's UnixUnixUnixUnixUnixUnixUnixUnixUnix sed-test.txt 1 linuxlinux linux 232 linuxLinux 343 linuxlinux linuxLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system10)
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.txt 1 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.txt 1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxlinux / usr/bin/fish/ CentOS Linux OSLinux is free and opensource operating system11) 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.txt number Unix unix unix numbernumbernumber linux Linux numbernumbernumber linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system12) how to find and replace only two digits with a pattern
If you want to use patterns instead of two digits, use the following sed command.
# sed's /\ b [0-9]\ {2\}\ b number sed-test.txt 1 Unix unix unix number2 linux Linux number3 linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system13) how to use the sed command to print only the lines to be replaced
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 Linux unix unix linuxunix LinuxLinux14) 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 Unix unix unix LinuxUnix 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.txt 1 Unix unix unix 232 linux Linux 343 Fedora30 UnixLinuxlinux / bin/bash Fedora30 Linux OSLinux is free and opensource operating system15) if a given pattern matches, how to find and replace the entire line
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.txt 1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxNew LineLinux is free and opensource operating system16) 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/ sed linux Linux ArchLinux linuxunix UnixLinuxlinux linuxunix UnixLinuxlinux / bin/bash CentOS ArchLinux OSLinux is free and opensource operating system Thank you for your reading. This is "the method of finding and replacing strings in files with sed commands under linux". After the study of this article, I believe you have a deeper understanding of the problem of using sed commands to find and replace strings in files under linux. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.