Flow editor SED

Sed introduction

Sed is a stream Editing tool, used to filter and replace text, especially when you want to make unified changes to dozens of configuration files, you will feel the charm of Sed! Sed processes certain instructions and outputs them by reading only one line at a time, so Sed is more suitable for processing large data files. First of all, Sed reads the content of the file through a file or pipe, but Sed does not directly modify the source file by default, but copies the read content to the buffer. We call it pattern space. All instruction operations All are performed in the pattern space, and then Sed processes the content in the pattern space according to the corresponding instructions and outputs the result, which is output to the standard output (that is, on the screen) by default. The Sed workflow is shown in the figure below:

share picture

Sed basic syntax format

Sed reads data from the file. If there is no input file, it will process the standard input process data by default. The script command is the first parameter that does not start with “-“. The specific syntax format is as follows.

Usage: sed[option]…{script instruction} [input file]…
Options: –version        Show sed version
   -h or –help        Show help Document
   -n, –quiet, –silent  silently output. By default, the sed program will Automatically print the content in the mode space, this option can block automatic printing

   -e script      Allow multiple script commands to be executed

   -e script     span>
   -f script-file    Reading script instructions from files, very useful for writing automatic script programs
   -i, -in-place    Use with caution, this option will directly modify the source file
   -l N           Specify the length of the line that the l command can output. The l command is to output non-printing characters.
   –posix       disable GNU sed extensions
   -r         Use extended regular expressions in script commands
   -s, – separate   By default, sed will treat multiple input file names as one long continuous input stream. GNU sed allows them to be treated as separate files.
   -u, -unbuffered  Minimal buffered input and output
p>

Sed introductory example
1. Basic format example
Sed processes files through specific script commands. Here is a brief introduction to several script command operations as examples of Sed programs. a, append means additional instruction; i, insert means insert instruction; d, delete means delete instruction; s, substitution means replacement instruction. The basic format of the sed script command is: [address] command (some commands can only operate on one line, some can operate on multiple lines), commands can also be combined with curly braces, so that the command sequence can act on the same address.

address {

command1

command2

command3

}

Note: The first command can be on the same line as the opening brace, but the closing brace must be on a line alone. In addition, adding a space after the command will cause an error.

Sed commands and scripts

1. Summary of Sed common commands
The following table gives the descriptions of commonly used sed script commands. Let’s take a look at the details of each command. usage.

y
Instructions Function Instructions Function
s replace d Delete
a append i insert
c Change l Print (display non Print characters)
character conversion L Print (non-printable characters are not displayed)
p Print r Load file content
w Save to file q td>

Exit

Example 1

The sample file used in Example 1 is (note that there are blank lines):

[[email protected] ~]# cat test.txt
DEVICE=ens33
ONBOOT=yes
BOOTPROTO=static

IPADDR=192.168.0.1
NETMASK=255.255.255.0

GATEWAY=192.168 .0.254

Example 1: Delete blank lines in the file

Write the sed script as:

[[emailprotected] ~]# cat sed.sh< /span>
/.*/{
/^$/d< /span>
}

The results of executing the sed program are as follows :

[[emailprotected] ~]# sed -f sed.sh test.txt
< span style="font-size: 16px;">DEVICE=ens33
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.1
NETMASK=25 5.255.255.0
GATEWAY=192.168.0.254

The sample files used in Example 2~Example 5 are:

[[emailprotected] ~]# cat test.txt
DEVICE=ens33
ONBOOT=yes
BOOTPROTO=static
NETMASK=255.255. 255.0
GATEWAY=192.168.0.254

Example 2: Add a line after the static line with the content IPADDR=192.168.0.1

[[emailprotected] ~]# sed ‘/static/a IPADDR=192.168.0.1’ test.txt

Example 3: Insert the content IPADDR before the line matching NETMASK =192.168.0.1

[[emailprotected] ~]# sed’/NETMASK/i IPADDR=192.168.0.1′ test. txt

Example 4: Change the content of the ONBOOT line to ONBOOT=no

< p>[[email protected] ~]# sed’/ONBOOT/c ONBOOT=no’ test.txt

Example 5: Print (l) the content in the display mode space, display non-printing characters, usually used with -n, otherwise it will be output twice

[[email protected] ~]# sed -n ‘1,2l’ test.txt # In the sed script file, #nshield automatic output is required

The results are as follows:

DEVICE =ens33$
ONBOOT=yes$

Example 6: Display the content of the first and second lines

Print (p): The function is similar to l (print), But it does not display non-display characters, generally used in conjunction with -n

[[emailprotected] ~]# sed -n ‘1 ,2p’ test.txt

The results are as follows:
DEVICE=ens33
ONBOOT=yes

Example 7: Exit the sed command after displaying the first two lines of test.txt content

[[emailprotected] ~ ]# sed ‘2q’ test.txt

Reference from, Ding Mingyi edited by “Linux Operation and Maintenance” 》

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5149 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.