In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to use the sed command to replace strings in Linux. For this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
The basic syntax for sed substitution is:
The code is as follows:
Sed's / original string / replacement string /'
In single quotation marks, s indicates substitution, and in the middle of the three slashes is the replacement style. special characters need to be escaped by backslash "\", but single quotation marks cannot be escaped by backslash "\". At this point, just change the single quotation marks in the command to double quotation marks, for example:
The code is as follows:
Sed "s / original string contains'/ replacement string contains'/" / / characters to be processed include single quotation marks
The three slash delimiters in the command can be replaced with other symbols, which is more convenient when there are more slashes in the content to be replaced, just follow the s definition, such as the question mark "?":
The code is as follows:
Sed's? original string? replacement string?'/ / Custom delimiter is a question mark
You can replace each matching keyword with g at the end, otherwise only the first one of each line can be replaced, for example:
The code is as follows:
Sed's / original string / replacement string /'/ / replace all matching keywords
The upper arrow "^" indicates the beginning of the line, and the dollar "$" symbol indicates the end of the line in quotation marks, but outside the quotation marks indicates the last line (the last line). It took a long time to search which symbol represents the first line, and it took a long time to remember that the first line is the number "1". So adding a string at the beginning and end of a line is to replace the end of a line and the beginning of a line, for example:
The code is as follows:
Sed's / ^ / header added & / g'/ / added at the beginning of all lines
Sed's Placement Plus & the added tail / g'/ / at the end of all lines
Sed '2s/ original string / replacement string / g' / / replace line 2
Sed'$s / original string / replacement string / g' / / replace the last line
Sed '2pr 5s / original string / replacement string / g' / / replace lines 2 to 5
Sed'2 dint strings / original string / replacement string / g'/ / replace 2 to last line
Replacement styles can be executed multiple times in the same command, separated by a semicolon, for example:
The code is as follows:
Sed's / ^ / added headers & / gumbnauts-added tail / g' / / execute two replacement rules at the same time
The output processed by sed is output directly to the screen. To save, you can redirect the output, or replace it directly in the file with the parameter "I":
The code is as follows:
Sed-I's / original string / replacement string / g 'filename / / replace all matches in the file
This is the answer to the question about how to use the sed command to replace strings in Linux. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.