An explanation of the SED stream editor for Linux Foundation

Source: Internet
Author: User
Tags stdin

Previously introduced the three big text editor grep, here introduced more than grep function more powerful sed flow editor


What is sed?


SED is the abbreviation of Stream editor, and man's simplicity of SED is

Sed-stream Editor for filtering and transforming text

Its main function is to filter and replace the text.


how SED works


the working process of SED : SED is a stream editor, the so-called flow editor refers to the SED each time only from the file or stdin read into a row, the read line is saved to the mode space and then processed according to the specified requirements, and the processed results output to the screen, and then read into the next line, The process of the entire text is output as pipelined, followed by line-by-row processing.


how the SED handles the content : SED is not directly processed in the original file or the original input, it will read the line into the buffer, the contents of the buffer cache processing, after processing the default will not write or overwrite the source file, but directly output to the screen.


It has two memory buffers called: pattern Space , and hold space is also known as the staging buffer (holding space).


In general, SED first put the first luggage into the mode space, after processing the output screen, and then the second pack into the mode space to replace the original content in the mode space, and then processing, sequentially loop, until the end.


Here is a diagram of my understanding of the SED workflow.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/85/9E/wKioL1eqjSPwnHDFAACW8D5G46E767.jpg "title=" Sed.jpg "width=" 720 "height=" 541 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:720px;height:541px; "alt=" Wkiol1eqjspwnhdfaacw8d5g46e767.jpg "/>

1. Sed reads a line from the contents of the stdin into the mode space each time

2, after the SED according to the pattern space matching conditions to match the conditions to enter the next stage of ordinary editing, not satisfied by default to the standard output, the end

3, after the ordinary editing in two stages, 1) after the ordinary editing of the selective display to stdout, end; 2) Enter holding space for advanced editing, after the end of the edit selective display to stdout, end.


SED syntax


Sed [OPTION]... {Script-only-if-no-other-script} [Input-file] ...

{Script-only-if-no-other-script}: Can be understood as the address delimitation Edit command.

Common Options :

-N: Do not output the contents of the mode space to the screen

Suppress automatic printing of pattern space

-e: Multi-point editing

Add the script to the commands to be executed

-F: One edit command per line

Add the contents of Script-file to the commands to be executed

-r: Using extended expressions

Use extended regular expressions in the script.

-I: Edit the original file directly

Edit files in place (makes backup if SUFFIX supplied)


Address Delimitation :

1, empty address: The full-text processing .

2. Single Address:

#: Specify line;

/pattern/: Each row that is matched by this pattern;

3. Address range:

#,#: All lines from # start to # End

#,+#: Start from # down to + #的行之间的所有行

#,/pat1/all rows from # start to matching lines to the first occurrence of the/pat1/pattern

/pat1/,/pat2/: First match all rows to the/pat1/line to the first match to the/pat2/row

4. Step in: ~

2~2: All odd rows: all even lines


Edit Command : The edit command must be used in conjunction with the address delimitation, which will cause an error if the edit command is missing.

D: Delete the contents of the pattern space. Delete pattern spaces

A \text inserting text "text" after a row, enabling multiple-line insertion using \ n

Append text, which have each embedded newline preceded by a back slash.

I \text insert text "text" in front of line, support using \ n to implement multiline insertion

Insert text, which have each embedded newline preceded by a back slash.

C \text replace the matched line with the text "text" specified here

Replace the selected lines with text, which have each embedded new line preceded by a backslash.

W filename Save mode space matches the line to the specified file

Write the current pattern space to filename.

R FileName is merged after reading the specified file contents to the line file that the current file is matched by the pattern

Append text read from filename.

=: Prints the line number of the line to which the pattern matches.

! : Conditional inversion

s/regexp/replacement/: Find replacement, its delimiter can be self-specified, often have [email protected]@@,s###

P: Show the rows that were successfully replaced

G: Global Substitution

W FileName: Saves the result of the substitution success to the specified file

P: Displays the contents of the current pattern space in Print, the present pattern spaces.


Example Demo :

1. Delete all whitespace characters from the beginning of lines in the/etc/grub2.conf file that begin with whitespace

Sed ' s#^[[:space:]]\+## '/etc/grub2.cfg

Here # #是将前面匹配到的内容删除的意思, the text above is too much, it will not be intercepted.

2. Remove all whitespace characters from the beginning of the line beginning with #, followed by at least one white-space character, in the/etc/fstab file.

Sed ' [email protected]^#[[:space:]]\[email protected]@ '/etc/fstab
[[Email protected] test]# cat /etc/fstab ## /etc/fstab# created by  anaconda on tue jul 19 23:52:41 2016## accessible filesystems,  by reference, are maintained under  '/dev/disk ' # see man pages  fstab (5),  findfs (8),  mount (8)  and/or blkid (8)  for more info#/dev/ mapper/centos-root /                        xfs     defaults         0 0UUID=6efb8a23-bae1-427c-ab10-3caca95250b1 /boot                    xfs      defaults        0 0/dev/mapper/ centos-swap swap                     swap     defaults        0 0[[email protected ] test]# sed  ' [email protected]^#[[:space:]]\[email protected]@ '  /etc/fstab#/etc /fstabcreated by anaconda on tue jul 19 23:52:41 2016#accessible  filesystems, by reference, are maintained under  '/dev/disk ' See man  Pages fstab (5),  findfs (8),  mount (8)  and/or blkid (8)  for more info#/dev /mapper/centos-root /                        xfs     defaults         0 0UUID=6efb8a23-bae1-427c-ab10-3caca95250b1 /boot                    xfs      defaults        0 0/dev/mapper/centos-swap swap                      swap    defaults        0 0

3, in/etc/ Fstab the beginning of each line, add #

cat /etc/fstab                            ## /etc/fstab# Created  By anaconda on tue jul 19 23:52:41 2016## accessible filesystems,  by reference, are maintained under  '/dev/disk ' # see man pages  fstab (5),  findfs (8),  mount (8)  and/or blkid (8)  for more info#/dev/ mapper/centos-root /                        xfs     defaults         0 0UUID=6efb8a23-bae1-427c-ab10-3caca95250b1 /boot                    xfs      d efaults        0 0/dev/mapper/centos-swap swap                      swap    defaults        0 0 sed  ' [ email protected]^@#&@ '  /etc/fstab##### /etc/fstab## created by anaconda  on Tue Jul 19 23:52:41 2016#### Accessible filesystems, by  reference, are maintained under  '/dev/disk ' ## see man pages fstab (5),  findfs (8),  mount (8)  and/or blkid (8)  for more info###/dev/mapper/ centos-root /                        xfs     defaults         0 0#uuid=6efb8a23-bae1-427c-ab10-3caca95250b1 /boot                    xfs     defaults         0 0#/dev/mapper/centos-swap swap                     swap     defaults        0 0

4, processing/etc/fstab path, use sed command to take out its directory name and base name

Directory name :

[Email protected] test]# echo '/etc/fstab ' | Sed-r ' s# (. */) ([^/]+/?) #\1# '/etc/

Base name :

[Email protected] test]# echo '/etc/fstab ' | Sed-r ' s# (. */) ([^/]+/?) #\2# ' Fstab


Sed Advanced Edit command:

H: The content of the pattern space is covered in the holding space;

H: Append the contents of the pattern space to the holding space;

G: To cover the contents of the holding space in the pattern space;

G: Append the contents of the holding space to the pattern space;

x: Swap the content in the pattern space with the content in the hold space;

N: Overwrites the next line of the row to the pattern space in the read match;

N: Append the next line of the row to the pattern space to read;

D: Delete rows in the pattern space;

D: Delete all rows in multi-line mode space;


Advanced Editing Command Example Demo:

Sed-n ' n;p ' FILE: shows even rows;

Sed ' 1! G;h;$!d ' file: Displays the contents of the files in reverse order;

Sed ' $!d ' FILE: Take out the last line;

Sed ' $! n;$! D ' file: Two lines after removing the file;

Sed '/^$/d; G ' FILE: Delete all of the original blank lines, and then add a blank line after all the non-blank lines;

Sed ' n;d ' FILE: Displays odd lines;

Sed ' G ' FILE: Adds a blank line behind the original line;


This article is from "Zhang Fan-it's fantasy drifting" blog, please be sure to keep this source http://chawan.blog.51cto.com/9179874/1836438

An explanation of the SED stream editor for Linux Foundation

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.