On the usage of SED

Source: Internet
Author: User

principle

Once a line of text is read into the pattern space, it will first determine whether the line of the pattern space can be matched to the pattern you define, if it can be matched to a single output operation, and then edit the operation, if you do not expect the content of the direct output mode space to the screen can add-n option

Text Processing Three musketeers:

grep, Egrep, Fgrep: Text filter

Sed:stream Editor, stream editor, line

awk: Text formatting tool, Report Builder

sed [OPTION] ... ' Script ' file1 ...

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

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

#,#: From line # to line #

#,+#: Starting from the third line down eight lines

#,/pat1/: Starting with line #, to the first match to the end of the pattern

/pat1/,/pat2/: Starting from match to first pattern, to match to second mode end

$: last line;

(4) Step forward: ~

: All Odd lines

2~2: All even lines

Edit command:

D: Delete: (shows lines that are not matched to)

Example: sed ' 1,3d '/etc/fstab shows everything from line fourth to the last line


P: Display the contents of the mode space, (the result is that the matching line is displayed two times,-n can be displayed silently, only once)

Example: sed ' 1,4p '/etc/fstab displays the entire text, and 1 to 4 lines display two times


A \text: Append the text "text" after the matched line, supporting the use of \ n to implement multi-line append;

I \text: Inserts the text "text" in front of the matching line, supports using \ n to implement multi-line insertion;

Example: sed ' 3i \new line '/etc/fstab inserts a new row in front of the third line

sed ' 3a \new line \ Another line '/etc/fstab add two lines after the third row, respectively Newli NE, anotherline

sed ' /^uuid/a \# Add new device base on UUID '/etc/fstab in each UU Add a comment line after the ID line.

Note: A I follow the existence of a backslash in a fixed mode.

C \text: Replace the matched line with the specified text "text";

Example: sed ' 1,3c \test '/etc/fstab


W/path/to/somefile: saves the line of pattern space to the specified file;

Example: Sed-n '/^[^#]/w/tmp/fstab.new '/etc/fstab Save all non-# lines to the Fstab.new file, and on the screen Output only once

R/path/from/somefile: Reads the contents of the specified file to the current file by the pattern to match the line, file merge;

Example: sed ' 3r/etc/issue '/etc/fstab save issue full text to Fstab after the third line


=: Prints the line number for the line to which the pattern matches;

Example: sed '/^uuid/= '/etc/fstab will be a line number on the next line of the lines you need to match your condition.


!: conditional inversion;

Example: sed '/^#/id ' will reverse all lines starting with #

Replace tag:

s///: Find replacement, its delimiter can be self-specified, commonly used have [email protected]@@, s## #等;

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]@ ' the path

Note: a non-diagonal character at the end of a line that appears at least once, followed by one or 0 slashes; Replace these with empty

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

Note: as above, regular expressions are used

            

Advanced editing Commands:

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;


650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "border=" 0 "alt=" image 3.png "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "/>

Example:

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

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

Note: If it is the first line we will not do G operation, if it is the last line we

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

Note: deletion is not the last line

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

Note: not the last line, N; not the last line, D.

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 the "who I moved cheese" blog, please be sure to keep this source http://wscto.blog.51cto.com/11249394/1782819

On the usage 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.