linux sed 正则条件,linux正则表达式--sed(示例代码)

sed是stream editor(流式编辑器)的缩写,它可以对文本流、指定文件集或标准输入进行文本编辑。功能非常强大。

sed命令的基本模式是:sed  [-参数]  ‘命令‘  文本

1. sed两大原则

sed命令总是以单个字母开头。比如[[email protected]]$echo "hello123" | sed ‘s/hello/HELLO/‘  #把hello用HELLO替换HELLO123

上例中s是替换命令,s后面是分割符号,啥都行(一般用‘/’),比如上例子与下面命令等价:[[email protected]]$echo "hello123" | sed ‘s*hello*HELLO*‘HELLO123

sed多数命令允许在前面加个地址。该地址用于指定输入流的哪一行被编辑,如果省略,默认是对所有行都进行编辑。例如

48304ba5e6f9fe08f3fa1abda7d326ab.png[[email protected]]$cat del2

hello1

hello2 hello

hello3

[[email protected]]$cat del2 | sed ‘2s/hello/HELLO/‘        #替换第2行hello1

HELLO2 hello

hello3

[[email protected]]$cat del2 | sed ‘2,3s/hello/HELLO/‘      #替换第2到3行hello1

HELLO2 hello

HELLO3

[[email protected]]$cat del2 | sed ‘s/hello/HELLO/‘         #替换所有行(没有地址,就是默认)HELLO1

HELLO2 hello

HELLO3

48304ba5e6f9fe08f3fa1abda7d326ab.png

2. sed基本编辑命令命令功能简称功能描述

i前插在当前行的前面插入

a后插在当前行的后面插入

d删删除当前行

p查打印当前行。默认是把所有的行都打印出来,并把符合条件的行也打印出来。要是屏蔽默认,加参数-n

s/regex/replacement/换把regex用replacement替换

y/set1/set2换把set1中的字符用对应的set2中的字符替换(必须保证两个集合的字符个数相等)

=输出当前行的行号

q处理完当前行后退出sed

Q直接退出sed

插(i前插,a后插)

48304ba5e6f9fe08f3fa1abda7d326ab.png[[email protected]]$cat del123[[email protected]]$sed ‘2i mm‘ del1mm23[[email protected]]$sed ‘2a mm‘ del12mm3[[email protected]]$sed ‘2,3i mmm‘ del1mmm2mmm3[[email protected]]$sed ‘2,3a mmm‘ del12mmm3mmm

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png[[email protected]]$cat del2

hello1

hello2 hello

hello3

[[email protected]]$sed ‘1d‘ del2                   #删除第一行hello2 hello

hello3

[[email protected]]$sed ‘$d‘ del2                   #删除最后一行hello1

hello2 hello

[[email protected]]$sed ‘2,3d‘ del2                 #删除第2到3行hello1

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

换——字符串替换

48304ba5e6f9fe08f3fa1abda7d326ab.png[[email protected]]$cat del2

hello1

hello2 hello

hello3

[[email protected]]$sed ‘1,2s/hello/NO/‘ del2       #第1到2行hello替换成NO(一行只替换一次)NO1

NO2 hello

hello3

[[email protected]]$sed ‘1,2s/hello/NO/g‘ del2      #地1到2行的hello全部替换成NO(参数g表示替换全部)NO1

NO2 NO

hello3

[[email protected]]$sed ‘1,2c NO‘ del2              #把1到2行用NO替换NO

hello3

[rte[email protected]]$sed ‘s/hello/HELLO/‘ del2       #把所有行的“hello”替换成“HELLO”,等价与sed ‘1,$s/hello/HELLO/‘ del2HELLO1

HELLO2 hello

HELLO3

48304ba5e6f9fe08f3fa1abda7d326ab.png

换——哈希替换

48304ba5e6f9fe08f3fa1abda7d326ab.png[[email protected]]$cat del2 

hello1

hello2 hello

hello3

[[email protected]]$cat del2 | sed ‘y/hel/HEL/‘  #把‘hel’对应的‘HEL’HELLo1

HELLo2 HELLo

HELLo3

48304ba5e6f9fe08f3fa1abda7d326ab.png

其他

48304ba5e6f9fe08f3fa1abda7d326ab.png[[email protected]]$cat del2

hello1

hello2 hello

hello3

[[email protected]]$cat del2 | sed ‘=‘          #输出当前行的行号1hello12hello2 hello3hello3

[[email protected]]$cat del2 | sed ‘q‘          #输出当前行就结束sedhello1

[[email protected]]$cat del2 | sed ‘Q‘          #立马结束sed

48304ba5e6f9fe08f3fa1abda7d326ab.png

3. sed常用参数参数参数含义

-n安静模式,不加-n会把输入流都输出到终端,加上后只输出符合条件的

-e多点

-f-f filename 执行filename文件里的sed命令

-i直接修改读取的文件内容(慎重)

-n

48304ba5e6f9fe08f3fa1abda7d326ab.png[[email protected]]$cat del2 | sed  ‘2p‘hello1

hello2 hello

hello2 hello

hello3

[[email protected]]$cat del2 | sed  -n ‘2p‘            #只输出符号条件的行hello2 hello

48304ba5e6f9fe08f3fa1abda7d326ab.png

-e

48304ba5e6f9fe08f3fa1abda7d326ab.png[[email protected]]$cat del2 | sed  -e ‘2p‘ -e ‘3p‘    #多重命令同时hello1

hello2 hello

hello2 hello

hello3

hello3

[[email protected]]$cat del2 | sed -n  -e ‘2p‘ -e ‘3p‘hello2 hello

hello3

48304ba5e6f9fe08f3fa1abda7d326ab.png

-f

48304ba5e6f9fe08f3fa1abda7d326ab.png[[email protected]]$cat DEL

s/hel/HEL/2,$p

[[email protected]]$sed -f DEL del2                    #按着DEL文件里的awk命令修改HELlo1

HELlo2 hello

HELlo2 hello

HELlo3

HELlo3

[[email protected]]$sed -n -f DEL del2

HELlo2 hello

HELlo3

48304ba5e6f9fe08f3fa1abda7d326ab.png

-i

48304ba5e6f9fe08f3fa1abda7d326ab.png[[email protected]]$cat del2

hello1

hello2 hello

hello3

[[email protected]]$sed -i ‘s/hel/HEL/‘ del2             #直接修改del2[[email protected]]$cat del2

HELlo1

HELlo2 hello

HELlo3

48304ba5e6f9fe08f3fa1abda7d326ab.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值