Linux Text Processing Three Musketeers--sed

Source: Internet
Author: User

Sed:
1, stream editor, which processes one line 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 proceed to the downstream, so repeat until the end of the file.
2,sed does not edit the original file content by default, only the data in the pattern space is processed unless you use redirection to store the output.

sed [OPTION] ' address command ' [script] file ...
OPTION
-N: Silent mode: Do not display rows in mode space
-e: Multi-point editing
-f/path/to/script_file: Reading the parameter script from the specified file
-R: Supports the use of extended regular expressions
-I: Modify the original file directly

Address
1, not to address: The full text of the processing
2, single address:
#: The specified row
/pattern/: Each row to which the pattern can be matched
3, address range:
#,#
#,+#
/pat1/,/pat2/
#,/pat1/
4,~: Step in
Odd lines
2~2 even rows

Command
P: Display the contents of the mode space
D: Delete the line matching the pattern space
A \string: Appends a new row after the specified line, with a string that supports appending multiple rows with \ n implementation
I \string: Append a new row before the specified line, string, support to append multiple rows with \ n implementation
C \string: Use string to replace the single or multiline content of a pattern space
W/path/to/somefile: Save the pattern matching line to the specified file
R/path/from/somefile: Reads the contents of the specified file into the pattern space after matching to the line
=: Prints line numbers for lines in pattern space
!: Match row inversion in pattern space

Supports the use of regular expressions
s/pattern/string/modifier: Find replacement, support use of other separators, [email protected]@@,s###
string supports using latter references: \1,\2 or:&

Replace tag:
G: In-line global substitution
I: Ignore character case
P: Show the rows that were successfully replaced
W/path/to/somefile: Save a successful replacement row to a file

Advanced Editing Commands:
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 the matched row to overwrite the pattern space
N: Reads the next line of the matched line to the pattern space
D: Delete rows in the pattern space
D: Delete the contents of the current mode space beginning to \ n (not passed to standard
After the command is discarded, but re-executes the SED on the remaining mode space

sed example
Sed-n ' n;p ' file prints even lines

[email protected] test]# cat File12345678910[[email protected] test]# sed-n ' n;p ' file246810

Sed ' 1! G;h;$!d ' File reverse display

[[Email protected] test]# sed ' 1! G;h;$!d ' file10987654321

Sed ' $! n;$! D ' file prints last two lines

[[Email protected] test]# sed ' $! n;$! D ' file910

Sed ' $!d ' file prints last line

[[Email protected] test]# sed ' $!d ' file10

Sed ' G ' file adds a blank line after each line

650) this.width=650; "title=" sed & #039; g& #039; File.png "alt=" Wkiol1ep0whqlrvnaaazfkxlane808.png "src=" http://s5.51cto.com/wyfs02/M01/85/9B/ Wkiol1ep0whqlrvnaaazfkxlane808.png "/>

Sed ' g ' file deletes all rows

650) this.width=650; "title=" sed & #039; g& #039; File.png "src=" Http://s3.51cto.com/wyfs02/M02/85/9B/wKiom1ep0oXBGFUrAAAOqJr39nM537.png "alt=" Wkiom1ep0oxbgfuraaaoqjr39nm537.png "/>

Sed '/^$/d; G ' file adds a blank line after each line

650) this.width=650; "title=" sed & #039; ^ $d; g& #039; File.png "alt=" Wkiom1ep0bvzot6qaaaawholxf0835.png "src=" http://s5.51cto.com/wyfs02/M01/85/9B/ Wkiom1ep0bvzot6qaaaawholxf0835.png "/>

Sed ' n;d ' file prints odd lines

[[Email protected] test]# sed ' n;d ' file13579[[email protected] test]#

Sed-n ' 1! g;h; $p ' file reverse display

[Email protected] test]# sed-n ' 1! G;h, $p ' file10987654321

This article is from the "Love Firewall" blog, be sure to keep this source http://183530300.blog.51cto.com/894387/1836287

Linux Text Processing Three Musketeers--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.