Practical basic Linux sed command example code

Practical basic Linux sed command example code

Summarize

The above is the practical basic Linux sed command example code introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Linux tutorial on replacing strings using sed command
  • Use the sed command to modify the kv configuration file in Linux
  • A brief discussion on the use of sed and awk commands in Linux
  • Briefly describe the Linux text processing command "sed"
  • Commonly used sed commands in Linux
  • Detailed explanation of linux sed command (recommended)
  • Summary of the use and precautions of sed command in Linux
  • One shell command a day Linux text content operation series - sed command detailed explanation
  • Usage of sed command in linux
  • Use of Linux sed command

The Linux stream editor is a useful way to run scripts in the data center. With these command examples, you can start to become familiar with sed.

Linux administrators who want to modify files without overwriting the original have many options, but one of the most effective tools is the stream editor - sed.

The stream editor is a default part of most Linux distributions. It enables you to use the Linux sed command to perform text file operations in the operating system.

Like most Linux applications, sed can handle piped input, which makes it an effective scripting tool. You can use it as a basic find and replace tool, as shown in the following example command, which finds one occurrence of a and replaces it with two instances. The command ends with /g.

sed 's/one/two/g' linuxidc linuxmi 

For example, this Linux sed command can help you locate and create new versions of configuration files. When these functions are run as part of a script, they are repeatable and consistent, and you can implement changes quickly.

But the main purpose of sed is to change the contents of text files. It uses some important command line switches. /s means search, and the command is separated by /g. The -i switch runs the command in place - it modifies the file directly.

sed -i 's/Port 22/Port 10000/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 Linux sed command to make file changes

It is possible to edit the file using sed, but it is a bit unsatisfying. Ad hoc editing can cause problems because sed does not have access to the complete source code and cannot identify errors or typos. Also, doing so puts the original file at risk because once you change the original code, there is no way to recover it.

You can specify multiple changes at once using the -e switch. Again, the sshd_config.conf file makes it simple to change multiple lines. The script below may look complicated, but the operating system is simply passing multiple packets of sed changes, 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/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 functions are on a new line; breaking up the command with \ does not work because sed treats it as a special character.

Combining prompts in sed

You can also chain multiple Linux sed commands together to change the location of an application. Manually modifying file paths leaves a lot of room for error, but automating it can make life easier.

Backslash used as a delimiter may not work with some scripts, but sed allows you to change the delimiter. For example, suppose you have a log file called example.conf with the following content:

logpath = /var/log/mylogfile.log

Change this path to /my/alternate/path/newlog.log , you can use | as separator because / does not work in file system paths.

sed -i 's|/var/log/mylogfile.log|/my/alternate/path/newlog.log|g' example.conf

Other ways to use the Linux sed command include prefixing the search pattern with ^#MyComment; this searches for lines that begin with #MyComment. You can use this on the output side so that a new line is created to replace an existing one. You can also use the $ character to find content at the end of a line.

To see more advanced examples, use the man sed command. It provides a more detailed breakdown of the commands and syntax.

<<:  Summary of Mysql slow query operations

>>:  JavaScript pie chart example

Recommend

4 solutions to mysql import csv errors

This is to commemorate the 4 pitfalls I stepped o...

Summary of nginx configuration location method

location matching order 1. "=" prefix i...

Example code for element multiple tables to achieve synchronous scrolling

Element UI implements multiple tables scrolling a...

JavaScript implements countdown on front-end web page

Use native JavaScript to simply implement the cou...

Several methods of implementing carousel images in JS

Carousel The main idea is: In the large container...

Bootstrap+Jquery to achieve calendar effect

This article shares the specific code of Bootstra...

How to use Greek letters in HTML pages

Greek letters are a very commonly used series of ...

Detailed process analysis of docker deployment of snail cinema system

Environmental Statement Host OS: Cetnos7.9 Minimu...

Solve the problem of garbled data in MySQL database migration

Under the instructions of my leader, I took over ...

Significantly optimize the size of PNG images with CSS mask (recommended)

This article is welcome to be shared and aggregat...

MySQL database transaction example tutorial

Table of contents 1. What is a transaction? 2. Th...

Summary of HTML horizontal and vertical centering issues

I have encountered many centering problems recent...

Introduction to Vue life cycle and detailed explanation of hook functions

Table of contents Vue life cycle introduction and...

How to implement a simple HTML video player

This article introduces the method of implementin...