Linux Learning Basics 6 SED usage

Source: Internet
Author: User

1 sed, also known as the stream editor, reads rows from the text file into the middle of the pattern space, edits the rows that match the editing criteria, and outputs them to the monitor. The default sed does not edit the original file to handle only the contents of the mode space.

2 sed usage

sed [option] ' addresscommand[modifier] ' file

2.1 option

-N in silent mode, the so-called silent mode is to output only the rows that are matched, not the contents of the default display mode space

-I directly modify the original file

-R using extended regular expressions

-e connect multiple SED commands simultaneously

such as Sed-e ' $a \this is end '-ne '/\<if\>/p '/ETC/RC.D/RC This is the simultaneous use of two sed,

- F followed by a file (the contents of the file are all Addresscommand)

2.2 Address Search scope

1 specifying a single row for exact matching

2 Specify a row to a line such as 3,5 expression 3-5 rows

3 3,+n indicates the third row followed by N rows

4/Regular Expression/indicates the row to match in the regular expression

5/Regular Expression 1/,/Regular expression 2/indicates the first row that is matched to a regular expression 1 and the line between the rows that are matched by the regular expression 2.

2.3 Command Editing commands

D Delete Delete

P printf Printing

A \string a new row underneath the match to the line writes the contents of the string

I \string a new row on the matching line and writes

R file reads the contents of the file into a matching line

W file writes the matching line contents to the file

s/pattern/string/

s /// This form of expression can also be @@@ 或者# # # in this form

\ (\) \1 \2 can also use a reverse reference, and when the reference is global, it can also be referenced directly with & .



2.4 Modifiers

-G global Match table shows all eligible replacements in each row

-I ignores character capitalization

Practice section:

1 Remove The whitespace from the first line of /etc/grup.conf

Sed s/^[[:space:]]///etc/grub.conf

Sed ' 1, $s/^[[:space:]]//'/etc/grub.conf here Notice that There is no space directly behind s /

2 thenumber in "id=3" in the replacement /etc/inittab is 5

Sed ' s/id:3/id:5/'/etc/inittab

Sed ' $s/id:3/id:5/'/etc/inittab

Sed ' $s/^id:3/id:5/'/etc/inittab

3 Delete blank lines in /etc/inittab

Sed '/^#$/d '/etc/inittab

4 Delete the # number at the beginning of the /etc/inittab file

Sed ' s/^#//'/etc/inittab

5 Remove a directory name from a file path

echo "/etc/rc.d/" | Sed-r ' [email protected]^ (/.*/) [^/]+/[email protected]\[email protected] '

decomposition of the above problems

1/etc/rc.d/   search ^/.*/ /etc/rc.d/

2/etc/rc.d/ search [^/]+ etc; + "matches to a character, [] means match a single character [^/] = except / [^/]+ connect ^/.*/ style= font-family:calibri; ">rc.d

when the first part of the greedy mode and the second part of the match to the same character, the second part of the match is from the beginning of the next word to match the direction of the previous word, in the reverse reference when it narrows the matching range It's just the part of/etc// that matches the moment.

3/? in the secondary and ^/.*/ matches the range coincident based on the minimum range matching principle, then the result of the last inverse reference match is

/etc/

7 Count The number of occurrences of each word in the/etc/init.d/functions file and sort

Sed-r ' s/[^[:alpha:]]+/\n/g '/etc/init.d/functions | Sort | Uniq-c


This article is from the "Linux Learning drip" blog, please be sure to keep this source http://2858259639.blog.51cto.com/8111214/1873323

Linux Learning Basics 6 SED usage

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.