Basic usage of the Linux BOC editor sed

Source: Internet
Author: User
Tags line editor

Basic usage of the Linux BOC editor sed

How to deal with SED

Reads the file into memory by default----> reads conforming rows into memory that belongs to SED (the mode space of sed) for editing----> display the contents of the post-processing mode space on the screen

Sed: line editor, which does not edit the source file by default, only handles the display of data in the pattern space

How to use

sed [OPTION] ... {Script-only-if-no-other-script} [Input-file].

sed [OPTION]: ' Linesrangecommand ' File ...

[OPTION]:

-N: Silent mode, no longer displays the contents of the mode space by default

-I: Modify source files directly

-E ' linesrangecommand '-e ' linesrangecommand ': Perform multiple operations simultaneously

-F: Specifies a script file that holds multiple SED commands to perform multiple operations simultaneously

-R: Means using an extended regular expression, using only regular expressions by default


Linesrange: Represents a range, that is, which rows

Usage 1:startline,endline such as 1,100 from line 1th to line 100th

Usage 2:/pattern/For example/^redhat/all rows starting with a line beginning with Redhat

Usage 3:/pattern1/,/pattern2/the first line to be matched by PATTERN1,

To the end of the line that was first matched to the pattern

Line: The specified row

Startline,+n N rows Backward from StartLine


Command: Indicates the commands

D: Delete rows that match the criteria

Sed ' 2, $d '/etc/passwd

P: Show rows that match the criteria

Sed '/home/p '/etc/passwd

A \string: Appends a new line after the specified line, with the contents of string

Sed '/^root/a \i am root '/etc/passwd

I \string: Adds a new row before the specified line, with the contents of string

Sed '/^root/i \i am root '/etc/passwd

R file: After you add the entire contents of the FILE1 file to the FILE2 line that matches the criteria,

For merging files

Sed ' 2r FILE1 ' FILE2

W File: Save eligible rows in the FILE2 file to the FILE1 file

Sed ' 2w FILE1 ' FILE2

s/pattern/string/: Find and Replace,

The default is to replace only the first matched string in each row

G: Global Replacement s/pattern/string/g

I: Ignore character case s/pattern/string/i

Sed ' 1,2s/oot/oot/'/etc/fstab

Back reference \ (string\), \1,\2

Sed ' s#\ (L.. e\) #\1r#g ' Test.txt

&: pattern matching such as sed ' s/l. E/&r/g ' Test.txt

After the string that matches the preceding pattern, add


This article is from the "surgery industry has specialized" blog, please be sure to keep this source http://fuvip.blog.51cto.com/9276123/1980853

Basic usage of the Linux BOC editor 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.