linux---sed指令

sed -n -e '1,/^exit 0$/!p' $0 > "${dir_tmp}/src.tar.gz" 2>/dev/null
选项和命令的区别
选项是sed之后跟着的-n/-e/-i等,命令是没有-,可能在引号里面使用的p/s/g/等

命令s:替换操作,‘s/This/aaa/’
选项-n/命令p: -n选项和p命令一起使用表示只打印那些发生替换的行,原文件不改变
sed -n ‘s/This/aaa/p’ test.txt

[root@localhost ~]# cat test.txt
MacBook-Pro:tmp maxincai$ cat test.txt
my cat's name is betty
This is your dog
my dog's name is frank
This is your fish
my fish's name is george
This is your goat This
my goat's name is adam

# -n选项+p命令打印出发生了替换的行
[root@localhost ~]# sed -n 's/This/aaa/p' test.txt 
aaa is your dog
aaa is your fish
aaa is your goat This # 这个This没有替换掉

# 源文件并没有改变
[root@localhost ~]# cat test.txt  
MacBook-Pro:tmp maxincai$ cat test.txt
my cat's name is betty
This is your dog
my dog's name is frank
This is your fish
my fish's name is george
This is your goat This
my goat's name is adam

选项-i: 文件中每一行的第一个This替换为this,改变原文件

没有加上i选项,就不会改变原文件的内容
[root@localhost ~]# cat test.txt 
MacBook-Pro:tmp maxincai$ cat test.txt
my cat's name is betty
This is your dog
my dog's name is frank
This is your fish
my fish's name is george
This is your goat This
my goat's name is adam

[root@localhost ~]# sed -i 's/This/this/' test.txt 

[root@localhost ~]# cat test.txt 
MacBook-Pro:tmp maxincai$ cat test.txt
my cat's name is betty
this is your dog
my dog's name is frank
this is your fish
my fish's name is george
this is your goat This # 这个This没有被替换
my goat's name is adam

命令后缀/g:会替换每一行中的所有匹配

root@Kylin:~# cat test.txt
my cat's name is betty
This is your dog
my dog's name is frank
This is your fish
my fish's name is george
This is your goat This
my goat's name is adam

# 此时打印所有的改变的内容
root@Kylin:~# sed 's/This/aaa/g' test.txt 
my cat's name is betty
aaa is your dog
my dog's name is frank
aaa is your fish
my fish's name is george
aaa is your goat aaa # 这个This被替换掉了
my goat's name is adam
root@Kylin:~# 

d:删除操作

[root@localhost ~]# cat test.txt 
my cat's name is betty

this is your this dog

my dog's name is this frank

this is your fish

my fish's name is this george

this is your goat this

my goat's name is this adam

[root@localhost ~]# sed '/^$/d' test.txt 
my cat's name is betty
this is your this dog
my dog's name is this frank
this is your fish
my fish's name is this george
this is your goat this
my goat's name is this adam

# 原文件没有改变
[root@localhost ~]# cat test.txt 
my cat's name is betty

this is your this dog

my dog's name is this frank

this is your fish

my fish's name is this george

this is your goat this

my goat's name is this adam

# 只有加上i选项,才会对原文件进行修改
[root@localhost ~]# sed -i '/^$/d' test.txt 
[root@localhost ~]# cat test.txt 
my cat's name is betty
this is your this dog
my dog's name is this frank
this is your fish
my fish's name is this george
this is your goat this
my goat's name is this adam

Nd:删除第N行内容
sed ‘nd’ test.txt
$d:删除最后一行
sed ' d ′ t e s t . t x t ∗ ∗ 2 , d' test.txt **2, dtest.txt2,d**:删除从第二行到最后一行的内容
n,Nd:删除从n行到N行的内容
/^str/d: 删除以str字符开头的行
sed ‘/^my/d’ test.txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值