In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly shows you the "linux how to replace the string in the file", the content is easy to understand, clear, hope to help you solve the doubt, the following let the editor lead you to study and learn "linux how to replace the string in the file" this article.
In linux, you can use the sed command to find and replace strings in a file, which uses stream editing mode to match and process data in a text file based on regular expressions; the replacement syntax is "sed's / string to search or regular expression / replacement value / file name to perform the operation".
The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
When you are using a text file, you probably need to find and replace strings in the file. In Linux, this can be done by using the sed command, which is mainly used to replace the text in a file.
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.
The sed command is in stream editing mode and can perform complex regular expression matching.
The sed command matches and processes the data in the text file based on regular expressions.
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 sed command replaces the string in the file
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 each 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 each 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 linux Linux linuxunix UnixLinuxlinux 2g 'linuxunix UnixLinuxlinux / bin/bash CentOS Linux OSLinux is free and opensource operating system is all the contents of the article "how to replace strings in a file". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.