"Linux learning 011" sed command detailed

Source: Internet
Author: User
Tags processing text line editor

First, Introduction

Sed (Stream editor) is a line editor with three common line editors in Linux: Gred,sed,awk, where awk commands are the most complex, grep commands are the simplest, and SED commands are moderately difficult.

The SED command is capable of receiving a file or pipeline input stream, processing the entire document in a single row, and being able to receive a pipeline inflow is a feature of the SED command.

Details of how to use SED commands online brochure: http://www.gnu.org/software/sed/manual/sed.html

SED most full command format expanded

Sed OPTIONS ... [SCRIPT] [Inputfile ...]

The [SCRIPT] section consists of two parts: the Address Command, which is actually

Sed OPTIONS ... [Address[command]] [Inputfile ...]
Ii. Options

1.-N enable silent mode. The default SED command prints out the contents of the pattern space, and using this option will no longer print.

2.-R to enable extended regular expressions

3.-F Script-file Use the script file to add additional processing text functions

4.-e script adds additional processing text functions using scripts

Third, Address

The role of address is "Select row (Selecting Lines)", which is the selected scope.

1.n

Select a specified row

2.startline,endline

Specify the start and end lines, such as 3, 5, to specify all rows from the third row to the fifth row.

3.startline~step

Specify the start line and step, or both, to select all lines starting from line 2nd until every 3 lines are selected at the end of the file.

4./regexp/

Specifies all rows that conform to a regular expression, which must be escaped with \ If there is/in the expression: \ \

5.$

Last line of selected file

6.startline,+n

From startline onwards, n rows Backward

7.startline,~n

Starting from Startline, n rows forward and backward

Iv. Command

Immediately after the address is command, which is the operation of the selected text

1.D Delete Qualifying rows

2.P displays rows that match the criteria, and the command general and the-N option use the

3.a \string adds a new row after the specified line, which is string and can be wrapped using \ n

4.I \string adds a new line before the specified line, with a string that can be wrapped using \ n

5.s/ regexp / replacement / flags replace the matched string with the specified one, and the delimiter/can be customized if the expression is used/required to be escaped using \.

Flag can contain

G: Global substitution. The default is to replace only the first occurrence of each row

I: Ignore letter case

Five, practice

1. Delete all whitespace characters from the beginning of the/etc/profile file

Sed-r ' s/^[[:space:]]+//g '/etc/profile

Parse: Remove a portion of a line of characters using the Replace function, so use the S command,-R to specify a regular expression that uses the extension.

2. Delete blank lines in the/etc/profile file

Sed-r '/^$/d '/etc/profile

Delete Row use command d,address use regular expression ^$ match, but need to add//

3. Delete the characters in the/etc/profile file that begin with # and have at least one space followed

Sed-r ' s/^#[[:space:]]+//g '/etc/profile

This article is from the "stolen A Plum blog" blog, please be sure to keep this source http://kdyzm.blog.51cto.com/8316029/1740917

"Linux learning 011" sed command detailed

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.