SED a stream editor

Introduction

Sed uses a stream editing mode:
The most obvious feature is that before sed processes data, it needs to provide a set of rules in advance. sed will edit the data according to this rule.


sed will process the data in the text file according to the script commands. These commands are either entered from the command line or stored in a text file.


The order in which this command executes data is as follows:
1. Only one line of content is read at a time. Match and modify the data according to the provided rules and commands.
Note that sed will not directly modify the source file data by default, but will copy the data to the buffer, and the modification is limited to the data in the buffer;
2. Output the execution result.
3. When one line of data is matched, it will continue to read the next line of data and repeat this process until all the data in the file is processed.

sed basic options

sed [options] [script command] filename

options
-e Script command, this option will add the following script command to the existing command.

-f script command file, this option will add script commands in subsequent files to existing commands.

-n By default, sed will automatically output the processed content after all the scripts are specified to be executed.
This option will shield the startup output. You need to use the print command to Complete the output.

-i This option will directly modify the source file, so use it with caution.

Find and replace

s
Find and replace script commands. The basic format of this command is:
[address]s/pattern/replacement/flags

address indicates the specific line to be operated on, and
pattern refers to the content that needs to be replaced ,
replacement refers to the new content to be replaced.

flags
Options
n Numbers between 1 and 512, which indicate the number of occurrences of the specified string to be replaced before replacement.
For example, There are 3 A in a row, but the user only wants to replace the second A, this is the mark;

g Replace all the matched content in the data,
If Without g, the replacement operation will only be performed when the first match succeeds.
p will print the lines that match the pattern specified in the replace command. This flag is usually used with the -n option.
w file Write the contents of the buffer to the specified file file;
& Replace with the contents matched by the regular expression;
\n matches the nth substring, this substring Previously, it was specified with \(\) in the pattern.
\ Escape (the escape replacement part includes: &, \, etc.).


sed -n "s/aa[az]aa/aa666aa/p" ld
Find, replace and print

Common commands

a\string Add one or more lines after the current line. When there are multiple lines, except for the last line, the end of each line needs to be continued with "\".

c\ Replace the text in the current line with the new text after this symbol. When there are multiple lines, except for the last line, the end of each line needs to be continued with "\"

i\ to insert text before the current line. When there are multiple lines, except for the last line, the end of each line needs to be continued with "\".

d Delete line

h Copy the contents of the pattern space to the temporary buffer

H Append the contents of the pattern space to the temporary storage buffer

g Copy the contents of the temporary buffer to the pattern space, overwriting the original contents

G Append the contents of the temporary buffer to the pattern space and append to the original contents

l List non-printing characters

p print Line

n Read in the next input line and start processing it from the next command instead of the first command

q End or exit sed

r Read input lines from the file

! Apply commands to all lines except the selected line

s Replace one string with another

g Global replacement within the line

w Write the selected line to the file

x Exchange the contents of the temporary buffer and the pattern space

y replace the character with another character (you cannot use the y command for regular expressions)

example

sed "2d" ld
Delete the second line

sed "/[0-9]/d" ld
Use a regular pattern to match numbers and delete

sed "/12 /d" ld
Match 12 lines and delete

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 = 5131 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.