Linux command-sed

Source: Internet
Author: User
Tags nameserver

Sed:stream Editor for filtering and transforming text flow editor textbox

SED does not use the-I case, do not modify the source files, only the processing results are printed to the screen, using-I will be directly modified source files, is a dangerous practice, used in scripts to modify the configuration file, beginners do not recommend.

How to use sed:

Sed Range modified Print:

Direct Print File line fourth: Sed-n ' 4p ' path_to_file

Only delete the data from line fourth to the next row (including the next blank line) and print additional data: sed ' 4,/^$/d '/path_to_file

Data from line fourth to the next row is not printed (including the next blank line): Sed-n ' 4,/^$/!p '/path_to_file

Show only the fourth line to the next empty row of data (including the next blank line) do not delete only print these: sed ' 4,/^$/!d '/path_to_file

The first line that is matched by the pattern is started, and the line to the second pattern matches to the end of all rows in the middle:/pattern1/,/pattern2/

Sed parameters:

-N:--quiet,--silent: Silent mode no longer defaults to display the contents of the mode

-r:use extended Regular expressions in the script. Using an extended regular expression

SED replacement:

Sed-r ' s/regexp/replacement/g '/path_to_file:sed-r ' s/[[:space:]]+//g ' file removes whitespace from the beginning of the line

Sed search Merge instance: Cat/etc/inittab |sed-r ' s/^#//g;s/^[[:space:]]+//g;/^$/d ': Remove the # number at the beginning of the file Inittab file, the first white space character, and the blank line

SED modifies or removes a word in the range: Sed-i ' 2,3s/\<nameserver\>//g '/etc/resolv.conf:

Remove 2 to 3 rows of nameserver

sed modifier: g: Global substitution

I: Ignore character case

Sed back to Application:

Sed ' s#\ (pattern\) #\1xx#g ' file

sed other: A \string: Append a new line after the specified line, with the contents "string"

I \string: Adds a new line before the specified line, with the content "Srting"

Linux command-sed

Related Article

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.