The Linux sed command uses

Source: Internet
Author: User
Tags first string

SED basic usage:

Sed:stream Editor Editor

SED: Mode space

By default, the source file is not edited, only the data in the pattern space is processed, and when the processing is finished, the schema space content is printed out

sed [options] ' addresscommand ' file ...

command editing commands for rows in files in file that conform to address specified addresses range

Options

-N: Silent mode

do not display content in the pattern space, only the matching content is displayed

-I: Modify the original file directly

-E script (script)-E script: Multiple scripts can be executed at the same time each-E can execute a script

-f/path/to/sed_srcipt: Save-e script in a file, read this file by-F execution

sed-f/path/to/sed_srcipt

-R: Represents a regular expression using an extension

Address mode:

1. Startline,endline

Format For example: 1,100, lines 1th through 100th

$: Indicates the last line

2./pattern (regular expression)/

For example:/^root/represents a row that starts with root

3./pattern1/,/pattern2/

Represents: The 1th time the line is pattern1 matched to the beginning of the 1th time by the pattern2 match to the end of the line, this middle of all the rows

4.LineNumber

The specified row

5.startline,+n

Means: Starting at the specified startline line, n rows Backward


Command:

D: Delete rows that match the criteria

$ Sed ' 2d ' example-----Delete the second line of the example file.

*

$ sed ' 2, $d ' example-----Delete the second line of the example file to the end of all lines.

*

$ sed ' $d ' example-----Delete the last line of the example file.

*

$ sed '/test/' d example-----Delete all rows containing test in example file.

For example:

[[Email protected] data]# sed "1,2d"/etc/issue---> To delete lines 1th and 2nd

Mage Education Learning Services

Http://www.magedu.com


[Email protected] data]# cat/etc/issue

CentOS Release 6.6 (Final)

Kernel \ r on an \m


Mage Education Learning Services

Http://www.magedu.com

[[Email protected] data]# sed "1d"/etc/issue---> means delete line 1th

[[Email protected] data]# sed "1,+3d"/etc/issue--To delete line 1th and next 3 lines, total 4 rows

[[Email protected] data]# sed "/^\//d"/etc/issue---> means delete lines with/To start, mode must//clamp up


P: Show rows that match the criteria

For example:

[email protected] data]# cat./test

He like her

She's a good boy

What is you doing

[[email protected] data]# sed/he/p./test---> Display the line containing the He

He like her

He like her----> The rows matching the pattern are displayed 2 times from the execution result because of the default meaning of the SED command

She is a good the boy will be displayed after the command line in the pattern space is processed, the line displayed in the pattern space

She's a good boy also exists, sed command mode is the display mode space, so can match to the display 2 times, if you do not want to

What is you doing display mode space, can be with-N, show silent mode, do not display mode space

[[email protected] data]# sed-n/he/p./test---> Silent mode to show only qualifying rows

He like her

She's a good boy

C: Replace, C can be followed by a string, these strings can replace the line between N1,N2

For example:

[email protected] ruby]# Cat AB

Hello!

Ruby is me,welcome to my blog.

End

[Email protected] data]# sed ' 1,2c hi './linux

Hi

End

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

For example:

[Email protected] data]# sed "/he/a \welcom to Linux World"./test-Append a row after the line with he

He like her

Welcom to Linux World

She's a good boy

Welcom to Linux World

What is you doing

[[Email protected] data]# sed "/he/a \welcom to Linux world\nthe"./test---> Append two lines,

Add "\ n" between characters for line breaks


I \string: Appends a new line to the specified line, with the contents of string

For example:

[Email protected] data]# sed "/he/i \welcom to Linux World"./test

Welcom to Linux World

He like her

Welcom to Linux World

She's a good boy

What is you doing

R file (file path): Adds the contents of the specified file to the line where the condition is met

For example:

[[Email protected] data]# sed ' 2r./first.sh './test---> There must be a single quote here, and double quotes will be an error.

He like her

She's a good boy

#!/bin/bash

u= ' wc-l/etc/rc.d/rc.sysinit |cut-d '-f1 '

G= ' wc-l/etc/rc.d/init.d/functions |cut-d '-f1 '

sum=$[$U + $G]

Echo $sum

What is you doing

[[Email protected] data]# sed ' 1,2r/first.sh './test---> Added after lines 1th and 2nd


W file (file path): The specified range of content exists in the specified file

For example:

[[Email protected] data]# sed '/he/w/xx.txt './test-Save the qualifying row in the./test file to./xx.txt

He like her file, prior xx.txt can not exist, if executed multiple times

She's a good boy saved in Xx.txt is overwritten append, which is also shown in the screen

What is on you doing because, working in pattern space instead of silent mode

[email protected] data]# cat./xx.txt

He like her

She's a good boy

s/pattern/string/modifier: Replaces the pattern in each row with a string, by default only replaces the first string in each line that is matched to the pattern

or [e-mail protected]@@ 修饰 or s## #修饰符都行, string is literal, cannot be regular expression

If you want to replace all you need to add modifiers

G: Global Substitution

I: Ignore case when finding

&: Reference pattern matches entire string

For example:

[[Email protected] data]# sed ' s/o/t/'./test---> Replace the 1th "O" in each row found with T

He like her

She's a gtod boy

What is Ytu doing

[[Email protected] data]# sed ' s/o/@/g '/test-plus g modifier, replace all

He like her

She is a [email protected]@d [email protected]

What is [email protected] [email protected]

[Email protected] data]# sed ' [email protected]\ (L.. e\) @\[email protected] './test

He liker her

She's a good boy

What is you doing

[[Email protected] data]# sed ' [email protected]@&[email protected] './test---> Here @ Represents the entire pattern before L. E,\1 and & have different conveniences in different places

He liker her

She's a good boy

What is you doing


This article is from the "Little Color ruffian" blog, please be sure to keep this source http://501steve.blog.51cto.com/9608615/1577200

The Linux sed command uses

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.