Examples of practical uses of SED

Source: Internet
Author: User

Sed:stream Editor text stream editing, SED is a "non-interactive" character stream-oriented editor. Can process multiple files at the same time more than one line of content, you can not change the original file, the entire file into the screen, you can only match to the mode of the content input to the screen. You can also change the original file, but will not return the results on the screen

Sed:stream Editor text stream editing, SED is a "non-interactive" character stream-oriented editor. Can process multiple files at the same time more than one line of content, you can not change the original file, the entire file into the screen, you can only match to the mode of the content input to the screen. You can also change the original file, but no more results will be returned on the screen.

Syntax format for SED commands:

SED command format: sed [option] ' sed command ' filename

SED script format: sed [option]-f ' sed script ' filename

Options for the SED command (option):

-N: Print only rows with pattern matching
-E: SED action edit directly in command line mode, this is the default option
-F: Writes the action of SED in a file and executes the SED action within filename with –f filename
-R: Extended expression support
-I: Modify file contents directly

How SED queries text in a file:

1) Use line number, can be a simple number, or a line number range


X

X is the line number

X, y

Indicates line numbers from X to Y

/pattern

Querying rows that contain patterns

/pattern/pattern

Querying rows that contain two patterns

Pattern/,x

Query for rows containing patterns on a given line number

x,/pattern/

Query matching rows by line number and pattern

x,y!

Query does not contain rows with the specified line number x and Y










2) using regular expressions, extending regular expressions (must be combined with the-r option)

^

The line of the anchor point matches the content of the condition, the usage format "^pattern"

$

The line of the anchor point matches the content of the condition, the usage format "pattern$"

^$

Blank Line

.

Match any single character

*

Match the character close to the front any time (0,1, multiple times)

.*

Match any character of any length

\?

Match the character close to the front 0 or 1 times

\{m,n\}

Matches the preceding character at least m times, up to N times

\{m,\}

Match the characters in front of it at least m times

\{m\}

Exact Match of previous M-Times \{0,n\}:0 to N-Times

\<

Anchor Word first----equivalent to \b, usage format: \<pattern

\>

Anchor point ending, usage format: \>pattern

\<pattern\>

Word Anchor Point

\(\)

Grouping, usage format: \ (pattern\), referencing \1,\2

[]

Matches any single character within the specified range

[^]

Matches any single character outside the specified range

[:d Igit:]

All numbers, equivalentto 0-9, [0-9]---> [[:d igit:]]

[: Lower:]

All lowercase Letters

[: Upper:]

All uppercase letters.

[: Alpha:]

All the letters

[: Alnum:]

Equivalent to 0-9a-za-z

[: Space:]

White space characters

[:p UNCT:]

All punctuation




Copy CodeThe code is as follows:
###### #sed的匹配模式支持正则表达式 #####################
Sed ' 5 q '/etc/passwd# print first 5 lines
Sed-n '/r*t/p '/etc/passwd# print match R has 0 or more lines followed by a T character
Sed-n '/.r.*/p '/etc/passwd# print matches a line with R and R followed by any character
Sed-n '/o*/p '/etc/passwd# print o character repeat any time
Sed-n '/o\{1,\}/p '/etc/passwd# print o Word repeat more than once
Sed-n '/o\{1,3\}/p '/etc/passwd# print o word repeat once to three times above

editing commands for sed (sed command):

P

Print matching lines (shared with-n option)

=

Show file line number

A\

Append new text information after locating line numbers

I\

Inserting new text information after locating line numbers

D

Delete Anchor row

C\

Replace anchored text with new text

W filename

Write text to a file similar to output redirect >

R filename

Read text from another file, similar to input redirect <

S

Replace pattern with substitution mode

Q

Exit or exit immediately after the first pattern match is completed

L

Displays the control that is equivalent to the octal acsii code

{}

Group of commands executed on the anchor line, separated by semicolons

N

Read the next line of text from another file and start processing it from the next command instead of the first command

N

Add the next line in the data flow to create a multiline group for processing

G

Paste pattern 2 into/pattern n/

Y

transferring characters, replacing individual characters


The operation of the file is nothing more than "adding and removing changes", how to use the SED command to achieve the file "adding and deleting", playing the SED is one of the essential to write automation scripts.
SED command print file information (query):

Copy CodeThe code is as follows:
# # # #用sed打印文件的信息的例子的命令 ######
Sed-n '/^#/!p '/etc/vsftpd/vsftpd.conf
Sed-n '/^#/! {/^$/!p} '/etc/vsftpd/vsftpd.conf
Sed-e '/^#/d '-e '/^$/d '/etc/vsftpd/vsftpd.conf
Sed-n ' 1,/adm/p '/etc/passwd
Sed-n '/adm/,6p '/etc/passwd
Sed-n '/adm/,4p '/etc/passwd
Sed-n '/adm/,2p '/etc/passwd
# # #以片是对这些sed命令例子的解释和显示结果




The SED command implements the addition of the contents of the file: (with the-I parameter added to the source file):


Copy CodeThe code is as follows:
# # # #sed命令可以实现的添加 ######
#1) The beginning of the match line is added, added in the peer
#2) to match a character in a row of a line after adding
#3) matches the line end of the line to add characters
#4) lines that match rows are preceded by rows added
#5) lines that match rows are followed by rows added
#6) Add a line at the beginning of the file
[[email protected] ~]# sed-i ' 1 i\sed command start ' myfile
#7) appends a line to the end of the file
[[email protected] ~]# sed-i ' $a \sed command end ' MyFile

The SED command implements the deletion of the contents of the file: (directly delete the source file with the-i parameter):

The delete operation for SED is for the line of the file, if you want to delete a character in the row, replace it (don't worry, replace it later, and replace it with the most commonly used sed)



Focus: The SED command implements the replacement of the contents of the file (replaced by the most actions in the Shell Automation script)


Copy CodeThe code is as follows:
#================ content inside the source file ===============================
[email protected] ~]# cat test
Anonymous_enable=yes
Write_enable=yes
local_umask=022
Xferlog_enable=yes
Connect_from_port_20=yes
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Device= "Eth0"
bootproto= "Static"
Hwaddr= "00:0c:29:90:79:78"
onboot= "Yes"
ipaddr=172.16.22.1
netmask=255.255.0.0
#======================================================================
[Email protected] ~]# sed-i '/device/c\ethernet ' test
#匹配DEVICE的行, replace it with Ethernet.
[Email protected] ~]# sed-i ' s/static/dhcp/' test
#把static替换成dhcp (/,@, #都是前面所说的地址定界符)
[Email protected] ~]# sed-i '/ipaddr/[email protected]\[email protected]@ ' test
#匹配IPADDR的行, replace 22.1 with 10.12 because the. Number has a special meaning. All need to be escaped
[Email protected] ~]# sed-i '/connect/s#yes#no# ' test
#匹配connect的行, replace Yes with no
[Email protected] ~]# sed-i ' s/bin/tom/2g ' test
#把所有匹配到bin的行中第二次及第二次之后出现bin替换成tom
[Email protected] ~]# sed-i ' s/daemon/jerry/2p ' test
#把所有匹配到bin的行中第二次出现的daemon替换成jerry, and in the same line of production as matching rows
[Email protected] ~]# sed-i ' S/ADM/BOSS/2 ' test
#把所有匹配到adm的行中仅仅只是第二次出现的adm替换成boss
[[email protected] ~]# sed-i '/root/{s/bash/nologin/;s/0/1/g} ' test
#匹配root的行, replace bash with Nologin and replace 0 with 1
[Email protected] ~]# sed-i ' s/root/(&)/g ' test
#把root用括号括起来,& to refer to the previously matched characters
[Email protected] ~]# sed-i ' s/bootproto/#BOOTPROTO/' test
#匹配BOOTPROTO替换成 #bootproto, typically used to annotate a line in a configuration file
[Email protected] ~]# sed-i ' s/onboot/#&/' test
#匹配ONBOOT的行的前面添加 #, which in the configuration file also indicates that a line is commented
[Email protected] ~]# sed-i '/onboot/s/#//' test
#匹配ONBOOT的行, replace # with empty, that is, the # number is removed, and is generally used to remove the # comment
#================ the contents of the file displayed after performing the sed command above ====================
[email protected] ~]# cat test
Anonymous_enable=yes
Write_enable=yes
local_umask=022
Xferlog_enable=yes
Connect_from_port_20=no
(root): x:1:1: (Root):/(Root):/bin/nologin
Bin:x:1:1:tom:/tom:/stom/nologin
Daemon:x:2:2:jerry:/sbin:/stom/nologin
Daemon:x:2:2:jerry:/sbin:/stom/nologin
Adm:x:3:4:boss:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Ethernet
#BOOTPROTO = "DHCP"
Hwaddr= "00:0c:29:90:79:78"
onboot= "Yes"
ipaddr=172.16.10.12
netmask=255.255.0.0

SED reference variables: (often used in automated shell scripts)

The first one can change the single quotation mark to double quotation mark when there is no default variable in the SED command.
The second, when there is a default variable in the SED command, the variable that you define needs to be quoted, and the statement inside the SED must be single-quoted

Copy CodeThe code is as follows:
[email protected] ~]# cat >> myfile << EOF
> Hello World
> I am Jie
> How is You
> EOF #先生成一个文件
[email protected] ~]# Cat myfile
Hello World
I am Jie
How is You
[Email protected] ~]# Name=li
#定义一个变量 and assign a value to the variable
[Email protected] ~]# sed-i "s/jie/$name/" myfile
#把匹配jie的字符替换成变量的值
[email protected] ~]# Cat myfile
Hello World
I am Li
How is You
[Email protected] ~]# sed-i "$a $name" myfile
#当sed命令也有默认变量时, syntax errors occur when referencing a variable that you define
Sed:-E expression #1, char 3:extra characters after command
[Email protected] ~]# sed-i ' $a ' $name ' myfile
#在引用自定义的变量时, the SED statement must be quoted in a single citation, and then the custom variable is enclosed in single quotes
[email protected] ~]# Cat myfile
Hello World
I am Li
How is You
Li
[Email protected] ~]#

Other advanced uses of sed:

1) Write the contents of the file that you are working with SED to an exception file

Copy CodeThe code is as follows:
[email protected] ~]# cat test #sed操作的文件中的内容
Ethernet
#BOOTPROTO = "DHCP"
Hwaddr= "00:0c:29:90:79:78"
onboot= "Yes"
ipaddr=172.16.10.12
netmask=255.255.0.0
[Email protected] ~]# sed-i ' s/ipaddr/ip/w ip.txt ' test
#把sed操作的文件内容保存到另外一个文件中, w means save, ip.txt file name
[email protected] ~]# cat Ip.txt #查看新文件的内容
ip=172.16.10.12
[Email protected] ~]#


2) read a file into a file that is being manipulated with SED

Copy CodeThe code is as follows:
[email protected] ~]# Cat myfile #文件内容
Hello World
I am Li
How is You
Li
[email protected] ~]# cat test #将用sed操作的文件的内容
Ethernet
#BOOTPROTO = "DHCP"
Hwaddr= "00:0c:29:90:79:78"
onboot= "Yes"
ipaddr=172.16.10.12
netmask=255.255.0.0
[Email protected] ~]# sed-i '/ethernet/r myfile ' test
#在匹配Ethernet的行, read the contents of another file, and the contents of the read-in file are inserted after the line matching Ethernet
[email protected] ~]# cat test #再次查看用sed命令操作的行
Ethernet
Hello World
I am Li
How is You
Li
#BOOTPROTO = "DHCP"
Hwaddr= "00:0c:29:90:79:78"
onboot= "Yes"
ipaddr=172.16.10.12
netmask=255.255.0.0
[Email protected] ~]#

Classic examples of sed:

Copy CodeThe code is as follows:
# #1), process the following file contents, remove the domain name and sort the count, as processed:
Http://www.baidu.com/index.html
Http://www.baidu.com/1.html
Http://post.baidu.com/index.html
Http://mp3.baidu.com/index.html
Http://www.baidu.com/3.html
Http://post.baidu.com/2.html
The following results are obtained:
The number of occurrences of the domain name
3 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com
[email protected] shell]# Cat File | Sed-e ' s/http:\/\///'-e ' s/\/.*//' | Sort | uniq-c | Sort-rn
3 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com
[[email protected] shell]# awk-f/' {print $} ' file |sort-r|uniq-c|awk ' {print ' \ t ', $ $} '
3 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com
# #2), remove the IP address of the NIC with grep combined with SED
[Email protected] ~]# Ifconfig | Grep-b1 "inet addr" |grep-v "\-\-" |sed-n-E ' n;s/\ (eth[0-9]\). *\n.*addr:\ ([0-9]\{1,3\}\.[ 0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\}\]. */\1 \2/p '

Learning to use SED is the basis for writing automated shell scripts, and SED is also a very useful and important command, one of the text processing tools, the above is my own summary of the SED command simple usage, SED has more advanced usage, but also in the study.

Examples of practical uses of SED

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.