Sed command details,

Source: Internet
Author: User
Tags printable characters

Sed command details,
1. Introduction

Sed is a non-interactive editor. It does not modify the file unless you use shell redirection to save the results. By default, all output rows are printed to the screen.

Sed editor processes files (or inputs) row by row and sends the results to the screen. The specific process is as follows: first, sed stores the currently being processed rows in a temporary cache (also known as the mode space), and then processes the rows in the temporary buffer, then, send the row to the screen. Sed deletes a row from the temporary buffer after processing, and then reads the next row for processing and display. After processing the last line of the input file, sed stops running. Sed stores each row in a temporary buffer and edits the copy. Therefore, the original file is not modified.

 

2. Addressing

The address is used to determine which rows to edit. The address format can be numbers, regular expressions, or a combination of the two. If no address is specified, sed processes all rows of the input file.

 

If the address is a number, it indicates the row number. If the address is a "$" symbol, it indicates the last row. For example:

Sed-n '3' datafile

Print only the third row

 

 

Only show the file content within the specified row range, for example:

 

# Only view rows 100th to 200th of the file

Sed-n '100,200 P' mysql_slow_query.log

 

 

The addresses are separated by commas, so the addresses to be processed are the range between the two rows (including the two rows ). The range can be represented by numbers, regular expressions, or combinations of the two. For example:

 

Sed '2, 5d 'datafile

# Delete the second to fifth line

Sed '/My/,/You/d' datafile

# Delete rows containing "My" to rows containing "You"

Sed '/My/, 10d' datafile

# Delete the content from the row containing "My" to the row 10

 

3. commands and options

 

The sed Command tells sed how to process each input line specified by the address. If no address is specified, all input lines are processed.

 

3.1 sed command

 

Command function

A \

Add one or more rows after the current row. In addition to the last row, the end of each row must be continued "\".

 

C \ Replace the text in the current line with the new text after this symbol. In addition to the last row, you must use "\" to continue the row at the end of each line.

I \ insert text before the current row. In addition to the last row, you must use "\" to continue the row at the end of each line.

D. delete a row.

H. Copy the content in the mode space to the temporary buffer.

H. append the content in the mode space to the temporary buffer.

G. Copy the content in the temporary buffer to the mode space to overwrite the original content.

G. append the content of the temporary buffer to the mode space. append the content to the end of the original content.

L list non-printable characters

P print row

N reads the next input line and starts processing the next command instead of the first command.

Q ends or exits sed

R. Read the input row from the file.

! Apply commands to all rows other than the selected row

S replaces the other with one string

G. perform global replacement within the row.

 

 

W. Write the selected row to a file

X swap content of the temporary buffer and mode space

Y replaces the character with another character (the y command cannot be used for the regular expression)

 

 

3.2 sed options

 

Option

-E: used when multiple sed commands are applied to the input line.

-N: cancel the default output.

-F specifies the sed script file name

 

 

4. Exit

 

Sed is not the same as grep. Whether or not the specified mode is found, its exit status is 0. The exit status of sed is not 0 only when the command has a syntax error.

 

5. Regular Expression metacharacters

 

Like grep, sed also supports Special metacharacters for Pattern Search and replacement. The difference is that the regular expression used by sed is the mode between the slash line.

 

If you want to change the regular expression separator "/" to another character, for example, o, you only need to add a backslash before the character, followed by the regular expression, and then followed by the character. Example: sed-n' \ o ^ Myop 'datafile

 

Metacharacter function example

^ The first line locator/^ my/matches all rows starting with my

$ Line tail locator/my $/match all rows ending with my

. Match a single character except the line break/m. y/match the letter m, followed by two arbitrary characters, followed by the letter y line

* Match zero or multiple leading characters/my */match rows containing letters m followed by zero or multiple y letters

[] Match any character in the specified character group/[Mm] y/match the row containing My or my

[^] Match any character not in the specified character group/[^ Mm] y/match y, but the character before y is not a row of M or m

\(.. \) Save the pattern between matched characters 1, 20 s/\ (you \) self/\ 1r/marked metacharacters, and save it as tag 1, you can use \ 1 to reference it later. A maximum of nine tags can be defined, starting from the left and the first tag on the left. In this example, 1st to 20th rows are processed, and you are saved as tag 1. If you find youself, replace it with your.

& Save the search string to reference s/my/** & **/Symbol & in the replacement string. My will be replaced with ** my **

\ <The first separator/\ <my = "" matches the line containing the word starting with my <= "" span = "" style = "word-wrap: break-word; ">

\> Tail locator/my \>/match the line containing the word ending with my

X \ {m \} x/9 \ {5 \}/matched rows containing 5 consecutive 9

X \ {m, \} At least m x/9 \ {5, \}/matched rows containing at least 5 consecutive 9

At least m x \ {m, n \}, but no more than n x/9 \ {5, 7 \}/matched rows containing 5 to 7 9 in a row

 

6. Example

 

6.1 p command

 

Command p is used to display the content of the mode space. By default, sed prints the input line on the screen, and option-n is used to cancel the default printing operation. When option-n and command p both appear, sed can print the selected content.

 

Sed '/my/P' datafile

 

# By default, sed prints all input rows on the standard output. If a line matches the mode my, the p command prints the line again.

 

Sed-n'/my/P' datafile

 

# Option-n cancel sed's default printing. The p command prints the line matching the mode my.

 

6.2 d command

 

Command d is used to delete the input line. Sed first copies the input line from the file to the mode space, then runs the sed command on the line, and finally displays the content in the mode space on the screen. If the command d is sent, the input row in the current mode space will be deleted and not displayed.

 

Sed '$ d' datafile

# Delete the last row, and the rest are displayed.

 

Sed '/my/d' datafile

# Delete the rows containing my, and the rest are displayed.

 

6.3 s command

 

Sed's/^ My/You/G' datafile

 

# The g at the end of the command indicates global replacement within the row. That is to say, if multiple mys appear in a row, all My s are replaced with You.

 

Sed-n'1, 20 s/My $/You/gp 'datafile

 

# Cancel the default output, process the rows from 1 to 20 matching the rows ending with My, replace all the rows in the row with You, and print them to the screen.

Sed's # My # Your # G' datafile

 

# The character that follows the s command is the separator between the search string and the replacement string. The default Delimiter is a forward slash, but it can be changed. No matter what characters (except line breaks and backlash), as long as the s command is followed, it becomes a new string separator.

 

6.4 e Option

 

-E is an editing command used when sed executes multiple editing tasks. Before editing the next line, all the editing actions will be applied to the row in the mode buffer.

 

Sed-e '1, 10d '-e's/My/Your/G' datafile

 

# Option-e is used for multiple edits. The First edits and deletes Row 1-3. The second edit replaces all My that appear with Your. Because these two commands are edited row by row (both commands are executed on the current row of the mode space), the order of the commands will affect the result.

 

6.5 r command

 

The r command is a READ command. Sed uses this command to add the content of a text file to a specific location of the current file.

 

Sed '/My/r introduce.txt' datafile

 

If a file named "datafile' is matched with a pattern", the file "introduce.txt" is read later. If the current myrow does not stop, the contents of the introduce.txt file will be read after the current myrow appears.

 

6.6 million commands

 

Sed-n'/hrwang/w me.txt 'datafile

 

6.7 a \ command

 

The a \ command is an APPEND Command that adds new text to the current row of the file (that is, the row in the Read mode buffer. The appended text line is located at the bottom of the sed command and starts from another line. If the content to be appended exceeds one row, each row must end with a backslash, except for the last row. The last line ends with quotation marks and a file name.

 

Sed '/^ hrwang/\

> Hrwang and mjfan are husband \

> And wife 'datafile

 

# If the row starting with hrwang is found in the datafile file, add hrwang and mjfan are husband wife to the row.

 

6.8 I \ command

 

The I \ command inserts new text before the current line.

 

6.9 c \ command

 

Sed uses this command to modify existing text into new text.

 

6.10 n command

 

Sed uses this command to get the next line of the input file and read it into the mode buffer. Any sed command will be applied to the next line followed by the matching line.

 

Sed '/hrwang/{n; s/My/Your/;}' datafile

Note: If you need to use multiple commands, or you need to nest an address within a certain address range, you must enclose the commands in curly brackets and write only one command per line, or use semicolons to separate multiple commands in the same line.

 

6.11 y command

This command is similar to the tr command in UNIX/Linux. The characters are converted from left to right in one-to-one mode. For example, y/abc/ABC/converts lowercase a to A, lowercase B to B, and lowercase c to C.

 

Sed '1, 20y/hrwang12/HRWANG ^ $/'datafile

# Convert all lower-case hrwang data from 1 to 20 rows to uppercase, convert 1 to ^, and convert 2 to $.

# The metacharacter of the regular expression does not work for the y command. Like the separator of the s command, the diagonal line can be replaced with other characters.

 

6.12 q command

 

The q command will cause the sed program to exit and no longer process it.

 

Sed '/hrwang/{s/hrwang/HRWANG/; q;}' datafile

 

 

6.13 h commands and g commands

 

# Cat datafile

My name is hrwang.

Your name is mjfan.

Hrwang is mjfan's husband.

Mjfan is hrwang's wife.

Sed-e '/hrwang/H'-e' $ G' datafile

Sed-e '/hrwang/H'-e' $ G' datafile

# Using the above two commands, you will find that h will clear the content of the original temporary buffer and only save the content of the mode space saved in the last execution of h. The H command appends the rows matching the hrwnag and stores them in the temporary buffer zone.

Sed-e '/hrwang/H'-e' $ G' datafile

Sed-e '/hrwang/H'-e' $ G' datafile

# Using the above two commands, you will find that g has replaced the content in the temporary buffer with the content of the current row in the mode space. Here, the last row is replaced. The G command appends the content of the temporary buffer to the current row of the mode space. Append to the end.

 

7. sed script

 

The sed script is a column of sed commands written in the file. In the script, no extra space or text is required at the end of the command. If multiple commands exist in one line, separate them with semicolons. When executing the script, sed first copies the first line of the input file to the mode buffer, and then executes all the commands in the script. After each row is processed, sed copies the next row of the file to the mode buffer and executes all the commands in the script. When using the sed script, no quotation marks are used to ensure that the sed command is not interpreted by shell. For example, sed script:

 

# Handle datafile

3i \

~~~~~~~~~~~~~~~~~~~~~

3, $ s/\ (hrwang \) is \ (mjfan \)/\ 2 is \ 1/

$ \

We will love eachother forever !!

 

 

 

# Sed-f script datafile

My name is hrwang

Your name is mjfan

~~~~~~~~~~~~~~~~~~~~~

Mjfan is hrwang's husband. # ~~~

Mjfan is hrwang's wife.

We will love eachother forever !!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.