In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the sed command to replace strings under Linux. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
The sed command replaces the string of the specified file and outputs it according to the specified format.
To replace the string, we need to use the following format.
$sed s / replaced target string / replaced string / file name
Below we replace the string "sample.txt" written as "appleorangemelon".
$sed s/orange/ORANGE/ sample.txt
The execution result is
AppleORANGEmelon
Replace and output the string.
In addition, as shown below, you can get the same result by connecting the sed command with "|" after the cat command.
$cat sample.txt | sed s/apple/APPLE/
Note that the sed command only replaces the string and outputs it, but does not overwrite the contents of the actual file
If you want to save the replacement in text, use redirect >.
Options used by the sed command
The command option description-e is replaced with the specified script-f file to add the contents of the script file described in the specified file-r uses extended regular expressions
Use of the sed command
Replace all lines
In the format described earlier, only the first matching string is replaced, even if there is a string in one line that matches multiple replacement object strings.
Therefore, to replace all matching strings:
$sed-e s/apple/APPLE/g sample.txt
The execution result is
APPLEorangemelonAPPLE
Replace the beginning and end of a line
$sed-e "s / ^ apple / APPLE/" sample.txt$ sed-e "s/apple\ $/ APPLE/" sample.txt
If you want to replace multiple substrings, you can specify multiple scripts.
$sed-e "s/apple/APPLE/"-e "s/orange/ORANGE/" sample.txt
Delete Row
Specify "d" to delete the specified row. For example, to delete the second line is "2d".
$sed-e '2d' sample.txt
In addition, you can delete multiple lines, as shown below: delete lines 1 to 3.
$sed-e '1jade 3d' sample2.txt about the method of replacing strings with sed commands under Linux is shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.