The use of SED

Source: Internet
Author: User

The use of SED

1. SED command

Sed:stream Editor, stream editor, line

2, use the format:

sed [OPTION] ... ' Script ' [Input-file] ...

Script

Address Delimitation Edit Command

Common options:

-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

3, Address delimitation:

(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

4. 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;

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;

~]# 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 #;

~]# 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;

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

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


This article from "Everything from scratch, do not forget beginner's mind." "Blog, be sure to keep this provenance http://liaodijin.blog.51cto.com/10988244/1727539

The use of SED

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.