Linux sed detailed

Source: Internet
Author: User

Text Processing Three musketeers:
grep, Egrep, Fgrep: Text filter
Sed:stream Editor, stream editor, line
awk: Text formatting tool, Report Builder


Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file.


SED command format

sed [OPTION] ... ' Script ' [Input-file] ...
Script: Address delimitation Edit Command


Common options (option):
-N: does not output the contents of the mode space to the screen;
-e script,--expression=script: multi-point editing;
-f/path/to/sed_script_file
One edit command per line;
-R,--regexp-extended: supports the use of extended regular expressions;
-i[suffix],--in-place[=suffix]: Directly edit the original file;
~]# sed-e ' [email protected]^#[[:space:]]*@@ '-e '/^uuid/d '/etc/fstab

Address delimitation (script):
(1) Empty address: To deal with the full text;
(2) Single address:
#: Specify line;
/pattern/: Each row that is matched by this pattern;
(3) Address range
#,#:
#,+#:
#,/pat1/
/pat1/,/pat2/
$: last line;
(4) Step forward: ~
: All Odd lines
2~2: All even lines

Edit command:
D: Delete;
P: Displays the contents of the mode space;
A \text: Append the text "text" After the line, support using \ n to implement multi-row append;
I \text: Insert text "text" in front of line, support using \ n to implement multiline insertion;
C \text: Replace the matched line with the text "text" specified here;
W/path/to/somefile: Saves the line of pattern space to the specified file;
R/path/from/somefile: Reads the contents of the specified file to the current file by the pattern to match the line, file merge;
=: Prints the line number for the line to which the pattern matches;
!: conditional inversion;
Address delimitation! Edit command;
s/*/*/*: Find replacement, its delimiter can be self-specified, commonly used have [email protected]@@, s## #等;
Replace tag:
G: global substitution;
W/path/to/somefile: Saves the result of the substitution success to the specified file;
P: Shows the successful replacement line;


example : (/etc/fstab file cp to/tmp/fstab)


Delete a row

[Email protected] ~]# sed ' 1d '/tmp/fstab #删除第一行


[Email protected] ~]# sed ' $d '/tmp/fstab #删除最后一行


[Email protected] ~]# sed ' 1,2d '/tmp/fstab #删除第一行到第二行


[Email protected] ~]# sed '/boot/d '/etc/fstab #删除包含boot字符串的行


Show a row

[Email protected] ~]# sed-n ' 6p '/tmp/fstab #显示第6行

Here to add the-n option, otherwise the contents of the schema space is the entire contents of the file, the default will be output.


[Email protected] ~]# sed-n ' 2, $p '/tmp/fstab #显示第二行到最后一行


Querying using a pattern

[[email protected] ~]# sed-n '/^#/p '/tmp/fstab #查询以 # start the line and output


Text Insert, replace


Text insertion

[[Email protected] ~]# sed '/^#/a \this test '/tmp/fstab

A \text: Append the text "text" After the line, support using \ n to implement multi-row append;


[[Email protected] ~]# sed '/^#/i \this test '/tmp/fstab

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


Line substitution

[[Email protected] ~]# sed '/^#/c\* '/tmp/fstab

replace lines that begin with # with *


[Email protected] ~]# sed-n '/^#/w/tmp/sed.txt '/tmp/fstab

saves the pattern space that matches the line to the specified file. Save lines beginning with # to/tmp/sed.txt


File merge ( reads the contents of the specified file to the line after the current file is matched to the pattern )

[Email protected] ~]# sed '/^#/r./abc.txt '/tmp/fstab

Add the contents of the abc.txt file after the lines that begin with the # number


Print line numbers for lines to which the pattern matches

[[Email protected] ~]# sed '/^#/= '/tmp/fstab


Text Find and replace

[[Email protected] ~]# sed ' [email protected]#@*@g '/tmp/fstab

Replace # with *. Text string substitution


[[Email protected] ~]# sed ' [email protected]#@*@w abc.txt '/tmp/fstab

writes lines that match the replacement criteria to the text


[Email protected] ~]# sed ' [email protected]@[email protected] '/tmp/fstab

To replace the UUID with a UUID



Exercise 1: Remove all white-space characters from the beginning of all lines in the/boot/grub/grub2.cfg file that begin with a blank character;

[[Email protected] ~] #sed ' [email protected]^[[:space:]]\[email protected]@ '/etc/grub2.cfg


Exercise 2: Remove all white-space characters from the beginning of the line at the beginning of the lines in the/etc/fstab file, preceded by #, and #;

[[Email protected] ~]# sed ' [email protected]^#[[:space:]]*@@ '/etc/fstab


Exercise 3: Output an absolute path to the SED command and take out its directory, which behaves like dirname;

[Email protected] ~]# echo "/var/log/messages/" | Sed ' [email protected][^/]\+/\[email protected]@ ' [[email protected] ~]# echo '/var/log/messages ' | Sed-r ' [Email protected][^/]+/[email protected]@ '


------------------------------------------Advanced-----------------------------------------

This article is from the "Tao Sound Still" blog, please make sure to keep this source http://zhaoyongtao.blog.51cto.com/10955972/1754663

Linux sed detailed

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.