SED learning Summary of common commands under Linux

Source: Internet
Author: User

    • sed function Description:

SED is a powerful text file processing tool for Linux, which can be used to add, delete, find, query, and match regular expressions to meet the needs of the work. It is also a core skill that an OPS person must master.

------------------------------------------------------------------------------------------------------

    • SED regular Expressions:

^ Match Line start

$ Match Line End

. Any character that matches a non-line break

* Match 0 or more characters

[] matches a character within a specified range

[^] matches a character that is not within the specified range

\(.. \) matches the substring, saves the matching characters, and gives the \ n reference to the replacement command s

& Save Search characters to replace other characters, to replace the & reference of the command s

X\{m\} repeat character x,m times

X\{m,\} repeat character x, at least m times

X\{m,n\} repeat character x, at least m times, no more than n times

------------------------------------------------------------------------------------------------------

    • SED command format:

sed [options] ' Range/Command ' file

------------------------------------------------------------------------------------------------------

Common options:

-n displays only the matched content, usually with the P-parameter configuration.

-e executes multiple commands at the same time, another implementation: ' Command 1; command 2 '

-I executes the command and actually modifies the file, and if you do not use this option, all of the operations of SED simply modify the values in memory.

------------------------------------------------------------------------------------------------------

    • Common range:

1: Match line 1th

1,5: Matches line 1th to line 5th

2,$: Matches line 2nd to last 1 rows

: Match cardinality line, 2 indicates step value

2~2: Number of spouses, 2 indicates step value.

/string/: Matches a row that contains a string

/[a-z]/: Matches a row that contains a range

/^string/: Matching rows containing meta-character sets

/Regular/: Rows matching the contents of the regular expression

------------------------------------------------------------------------------------------------------

    • Common commands:

s replaces the specified content format: '/range/s/replace content/new content/replace range '

A\ Insert text Format below line: ' Range a\ content '

I\ Insert text Format above line: ' Range i\ content '

C\ to change the selected line to a new text format: ' Range c\ content '

d Delete the selected row format: ' Range d '

H copy content to buffer format: ' range h '

G Get buffer contents and append format: ' Range g '

P Print File Content format: ' range/Command/P '

R reads the file contents after the range format: ' Range/R file '

W Extract the contents of the range into file format: ' Range/w file '

! Match the content format to the inverse of the range: ' Range! '

------------------------------------------------------------------------------------------------------

    • Replace command s common parameters:

/delimiter format: '/' If the old value contains '/', you can modify/Be @ or # or |

G Replace range, n denotes the first match format: ' range/s/old value/new value/ng ' No n means replace all

\ n refers to the substring, which indicates the number of substring formats: ' Range/s/* substring */\n/g ' * indicates other content

& Quote ' # ' content format: ' Range/s/#/*&*/g ' #表示匹配内容


------------------------------------------------------------------------------------------------------

Examples of regular expressions:

/^#/matches all lines beginning with #

/sh$/matches all rows ending with sh

/s.d/match S followed by an arbitrary character, D

/s*ed/matches 0 or more rows of S-connected Ed

/[0-9]/matches rows that contain numbers

/[a-z]/! Match lines that do not contain letters

/[^a-z]/matches lines that do not contain full letters

/\ (sh\) ell/#\1#/matching shell replaced by #sh#

/shell/*&*/match Shell replaced with *shell*

/s\{5\}/matches a row with 5 s

/s\{5,\}/matches a row with at least 5 s

/s\{5,10\}/matches the line of 5~10 s

------------------------------------------------------------------------------------------------------

Examples of common options:

Show only the 1th line that matches to: sed-n ' 1p '/etc/passwd

Execute multiple commands simultaneously: sed-n-e ' 1p '-e '/mysql/p '/etc/passwd or sed-n ' 1p;/mysql/p '/etc/passwd

Execute the command and modify the original file: Sed-i ' 2d '/etc/rc.local

------------------------------------------------------------------------------------------------------

Examples of common scopes:

Match line 1th: sed-n ' 1p '/etc/passwd

Match line 1th to 5th: Sed-n ' 1,5p '/etc/passwd

Match line 2nd to last 1 lines: Sed-n ' 2, $p '/etc/passwd

Match odd line: Sed-n ' 1~2p '/etc/passwd

Number of spouses: sed-n ' 2~2p '/etc/passwd

Match the line containing MySQL: sed-n '/mysql/p '/etc/passwd

Match lines starting with a/b/c: Sed-n '/^[a-c]/p '/etc/passwd

Match the line starting with root: Sed-n '/^root/p '/etc/passwd

Match 5 plus at least 2 rows of 0: Sed-n '/50\{2,\}/p '/etc/passwd

------------------------------------------------------------------------------------------------------

Examples of common commands:

Match logon permissions for lines that contain the beginning of MySQL: Sed-n '/^mysql/[email protected]/bin/[email protected]/sbin/[email protected] '/etc/passwd

Match the 1th line of the file and insert ###### before the row: sed ' 1i\###### '/etc/passwd

Match the 1th line of the file and insert ###### after the row: sed ' 1a\###### '/etc/passwd

Match the 2nd line of the file and modify it to ######: sed ' 2c\###### '/etc/rc.local

Match the 2nd line of the file and delete the content: sed ' 2d '/etc/rc.local

Copy the 2nd line of the file and append to the last 1 lines: sed ' 2h; $G '/etc/rc.local

Read RESOLV content appended to local last 1 lines: sed ' $r/etc/resolv.conf '/etc/rc.local

Reads the local 3rd to 5th line to write to Newfile:sed-n ' 3,5w newfile '/etc/rc.local

Match file does not contain empty line contents: Sed-n '/^$/!p '/etc/rc.local

------------------------------------------------------------------------------------------------------

Examples of common parameters for the Replace command s:

Modify the delimiter according to the actual situation: sed ' [email protected]#!/bin/[email protected]#!/bin/[email protected] '/etc/rc.local

Start replacement after 2nd match: Echo Shellshellshell |sed ' s/shell/shell/2g '

Extract substring and replace with reverse reference: Echo Test is ABc or 111 |sed ' s/\ ([a-z]\{3\}\) or \ ([0-9]*\)/*\2* or #\1#/'

Keep the search to the content and reference: Echo this is test file |sed ' s/test/##&##/'

------------------------------------------------------------------------------------------------------

Attention:

When SED handles the replace operation in the script, if the new content is a reference variable, you need to change the single quotation mark to double quotation marks

Example:

name=123456

echo "# # # #6543210 # # # |sed-n" s/[0-9]\+/$name/P "

------------------------------------------------------------------------------------------------------


This article comes from the "Damned Tenderness" blog, so be sure to keep this source http://lsong.blog.51cto.com/2432419/1922121

SED learning Summary of common commands under Linux

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.