ITPub博客

首页 > > > shell对文件的操作-3

shell对文件的操作-3

作者:longmanma 时间:2019-12-10 13:45:39 0 删除 编辑

2.2.2)命令选项说明

在命令行中使用多个命令 -e

将brown替换为green dog替换为cat

[root@www ~]# sed -e 's/brown/green/;s/dog/cat/' data1

1 the quick green fox jumps over the lazy cat.

2 the quick green fox jumps over the lazy cat.

3 the quick green fox jumps over the lazy cat.

4 the quick green fox jumps over the lazy cat.

5 the quick green fox jumps over the lazy cat.

从文件读取编辑器命令 -f 适用于日常重复执行的场景

1)将命令写入文件

[root@www ~]# vim abc

s/brown/green/

s/dog/cat/

s/fox/elephant/

2)使用-f命令选项调用命令文件

[root@www ~]# sed -f abc data1

1 the quick green elephant jumps over the lazy cat.

2 the quick green elephant jumps over the lazy cat.

3 the quick green elephant jumps over the lazy cat.

4 the quick green elephant jumps over the lazy cat.

5 the quick green elephant jumps over the lazy cat.

抑制内存输出 -n

打印data1文件的第二行到最后一行内容 $最后的意思

[root@www ~]# sed -n '2,$p' data1

2 the quick brown fox jumps over the lazy dog.

3 the quick brown fox jumps over the lazy dog.

4 the quick brown fox jumps over the lazy dog.

5 the quick brown fox jumps over the lazy dog.

使用正则表达式 -r

打印data1中以字符串"3 the"开头的行内容

[root@www ~]# sed -n -r '/^(3 the)/p' data1

3 the quick brown fox jumps over the lazy dog.

从上述的演示中,大家可以看出,数据处理只是在缓存中完成的,并没有实际修改文件内容,如果需要修改文件内容可以直接使用-i命令选项。在这里我需要说明的是-i是一个不可逆的操作,一旦修改,如果想复原就很困难,几乎不可能,所以建议大家在操作的时候可以备份一下源文件。-i命令选项提供了备份功能,比如参数使用-i.bak,那么在修改源文件的同时会先备份一个以.bak结尾的源文件,然后再进行修改操作。

1)查看文件列表,没有发现data1.bak

[root@www ~]# ls

abc apache data1 Dobby file node-v10.14.1 Python-3.7.1 soft1 vimset

2)执行替换命令并修改文件

[root@www ~]# sed -i.bak 's/brown/green/' data1

3)发现文件夹中多了一个data1.bak文件

[root@www ~]# ls

abc data1 Dobby node-v10.14.1 soft1

apache data1.bak file Python-3.7.1 vimset

4)打印比较一下,发现data1已经被修改,data1.bak是源文件的备份。

[root@www ~]# cat data1

1 the quick green fox jumps over the lazy dog.

2 the quick green fox jumps over the lazy dog.

3 the quick green fox jumps over the lazy dog.

4 the quick green fox jumps over the lazy dog.

5 the quick green fox jumps over the lazy dog.

[root@www ~]# cat data1.bak

1 the quick brown fox jumps over the lazy dog.

2 the quick brown fox jumps over the lazy dog.

3 the quick brown fox jumps over the lazy dog.

4 the quick brown fox jumps over the lazy dog.

5 the quick brown fox jumps over the lazy dog.

2.2.3)标志

演示文档

[root@www ~]# cat data2

1 the quick brown fox jumps over the lazy dog . dog

2 the quick brown fox jumps over the lazy dog . dog

3 the quick brown fox jumps over the lazy dog . dog

4 the quick brown fox jumps over the lazy dog . dog

5 the quick brown fox jumps over the lazy dog . dog

数字标志:此标志是一个非零正数,默认情况下,执行替换的时候,如果一行中有多个符合的字符串,如果没有标志位定义,那么只会替换第一个字符串,其他的就被忽略掉了,为了能精确替换,可以使用数字位做定义。

替换一行中的第二处dog为cat

[root@www ~]# sed 's/dog/cat/2' data2

1 the quick brown fox jumps over the lazy dog . cat

2 the quick brown fox jumps over the lazy dog . cat

3 the quick brown fox jumps over the lazy dog . cat

4 the quick brown fox jumps over the lazy dog . cat

5 the quick brown fox jumps over the lazy dog . cat

g标志:将一行中的所有符合的字符串全部执行替换

将data1文件中的所有dog替换为cat

[root@www ~]# sed 's/dog/cat/g' data2

1 the quick brown fox jumps over the lazy cat . cat

2 the quick brown fox jumps over the lazy cat . cat

3 the quick brown fox jumps over the lazy cat . cat

4 the quick brown fox jumps over the lazy cat . cat

5 the quick brown fox jumps over the lazy cat . cat

p标志:打印文本内容,类似于-p命令选项

[root@www ~]# sed '3s/dog/cat/p' data2

1 the quick brown fox jumps over the lazy dog . dog

2 the quick brown fox jumps over the lazy dog . dog

3 the quick brown fox jumps over the lazy cat . dog

3 the quick brown fox jumps over the lazy cat . dog

4 the quick brown fox jumps over the lazy dog . dog

5 the quick brown fox jumps over the lazy dog . dog

w filename标志:将修改的内容存入filename文件中

[root@www ~]# sed '3s/dog/cat/w text' data2

1 the quick brown fox jumps over the lazy dog . dog

2 the quick brown fox jumps over the lazy dog . dog

3 the quick brown fox jumps over the lazy cat . dog

4 the quick brown fox jumps over the lazy dog . dog

5 the quick brown fox jumps over the lazy dog . dog

可以看出,将修改的第三行内容存在了text文件中

[root@www ~]# cat text

3 the quick brown fox jumps over the lazy cat . dog

三、sed小技巧

$= 统计文本有多少行

统计data2有多少行

[root@www ~]# sed -n '$=' data2

5

打印data2内容时加上行号

[root@www ~]# sed '=' data2

1

1 the quick brown fox jumps over the lazy dog . dog

2

2 the quick brown fox jumps over the lazy dog . dog

3

3 the quick brown fox jumps over the lazy dog . dog

4

4 the quick brown fox jumps over the lazy dog . dog

5

5 the quick brown fox jumps over the lazy dog . dog

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2730/viewspace-3170802/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: shell变量介绍
请登录后发表评论 登录
全部评论

注册时间:2019-12-18

  • 博文量
    492
  • 访问量
    11993