The Linux shell sed

Source: Internet
Author: User
Tags printable characters

The SED editor processes the input line by row, and then sends the results to the screen.

-i option: Direct Action source file, source file will be modified.

sed commands and options:

A\ Add one or more rows after the current line
C\ Replace text in the current line with new text
D Delete Row
I\ Insert text before the current line
H Copy the contents of the schema space to the staging buffer
H Add the contents of the schema space to the buffer
G Remove the contents of the staging buffer and copy it to the pattern buffer
G Remove the contents of the staging buffer and append it to the pattern buffer
L List non-printable characters
P Print Line
N Reads the next line of input and processes it from the next rather than the first command
Q End or exit sed
R Reading input rows from a file
! Apply all commands to a row other than the selected row
S Replace another string with one string

Replace flag:

G Global substitution within a row
P Print Line
W Writing rows to a file
X Swapping the contents of a staging buffer and pattern space
Y Convert a character to another character

Examples of sed:

Print: P command

Sed '/abc/p ' file

Prints the line containing ABC in file. By default, sed prints all rows to the screen, and if a row matches the pattern, print the line again

Sed-n '/abc/p ' file Just like above, only the default behavior of SED is removed, and only matching rows are printed

Delete: D command

Sed ' 3, $d ' file Deletes the contents from line 3rd to the last row.
Sed ' $d ' file Delete the contents of the last row
Sed '/abc/d ' Delete the line that contains the ABC.
Sed ' 3d ' file Delete the contents of the third row

Replace: s command

Sed ' s/abc/def/g ' file Replace all ABC in the line with Def, and if there is no G, replace only the first ABC in the row
Sed-n ' s/abc/def/p ' file Print only those rows where the substitution occurred
Sed ' s/abc/&def/' file Add Def (& to match content) behind all ABC
Sed-n ' s/abc/def/gp ' file Replace all ABC with DEF and print the lines where the substitution occurred
Sed ' s#abc#def#g ' file Replace all ABC with DEF, followed by the character behind S to find the string and
Replace the split character between the strings, in this case try #

Specify the range of rows: comma

Sed-n '/abc/,/def/p ' file Print mode ABC to DEF line
Sed-n ' 5/,/def/p ' file Prints the line from line fifth to the containing Def line.
sed/abd/,/def/s/aaa/bbb/g Modify the line between pattern ABC and mode DEF to replace AAA with Def

Multiple edits-E

Sed-e ' 1,3d '-e ' s/abc/def/g ' file Delete 1-3 lines and replace the remaining lines of ABC with DEF

Read file: R command

Sed '/abc/r newfile ' file Read the contents of the NewFile after the line containing ABC

Write file: w command

Sed '/abc/w newfile ' file Write newfile on lines that contain ABC

Append: a command

Sed '/abc/a\def ' file A new line after the line containing ABC, written to Def

insert: I command

Sed '/abc/i\def ' file A new line before the line containing ABC, written to Def

Modify: C command

Sed '/abc/c\def ' file Replace the line containing ABC with DEF and the old text is overwritten

Read Next line: N command

Sed '/abc/{n; s/aaa/bbb/g;} ' file Reads the next line of lines containing ABC, replacing AAA for BBB

convert: Y command

Sed ' y/abc/abc ' file Replace A with a, a, a, a, or a b,c with a C (regular expression metacharacters do not work)

exit: Q command

Sed '/abc/{s/aaa/bbb/; q;} ' file Include ABC in a line, replace AAA with BBB, and then exit sed.

Staging and fetching: H Command (store pattern rows to staging buffer) and g (remove row from staging buffer and overwrite pattern buffer) g (row to remove temporary buffer) command

H and G are replication behaviors (Overrides), and H and G represent append.

Sed-e '/abc/h '-e ' $G ' file Rows containing ABC are saved to the staging buffer by the H command, and in the second command sink, when Sed reads the last line of $, the G command reads a row from the staging buffer, appended to the pattern buffer. That is, the last line of all lines that contain ABC is copied to the end of the file.
Sed-e '/abc/{h; D;} '
-E '/def/{g;} ' file
Rows containing the ABC are moved to the line containing Def and overwritten.

Staging and interchange: H and X Commands

Sed-e '/abc/h '
-E '/def/x ' file
Contains

The Linux shell sed

Related Article

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.