sed正则表达式语

//字符匹配
    .       //匹配任意单个字符
    []      //匹配指定范围内的任意单个字符
    [^]     //匹配指定范围外的任意单个字符
//次数匹配
    *       //匹配其前面的任意单个字符任意次
    ?       //匹配其前面的任意单个字符1次或0次
    +       //匹配其前面的任意单个字符至少1次
    {m,n}   //匹配其前面的任意单个字符至少m次,至多n次

//位置锚定
    ^       //锚定行首,此字符后面的任意单个字符必须出现在行首
    $       //锚定行尾,此字符前面的任意单个字符必须出现在行尾
    ^$      //空白行
    \<或\b       //锚定词首,其后面的任意单个字符必须作为单词首部出现
    \>或\b       //锚定词尾,其前面的任意单个字符必须作为单词尾部出现
//分组
    ()      //分组
    \1,\2,\3,....
   例:(ab)*
    //后向引用
        \1      //引用第一个左括号以及与之对应的右括号所包括的所有内容
        \2      //引用第二个左括号以及与之对应的右括号所包括的所有内容
//或者
    |       //or 默认匹配|的整个左侧或者整个右侧的内容
    //例:C|cat表示C或者cat,要想表示Cat或者cat则需要使用分组,如(C|c)at

扩展正则字符注释

//元字符
    .           //任意单个字符
    []          //匹配指定范围内的任意单个字符
    [^]         //匹配指定范围外的任意单个字符
//匹配次数(贪婪模式)
    *           //匹配其前面的任意单个字符任意次
    .*          //任意长度的任意字符
    \?          //匹配其前面的任意单个字符1次或0次
    \+          //匹配其前面的任意单个字符至少1次
    \{m,n\}     //匹配其前面的任意单个字符至少m次,至多n次
//位置锚定
    ^           //锚定行首,此字符后面的任意单个字符必须出现在行首
    $           //锚定行尾,此字符前面的任意单个字符必须出现在行尾
    ^$          //空白行
    \<或\b       //锚定词首,其后面的任意单个字符必须作为单词首部出现
    \>或\b       //锚定词尾,其前面的任意单个字符必须作为单词尾部出现
/分组
    \(\)
    例:\(ab\)*
    //后向引用
        \1      //引用第一个左括号以及与之对应的右括号所包括的所有内容
        \2      //引用第二个左括号以及与之对应的右括号所包括的所有内容

基本正则表达式

S替换命令使用

示例1 [root@node1 ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
//先编辑一个文本

[root@node1 ~]# sed -r "s/ORA (.*)/O'Reilly &/g" 1
.Ah "Major Heading"
O'Reilly ORA Associates, Inc.
//替换Reilly

[root@node1 ~]# sed 's/ORA/& 456/g' 1
.Ah "Major Heading"
ORA 456 Associates, Inc.
//只匹配ORA

[root@node1 ~]# sed 's/ORA.*/& 456/g' 1
.Ah "Major Heading"
ORA Associates, Inc. 456
//匹配整行

示例2
[root@node1 ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.


[root@node1 ~]# sed 's/UNIX/\\s-2&\\s0/g' 1
.Ah "Major Heading"
ORA Associates, Inc.
on the \s-2UNIX\s0 Operating Ststem.
//替换UNIX为 \s-2UNIX\s0

[root@node1 ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.
See Section 1.4
See Section 12.9

[root@node1 ~]# sed 's/See Section [1-9][0-9]*\.[1-9][0-9]*/(&)/g' 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.
(See Section 1.4)
(See Section 12.9)
//添加括号操作


[root@node1 ~]# sed -r 's/(See Section) ([1-9][0-9]*\.[1-9][0-9]*)/\1\\fb\2\\fp/g' 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.
See Section\fb1.4\fp
See Section\fb12.9\fp
//使用\\添加类容

[root@node1 ~]# sed -r 's/(.*):(.*)/\2:\1/g' 1
second:first
tow:noe
//位置交换文本类容

a追加

[root@node1 ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.
See Section 1.4
See Section 12.9
first:second
noe:tow
//文本样式

[root@node1 ~]# sed '1aKFC' 1
.Ah "Major Heading"
KFC
//在第一行后面插入
[root@node1 ~]# sed '1a"  KFC"' 1
.Ah "Major Heading"
"  KFC"
//插入时添加空格

[root@node1 ~]# sed '/^\.Ah/a \   KFC' 1
.Ah "Major Heading"
   KFC
//使用匹配的方式

[root@node1 ~]# sed '/^See/a KFC' 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.
See Section 1.4
KFC
See Section 12.9
KFC
//匹配首行同样字母追加

[root@node1 ~]# sed '/See.*[1-9][0-9]\.[0-9]/axixi' 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.
See Section 1.4
See Section 12.9
xixi
See Section 13.5
xixi
first:second
noe:tow
//匹配行尾不同字符追加类容

i插入

[root@node1 ~]# sed '1iKFC' 1
KFC
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.

//自动会在首行插入

[root@node1 ~]# sed '/^\.Ah/ijiji' 1
jiji
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.
//用匹配的方式插入

c修改

[root@node1 ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
noe:tow
//文本原有类容

[root@node1 ~]# sed '1cjijiyyy' 1
jijiyyy
ORA Associates, Inc.
on the UNIX Operating Ststem.
//修改第一行类容

[root@node1 ~]# sed '/^\.Ah/ckkkyy' 1
kkkyy
ORA Associates, Inc.
on the UNIX Operating Ststem.
//用匹配的方式修改


[root@node1 ~]# sed '/^\.Ah/,/on /cabc' 1
abc
See Section 1.4
See Section 12.9
See Section 13.5
first:second
noe:tow
//用匹配的方式批次修改

d删除

[root@node1 ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
noe:tow

[root@node1 ~]# sed /^noe:tow/d 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
//用匹配的方式删除行尾

[root@node1 ~]# sed /^\.Ah/d 1
ORA Associates, Inc.
on the UNIX Operating Ststem.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
noe:tow
//第一行的.代表单个任意字符 所有要加\转义符

y转换

[root@node1 ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Ststem.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
noe:tow
//文本原有类容

[root@node1 ~]# sed '1y/ajor/KFCJ/' 1
.Ah "MKFCJ HeKding"
ORA Associates, Inc.
on the UNIX Operating Ststem.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
noe:tow
//转换第一行类容 '所选类容必须与修改类容对等‘

p打印

[root@node1 ~]# cat 2
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Append, Insert and Change"
.Ah "list"
//文本原有类容

[root@node1 ~]# sed -n '/Comment/{=;p}' 2
1
.Ah "Comment"
//打印行号 -n 取消默认的打印


[root@node1 ~]# sed '/Comment/{=;p}' 2
1
.Ah "Comment"
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Append, Insert and Change"
.Ah "list"
//默认打印

处理文本类容案例

[root@node1 ~]# cat 2
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Append, Insert and Change"
.Ah "list"
//文本原有类容


[root@node1 ~]# sed '/^\.Ah/{p;s/"//g;s/^\.Ah //}' 2
.Ah "Comment"
Comment
.Ah "Substitution"
Substitution
.Ah "Delete"
Delete
.Ah "Append, Insert and Change"
Append, Insert and Change
.Ah "list"
list

//在行改变之前和之后打印

N追加下一行

 

[root@localhost ~]# cat abc
Consult Section 3.1 in the Owner and Operator 
Guide for a description of the tape drives 
available on your system.
//源文本类容
[root@localhost ~]# sed -n '/Operator$/{N;s/Owner and Operator\nGuide/Installation Guide\n/;p}' abc

Consult Section 3.1 in the Installation Guide
 for a description of the tape drives

[root@localhost ~]# cat abc
Consult Section 3.1 in the Owner and Operator 
Guide for a description of the tape drives 
available on your system.
Look in the Owner and Operator Guide shipped with your system.
Two manuals are provided inc luding the Owner and
Operator Guide and the User Guide.
The Owner and Operator Guide is shipped with your system.

[root@localhost ~]# cat node1 
s/Owner and Operator Guide/ Installation Guide/
/Owner/ {
N
s/ *\n/ /
s/Owner and Operator Guide */Instal lation Guide\
/
}

[root@localhost ~]# sed -f node1 abc
Consult Section 3.1 in the Instal lation Guide
for a description of the tape drives 
available on your system.
Look in the Owner and Operator Guide shipped with your system.
Two manuals are provided inc luding the Owner and
Operator Guide and the User Guide.
The Owner and Operator Guide is shipped with your system.

D删除多行内容

 

[root@localhost ~]# cat ccc
hello world

hello world


hello world



hello world




hello world

[root@localhost ~]# sed '/^$/{N;/^\n$/d}' ccc
hello world

hello world
hello world

hello world
hello world

[root@localhost ~]# sed '/^$/{N;/^\n$/D}' ccc
hello world

hello world

hello world

hello world

hello world


 P多行打印

[root@localhost ~]# cat abc
Here are examples of the UNIX 
System.  Where UNIX
System appears,it should be the UNIX
Operat ing System.
[root@localhost ~]# cat node1
s/Owner and Operator Guide/ Installation Guide/
/Owner/ {
N
s/ *\n/ /
s/Owner and Operator Guide */Instal lation Guide\
/
}

[root@localhost ~]# sed -f node1 abc
Here are examples of the UNIX 
System.  Where UNIX
System appears,it should be the UNIX
Operat ing System.

 

[root@localhost ~]# cat ddd
1
2
11
22
111
222
[root@localhost ~]# sed -n '/1/{h;d};/2/{G;p}' ddd
2
1
22
11
222
111


awk

[root@localhost ~]# cat abc
this line of data is ignored
[root@localhost ~]# awk '{print "Do not die"}' abc
Do not die

[root@localhost ~]# echo '' >> abc
[root@localhost ~]# awk '{print "Do not die"}' abc
Do not die
Do not die

[root@localhost ~]# awk '/^$/ {print "Do not die"}' abc
Do not die
[root@localhost ~]# echo 'a b c d'|awk 'BEGIN{one = 1; two = 2 }{print $(one + two)}'
c
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值