Sed commands use metacharacters with regular expressions, and sed commands use regular expressions

Source: Internet
Author: User

Sed commands use metacharacters with regular expressions, and sed commands use regular expressions

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. Example: sed-n' $ P'/etc/issue

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
A \
Add one or more rows after the current row. In addition to the last line of multiple rows, the end of each line must be followed by "\" to continue row-a, only to display. To change the text, you must sed-I. bak '', a file with the bak format will be generated. This file is the source file.
[Root @ entos74 app] # cat-n passwd.txt | sed '10, 20a \ 11 adadadadadada'
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.
Cat-n passwd.txt | sed '10c \ xxxxxxxxxxxxxxxxxxxxxxxxxx'
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.
Cat-n passwd.txt | sed '10i \ xxxxxxxxxxxxxxxxxxxxxxxxxx'
D
Delete row: ss-ntl | sed '1d 'Delete the first 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, and append the content to the end of the original content.
P
Print the content of the current mode space and append it to the default output
N
Read the next input line and process it from the next command instead of the first command.
Q
End or exit sed
R
Read input lines from files
!
Apply commands to all rows other than the selected row
S
Replace another string
G
Global replacement within a row: cat-n passwd.txt | sed's @/bin/bash $ @/sbin/nologin @ G'
W
Write the selected row to the file sed '/^ lixiaozi/w/app/lixiaozi.txt' passwd.txt
X
Swap content of the temporary buffer and mode space
Y
Replace the character with another character (the y command cannot be used for the regular expression)

3.2 sed options
Option
Function
-E
Multiple edits, that is, when multiple sed commands are applied to the input line
-N
Cancel default output
-F
Specify 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

Metacharacters
Function
Example
^
First line Separator
/^ My/match all rows starting with my
$
Line tail Locator
/My $/match all rows ending with my
.
Match a single character except line breaks
/M .. y/match with the letter m, followed by two arbitrary characters, followed by the letter y line
*
Matches zero or multiple leading characters
/My */match the row containing the letter m followed by zero or multiple y letters
[]
Match any character in the specified character group
/[Mm] y/match the rows that contain My or my
[^]
Match any character that is not in the specified character group
/[^ Mm] y/matches y, but the character before y is not a line of M or m
\(..\)
Save matched characters
1, 20 s/\ (you \) self/\ 1r/mark the mode between metacharacters and save it as tag 1. Then you can reference it with \ 1. 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 it in the replacement string
S/my/** & **/Symbol & represents the search string. My will be replaced with ** my **
\ <
First-word Locator
/\ <My/match rows that contain words starting with my
\>
Suffix
/My \>/match rows that contain words ending with my
X \ {m \}
M x in a row
/9 \ {5 \}/match rows containing 5 consecutive 9
X \ {m ,\}
At least m x
/9 \ {5, \}/match rows containing at least five consecutive 9
X \ {m, n \}
At least m, but no more than n x
/9 \ {5, 7 \}/match rows containing 5 to 7 consecutive 9




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.

[Root @ entos74 ~] # Sed-n '3' filelist.txt
Print only the third row

Only show the file content within the specified row range, for example:
# Only view rows 20th to 30th of the file and include the row number
Cat-n/etc/passwd | sed-n '20, 30p'

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.
Cat-n passwd.txt | sed '/mail/, 25d'
# Delete the contents of rows containing "mail" to 25th rows

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"


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.

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 million commands
Sed-n'/hrwang/w me.txt 'datafile

6.6 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.7 I \ command
The I \ command inserts new text before the current line.

6.8 c \ command
Sed uses this command to modify existing text into new text.

6.9 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








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.