In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How do I use the sed command in Linux? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.
Sed's Universe linuxidc linuxmi TwoCompact
For example, this Linux sed command can help you locate and create a new version of the configuration file. When these functions are run as part of a script, they are repeatable and consistent, and you can quickly implement changes.
But the main purpose of sed is to change the contents of a text file. It uses some important command line switches. / s means search and the command is separated by / g. The-I switch runs the command in the appropriate location-- it modifies the file directly.
Sed-I 's/Port 22/Port 10000 gamma g' / etc/ssh/sshd_config
In this example, the port number used in the Secure Shell server in the / etc/ssh/sshd_config file is changed from the default port 22 to port 10000.
Use the Linux sed command to make file changes
You can use sed to edit the file, but I'm a little dissatisfied. Ad hoc editing can cause problems because sed does not have access to the complete source code and cannot recognize errors or spelling errors. In addition, doing so puts the original file at risk, because once you change the original code, you cannot restore the original code.
You can specify more than one change at a time using the-e switch. Similarly, the sshd_config.conf file makes it easy to change multiple lines. The following script may look complex, but the operating system simply passes multiple sed change packets, each prefixed with-e.
Using the sshd_config file, you can change the port number, disable password authentication, and enable public key authentication in one step.
Sed-I-e 's/Port 22/Port 10000 go to g'-e 's/PermitRootLogin yes/PermitRootLogin no/g'-e' s/PasswordAuthentication yes/PasswordAuthentication no/g'-e 's/#PasswordAuthentication no/#PasswordAuthentication no/g' / etc/ssh/sshd_config
The search and replace function is on a new line; exploding the command with\ does not work because sed treats it as a special character.
Combine the hints in sed
You can also link multiple Linux sed commands together to change the location of the application. There is a lot of room for error in manually changing the file path, but automating it can make life easier.
The backslash used as a delimiter may not apply to some scripts, but sed allows you to change the delimiter. For example, suppose you have a log file called example.conf that contains the following:
Logpath = / var/log/mylogfile.log
To change this path to / my/alternate/path/newlog.log, you can use | as the delimiter because / does not work in the file system path.
Sed-I's | / var/log/mylogfile.log | / my/alternate/path/newlog.log | g 'example.conf
Other ways to use the Linux sed command include using ^ # MyComment to prefix the search pattern; this searches for lines that start with # MyComment. You can use it on the output side to create a new line to replace the existing one. You can also use the $character to find content at the end of the line.
To see a more advanced example, use the man sed command. It provides more detailed command and syntax decomposition.
Summary
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.