sed基本用法介绍1

sed常用语对文本流进行处理,以行为单位进行筛选,

格式:   sed  options   script  file

常用参数:

-e    表示多个命令,命令之间用分号分隔

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data
this is a test of the test script
this is the second test of the test script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# sed -e 's/test/trial/;s/this/that/' data
that is a trial of the test script
that is the second trial of the test script
that is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh#

   -f    用于将多个sed命令放在文件当中,可以重复多次使用

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data
this is a test of the test script
this is the second test of the test script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# cat script
s/test/trial/
s/this/that/
root@jiangjian-K42JZ:/home/jiangjian/sh# sed -f script data
that is a trial of the test script
that is the second trial of the test script
that is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh#


-n       不显示未被处理 的文本行,常于p 连用

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data
this is a test of the test script
this is the second test of the test script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# sed -n 's/test/trial/p' data
this is a trial of the test script
this is the second trial of the test script
root@jiangjian-K42JZ:/home/jiangjian/sh#



现在解释sed更多的替换选项
part  1

替换标记,正如前面的s参数指示sed进行替换,可以发现它只替换当行第一个匹配项,这时我们可以在最后的正斜杆后面指示替换的位置和要求

数字:表示心文本替换的模式;

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data
this is a test of the test script
this is the second test of the test script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# sed 's/test/trial/2' data
this is a test of the trial script
this is the second test of the trial script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# clear


g:表示用于新文本替换现有文本的所有实例

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data
this is a test of the test script
this is the second test of the test script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# sed 's/test/trial/g' data
this is a trial of the trial script
this is the second trial of the trial script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# clear


p:表示打印原始行的内容

w:表示替换的结果写入文件

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data
this is a test of the test script
this is the second test of the test script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# sed 's/test/trial/w test' data
this is a trial of the test script
this is the second trial of the test script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# cat test
this is a trial of the test script
this is the second trial of the test script
root@jiangjian-K42JZ:/home/jiangjian/sh#


可能在实际的使用当中会遇到正斜杆,这时可以使用转义的方式来解决,但是sed提供更为简便的方法,就是使用!作为界限符

root@jiangjian-K42JZ:/home/jiangjian/sh# sed -n 's/\/bin\/bash/\/bin\/csh/p' /etc/passwd
root:x:0:0:root:/root:/bin/csh
jiangjian:x:1000:1000:I am the owner :/home/jiangjian:/bin/csh
guest-w2woWH:x:115:125:Guest,,,:/tmp/guest-w2woWH:/bin/csh
fangjuan:x:1001:1001:,,,:/home/fangjuan:/bin/csh
bob:x:1002:1002:,,,:/home/bob:/bin/csh
root@jiangjian-K42JZ:/home/jiangjian/sh# 

使用!做为界限符

root@jiangjian-K42JZ:/home/jiangjian/sh# sed -n 's!/bin/bash!/bin/csh!p' /etc/passwd
root:x:0:0:root:/root:/bin/csh
jiangjian:x:1000:1000:I am the owner :/home/jiangjian:/bin/csh
guest-w2woWH:x:115:125:Guest,,,:/tmp/guest-w2woWH:/bin/csh
fangjuan:x:1001:1001:,,,:/home/fangjuan:/bin/csh
bob:x:1002:1002:,,,:/home/bob:/bin/csh
root@jiangjian-K42JZ:/home/jiangjian/sh#



part2  使用地址

默认情况下sed处理的是整个文件的所有行,当我们在实际使用当中没有必要进行全部的处理,就必须使用地址项来指定所有的处理,这里提供了两种方式:

1  行的数值范围

2  筛选行的文本模式

格式:

[ address]   command

可以将多个命令组合在一起,应用与一个特定的地址

address {

       command1

       command2

       command3

}

详解如下:

数字式行寻址

//单地址指定方式

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data
this is a test of the test script
this is the second test of the test script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# sed '2s/test/trial/' data
this is a test of the test script
this is the second trial of the test script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh#

//多地址指定方式

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data
this is a test of the test script
this is the second test of the test script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# sed '2,3s/this/that/' data
this is a test of the test script
that is the second test of the test script
that is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh#



//从指定行到行尾

root@jiangjian-K42JZ:/home/jiangjian/sh# cat data
this is a test of the test script
this is the second test of the test script
this is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# sed '1,$s/this/that/' data
that is a test of the test script
that is the second test of the test script
that is a different line
root@jiangjian-K42JZ:/home/jiangjian/sh# 


使用文本模式筛选器


命令格式:    /pattern/command

eg

root@jiangjian-K42JZ:/home/jiangjian/sh# sed -n '/root/s/bash/csh/p' /etc/passwd
root:x:0:0:root:/root:/bin/csh
root@jiangjian-K42JZ:/home/jiangjian/sh#



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值