Linux text editing the sed of the Three Musketeers

Source: Internet
Author: User
Tags processing text

Sed is one of the Three Musketeers known as Linux text Processing.

SED operates on a line-by-row basis for text, but by default SED does not modify the original file (you can add parameters to modify the original file) because SED creates its own pattern space when processing text, so Before the text is processed, the text is read into its schema space, and then the contents of the line are then manipulated in the pattern space according to the matching criteria. In addition, SED also provides a different spatial pattern (we are temporarily called the Hold space), maintains the space to read the content in the pattern space, and then performs the corresponding operation according to the command. In fact, keeping space is a data broker provided by SED to deal with the text in the pattern space.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/72/E9/wKioL1Xv8R6xsE6gAAG5ceczI3E354.jpg "title=" Sed.png "alt=" Wkiol1xv8r6xse6gaag5ceczi3e354.jpg "/>


Here are the common commands for SED:

sed:sed [OPTION] ... ' sed script ' [input-file].

Common options:

-N: Silent mode, does not output the contents of the mode space to the screen

-e: Multi-point editing enables simultaneous processing of multiple files

-f:/path/from/script_file: Read the edit script from the specified file (only script commands are required in the script, no sed is needed)

Example: [[email protected] tmp]# Cat SS

S/aaa/hello/g


-R: Supports the use of Extended regular expressions (SED supports basic regular expressions by default):

-I: Modify the original file directly

Address delimitation:

1, do not give the address, the full text of each line to deal with

2, single address,

#, indicating that a specific row is specified

/patter/: Each row that can be matched by the secondary pattern

3. Address range:

#,# specify the first line to the first line

#,+n indicates the first line and subsequent lines

/patter1/,/patter/indicates the line ending from the Patter1 match to the beginning of the line to which the Patter2 matches

#,/patter1/indicates all rows from the beginning of the first line to the row that the pattern was matched to.

4, ~: Indicates step forward

Example: An odd line is displayed

2~2 indicates that even rows are displayed

Common editing commands;

D: Delete

P: Shows the rows in the pattern space that are matched by the pattern

A \text:append, append text, support \ n Append line add

I \text:insert, insert text in front, support \ n Insert multiple lines

C \text:change, replacing line pseudo-single or multiline text

W/path/to/filename: Saves matching rows to a specified file

R/path/from/filename: reads the text stream of the specified file to the end of the line that matches to the line

=: Prints line numbers for lines of pattern space

! : Take the inverse condition, example: sed "/^uuid/!d"/etc/fstab

S///G: Supports the use of other separators, [email protected]@@,s###,

G-line represents global substitution

P: Show the rows that were successfully replaced

W/path/to/somefile: Save the result of a successful replacement to a file


Advanced editing Commands: (commands for exchanging data in pattern space and holding space)

H: Overwrite the contents of the pattern space in the hold space

H: Append the contents of the pattern space to the hold space

G: Remove data from hold space to pattern space

G: Append content from hold space to mode space

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

N: Reads the next line of matching rows to the pattern space

N: Append the next line of the row to the pattern hole home

D: Delete rows in the pattern space

D: Delete all rows in multi-line mode space

Cases:

Sed ' s/r. t/&er/' file: & replaces everything that the previous pattern matches to

Example: [[email protected] ~]# sed ' s/^[[:space:]]*//'/boot/grub/grub.conf

Sed-n ' n;p ' file: Show even rows

Sed ' 1! G;h;$!d ' file: Reverse display of the contents of files

Sed ' $! n;$! D ' file two lines after removing files

Sed ' $!d ' takes out the last line of the file

Sed ' G ' file:

Sed '/^$/d; G ' file merges multiple blank lines into a single blank line

Sed ' n;d ' file: Show odd lines

Sed-n ' 1! g;h; $p ' file: Reverse display of each row in a file


Take path name

Sed-r ' [email protected] (/.*/) [^/]+/[email protected]\[email protected] '

Sed-r ' [email protected] (/.*) +[^/]/? (. *) @\[email protected] '

Sed ' [Email protected][^/]\+/\[email protected]@ '

Fetch PATH Base name

Sed-r ' [email protected] (/.*/) ([^/]+/?) @\[email protected] '

Sed-r ' [email protected] (/.*) + ([^/]/?) @\[email protected] '


This article is from the "Seven Stars" blog, please be sure to keep this source http://qikexing.blog.51cto.com/7948843/1693180

Linux text editing the sed of the Three Musketeers

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.