Sed quick query manual

Source: Internet
Author: User
Sed (a stream editor)
Four spaces: input stream, pattern buffer, output stream, and hold Buffer
The basic operation process is:
(1) Put the current row of the input stream into the pattern buffer, and then the pointer of the input stream points to the next row;
(2). process the rows in the pattern buffer;
(3). Put the processing result of 2 into the output stream. Then loop through this process.

The hold buffer is another space. You can use commands to interact with the pattern buffer.

Sed command Introduction

1. Basic command --- "replace": S
1.1 Basic usage
E.g. Sed's/day/night/'<old> New
In this example, replace the day that appears for the first time in each row in the file old with night, and output the result to the file new
S "replace" command
/../Delimiter (delimiter)
Day search string
Night replacement string

In fact, the delimiter "/" can be replaced by other symbols, such as ",", "|.
E.g. Sed's // USR // local // bin // common // bin/'<old> New
It is equivalent to SED's _/usr/local/bin _/common/bin _ '<old> New
Obviously, it is much better to use "_" as a separator.

1.2 use & to represent matched strings

Sometimes you may want to add some characters around or near the matched string.
E.g. Sed's/ABC/(ABC)/'<old> New
In this example, brackets are added before and after ABC.
This example can also be written as SED's/ABC/(&)/'<old> New
The following is a more complex example:
Sed's/[A-Z] */(&)/'<old> New
Sed's/[0-9] */&/'<old> New

1.3 Use/1,/2,...,/9 to represent the matched string

E.g. Sed's // ([A-Z] */) [0-9] * // 1/'<old> New
In this example,/1 refers to the preceding/([A-Z] */).

Sed's // ([A-Z] */)/([0-9] */)/2/1/'<old> New
In this example,/2 and/1 represent the previous/([0-9] */) and/([A-Z] */).

/1,/2,...,/9 can also appear in the search string
E.g. Sed's // ([A-Z] */)/1 // 1/'<old> New
In this example, we can remove duplicate words consisting of letters.

1.4 "replace" option
1.4.1/g replace all
Sed only replaces the first appearance of the search string by default. You can use/g to replace all
Such,
Sed's // ([^] */)/(&)/G' <old> New

1.4.2 use/1,/2,... to indicate the occurrence of replacement
E.g. Sed's/[^] * // 2' <old> New
Can be used from/1/512

1.4.3/P print Option
When the SED command has the-n option, this command does not use output.
-N with/P options, if the row is indeed replaced, the line is output; otherwise, no output is made.

1.4.4/W filename is written to file filename
E.g. Sed's // ([0-9] */)/([A-Z] */) // 2/W new' <old
In this example, the output is put into the file new.

1.5 replace and insert line breaks
Replace (echo a; echo X; echo y) | sed '/x $ /{
N
S: X/N: X:
}'

Insert
(Echo a; echo X; echo y) | SED's: X/
:'

2. Only processing specific rows

2.1 limit by row number
Sed's 3 S/[0-9] [0-9] * // '<old> new processes only 3rd rows

Sed's 1,100 S/A/'<old> New only processes 1 to 100 rows

Sed '2017, $ S/A/'<old> new processes 101 to the last row of the file

Sed '2017, $! S/A/'<old> New here! Indicates that only 1 to 100 rows are replaced ,! The role is to reverse

2.2 use regular expressions
Sed '/start/,/Stop/S/#. * //' <old> New
In this example, sed first finds the row with start as the start, and finds the last row with stop as the end.
To perform operations.
Repeat the above process until the end of the file

In the following example, the row number and regular expression are used together to limit
Sed '1,/start/S/#. * // '<old> new processes 1st rows with start

3. Other simple commands
3.1 DELETE command d
Sed '11, $ d' <old> New deletes the row from 11 to the end of the file
Sed '/^ #/d' <old> New deletes all rows starting #.

3.2 print command P (note the difference with the/P option of the S command)
Sed 'P' <old each line will be output twice
Sed-N 'P' <old each line will be output once (-N blocked once)
Sed '/^ $/P' <old outputs only for empty rows twice, and the other outputs only once
Sed-n'1, 10 P' <old output the first 10 rows
Sed-n'/match/P' <old outputs rows containing match

3.3 quit command Q
Sed '11 Q' <old output the first 10 rows (exit from the first 10 rows)
Note: The Q command cannot receive multiple lines, for example
Sed '2, 5 Q' <old is incorrect

3.4 write the file command W filename (note the difference with the/W option of the S command)
Write certain rows to the file filename
Sed-n'/^ [0-9] * [02468]/W even' <old writes the row starting with an even number to the file even

3.5 output line number command =
Sed-n'/pattern/= '<old: When a row containing pattern is encountered, the row number is output at the same time.

3.6 append, change, Insert a new row
APPEND Command
#! /Bin/sh
Sed'
/Word//
Add this line after every line with word
'

Change command C
#! /Bin/sh
Sed'
/Word/C/
Replace the current line with the line
'

INSERT command I
#! /Bin/sh
Sed'
/Word/I/
Add this line before every line with word
'

3.7 conversion command y
Sed 'y/abcdef/'<old this example converts the characters abcdef into uppercase letters respectively.

3.8 command l that displays the control Letter of the line
Sed '1, 10 l' <old

3.9 D and D commands
D command to delete the content in the pattern buffer into the next operation cycle
D command to delete the content before the first line break in pattern buffer into the next operation cycle, as shown in
If there is still content in the pattern buffer, you do not need to read it from the input stream.

3.10 p command and p command
The p command outputs the content in the pattern buffer.
The p command outputs the content before the first line break in the pattern buffer.

3.11 n commands and N commands
The N command reads the next line into the pattern buffer (if the-n option is not used, the original line is output)
N command to append the next line to pattern buffer

3.12 process control commands
B label command: Jump to label on the specified line
T label command: If a line is replaced, jump to label
T label command: If there is no replacement in a line, jump to label

4. parameters when SED is called

4.1-E script: Execute the script.
E.g. Sed-E's/a/A/'-E's/B/' <old> New
Run the following command for each row /'

4.2-N disable output
Here,-N works with the previous/P to output only modified rows.

4.3-F scriptname: add the SED command in the scriptname file to this sed call.
E.g. Sed-F sedscript <old> New

The content of sedscript may be as follows:
# Sed comment-this script changes lower case vowels to upper case
S/A/G
S/E/g
S/I/g
S/o/g
S/u/g

5. Hold Buffer

X command: Put the pattern buffer into the hold buffer, and output the content of the hold buffer.
The buffer content is changed to the next line.
H command: Put pattern buffer into the hold buffer and output the content of pattern buffer,
The content of the pattern buffer is changed to the next line.
H command: append the pattern buffer to the hold Buffer
G and G commands: G replaces the content of pattern buffer with the content of hold buffer, while G will hold the content
Append to pattern buffer

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.