The SED command usage for Shell Scripting Learning notes

Source: Internet
Author: User
Tags first string line editor


The SED command usage for Shell Scripting Learning notes





Basic usage of SED

Sed:stream Editor

Line Editor



SED: Pattern space Stores matched text content in pattern space

By default, the original file is not edited, only the data in the pattern space is processed, and then, when the processing is finished, the contents of the schema mode space are displayed

Sed

-N

-I: Modify the original file directly

-E script-e script: Multiple scripts can be executed at the same time

-F File

Sed-f/scripts File

-r: Indicates the use of extended regular expressions


Sed ' adresscommand ' file ...

-N: Silent mode, which does not display the contents of the mode space by default, only displays content that conforms to the SED command


Address

1. Start line, end line

If 1,100 means 1 to 100 rows


2./pattern/Regular Expressions

3./pattern1/,/pattern2/

The first line that is matched by the pattern1 begins at the end of the line that is first matched to the pattern2, and all the rows in the middle


4.LineNunber

The specified row



5.startine,+n

Starting with startline, the backward n rows:

Command

D: To delete rows that match the criteria

' 1,2d ' delete first, two lines

'/pattern/d ' Delete the line containing the pattern

' 2,+2d ' Delete second and subsequent two lines


P: Show rows that match the criteria

'/^\//p ' shows rows with/As the beginning of the line


A \string: Appends a new line to the specified line, with the content ' string '

As '/^\//a \# Hello World ' adds #hello world behind lines beginning with/Line

\ n: can be used for line wrapping


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


R file: Adds the specified file content to the line where it is added

Sed ' 2r/etc/issue '/etc/fstab inserts a file issue after the second line of Fstab

W File: Save the row in the range specified by the address to the specified file

Sed '/oot/w/tmp/oot.txt '/etc/fstab save rows containing oot in/etc/fstab to file Oot.txt



S/pattern/string: Find and Replace, default replaces only the first string in each line that is matched to the pattern

Sed ' s/oot/oot/'/etc/fstab replace oot in fstab file with Oot

s/^\//#/'


Add modifier

G: Global Substitution

Sed ' s/oot\//#/g '/etc/fstab all/Replace with #

I: Ignore character case

pattern/i,


S///:s###,[email protected]@@

Sed ' [email protected]/@#@g '/etc/fstab @/Replace with #

Can understand this, first will/[email protected], and then will @=#, so it is equal to/=#


\ (\), \1,\2


L.. E

Like--liker

Love--lover

Sed ' s#l. E#&r#g ' Sed.txt


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


Like--like

Love--love


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

& Matching mode applies to the entire


History | Sed ' s#^[[:space]]# #g '

Leave the space in front of the number empty


This article is from the "Endmoon" blog, make sure to keep this source http://endmoon.blog.51cto.com/10533729/1681422

The SED command usage for Shell Scripting Learning notes

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.