Linux Learning record-sed

Source: Internet
Author: User

Text Processing Tools

Grep

Sed (stream editor)

awk (report text Generator)



Basic usage of SED

By default, the original file is not edited, only the data in the pattern space is processed.

Sed:stream EDitor


sed [options] ' addresscommand ' file ...

-N Silent mode does not display content in mode space

-I modify the original file

-E script-e script: Multiple footsteps can be specified at the same time

-F: Specify a script file

-r: Indicates the use of extended regular expressions

Address

1.startline,endline

Like 1,100.

2./regexp/

/^root/

3./pattern1/,/pattern2/

First time by Pattern1 match to and Line start, to first be pattern2 match to end

4.LineNumber

The specified row

$ represents the last line

5.startyline,+n

From startline onwards, n rows Backward


Command:

D: Delete rows that match the criteria

Sed ' 1,5d '/etc/fstab

Sed '/oot/d '/etc/fstab

Sed ' 1,+2d '/etc/fstab

Sed '/^\//d '/etc/fstab with ^ anchor header/need add \ Escape

P: Show rows that match the criteria

A \string: Appends a new line after the specified line. Content is string

Sed '/^\//a \ #hello world\n#helo,linux '/etc/fstab

I \string: Appends a new line before the specified line. Content is string

R file: Adds the contents of the specified file to the qualifying line

Sed ' 2r/etc/issue '/etc/fstab

Sed ' $r/etc/issue '/etc/fstab last line added

W File: Saves content in the specified range to the specified file

Sed '/oot/w/tmp/oot.txt '/etc/fstab

s/pattern/string/modifier: Find and Replace, default replaces only the first string in each line that is matched to the pattern

Add modifier

G: Indicates global substitution

I: Ignore character case

Sed ' s/^\//#/'/etc/fstab at the beginning of/replaced by #

Sed ' s/\//#/g '/etc/fstab All/replaced by #

Sed ' [email protected]/@#@g '/etc/fstab/delimiter can be delimited with other characters such as @

$ reference pattern matches to the string

History | Sed ' s#^[[:space:]]*# #g ' | Cut-d '-f1

echo "/ETC/RC.D" | Sed-r ' [email protected]^ (/.*/) [^/]+/[email Protected]\[email protected] ' parent directory of a file


Linux Learning record-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.