Summary of common usage of Linux sed

Source: Internet
Author: User
Tags modifier

Sed is the more commonly used text editing Class command in Linux, with the rule progressive output

Its more commonly used usage is as follows:


sed [OPTION] {SCRIPT} FILENAME


Main [OPTION]:

-N: No longer displays content in the default mode space

-I: Modifying source files

-E: Perform multiple operations at the same time

For example: sed-e {script}-e {script} ...

-F: Specifies that the script file handles the text content

Example: Sed-f scriptname FILENAME

-R: Allow extended regular expressions (same as GREP-E or egrep)

{SCRIPT} can be split into two parts, {Addresscommand}

Address: For addressing part: need to find the line that meets the requirements or can match

There are several ways of expressing it (note: p In the example is the command mentioned above):

1. Exact matching line (Startline,endline):

Example: 5 The fifth line sed ' 5p ' filename

2. Range matching Row (linenumber): where $ represents the last line

Example: 3rd to 6th line sed ' 3,6p ' filename

3, starting from a line, how many lines backward (startline,+n):

For example: 10 rows backward from 3rd sed ' 3,+10p ' filename

4. Pattern matching (/pattern/): line matching to pattern

For example: matches a line that starts with the root character and is a single word

Sed '/^root\>/p ' filename

5. Pattern range Matching (/pattern1/,/pattern2/): The first line that is matched by pattern1 to the end of the line to which the first match is pattern2, and all rows between

For example: Match a line beginning with use1 character to all rows before a line beginning with User10

Sed '/^user1\>/,/^user10\>p ' filename

Command: Editing commands that need to be executed

-D: Delete rows to match

-P: Display matching rows

-I \string: inserting text information on a line that matches to a row

-A \string: Inserts text information into the row below the line

-R FileName: Adds the contents of the specified file to the line where it matches

-W FileName: Saves the matching line information to the specified file

-s/pattern/string/modifier/: Replaces the matched character with a string, which defaults to the first match to the character in a row

Modifier: G Global substitution

I ignore case

For example: Match root characters are all replaced by User1

Sed ' s/root/user1/g ' filename

where separators///may be substituted with other symbols: e.g. @ etc.

For example:

Sed ' [email protected]@[email protected] ' filename

You can also use the Group Reference feature in this mode:

For example: will all match to the u ... The r character of U is changed to uppercase

Sed ' s/u\ (.. r\)/u\1/g ' filename


Summary of common usage of Linux 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.