ITPub博客

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

shell对文件的操作-2

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

文件内容增加操作,将数据插入到某个位置之前,使用命令i。

演示案例

在data1的每行前插入一行新数据内容: insert data "haha"

[root@www ~]# sed 'i\insert data "haha"' data1

insert data "haha"

1 the quick brown fox jumps over the lazy dog.

insert data "haha"

2 the quick brown fox jumps over the lazy dog.

insert data "haha"

3 the quick brown fox jumps over the lazy dog.

insert data "haha"

4 the quick brown fox jumps over the lazy dog.

insert data "haha"

5 the quick brown fox jumps over the lazy dog.

在第二行前新开一行插入数据: insert data "haha"

[root@www ~]# sed '2i\insert data "haha"' data1

1 the quick brown fox jumps over the lazy dog.

insert data "haha"

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.

在第二到四行每行前新开一行插入数据: insert data "haha"

[root@www ~]# sed '2,4i\insert data "haha"' data1

1 the quick brown fox jumps over the lazy dog.

insert data "haha"

2 the quick brown fox jumps over the lazy dog.

insert data "haha"

3 the quick brown fox jumps over the lazy dog.

insert data "haha"

4 the quick brown fox jumps over the lazy dog.

5 the quick brown fox jumps over the lazy dog.

匹配字符串插入: 找到包含"3 the"的行,在其前新开一行插入内容: insert data "haha"

[root@www ~]# sed '/3 the/i\insert data "haha"' data1

1 the quick brown fox jumps over the lazy dog.

2 the quick brown fox jumps over the lazy dog.

insert data "haha"

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.

文件内容修改操作--替换,将一行中匹配的内容替换为新的数据,使用命令s。

演示案例

从标准输出流中做替换,将test替换为text

[root@www ~]# echo "this is a test" |sed 's/test/text/'

this is a text

将data1中每行的dog替换为cat

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

1 the quick brown fox jumps over the lazy cat.

2 the quick brown fox jumps over the lazy cat.

3 the quick brown fox jumps over the lazy cat.

4 the quick brown fox jumps over the lazy cat.

5 the quick brown fox jumps over the lazy cat.

将data1中第二行的dog替换为cat

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

1 the quick brown fox jumps over the lazy dog.

2 the quick brown fox jumps over the lazy cat.

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.

将data1中第二到第四行的dog替换为cat

[root@www ~]# sed '2,4s/dog/cat/' data1

1 the quick brown fox jumps over the lazy dog.

2 the quick brown fox jumps over the lazy cat.

3 the quick brown fox jumps over the lazy cat.

4 the quick brown fox jumps over the lazy cat.

5 the quick brown fox jumps over the lazy dog.

匹配字符串替换:将包含字符串"3 the"的行中的dog替换为cat

[root@www ~]# sed '/3 the/s/dog/cat/' data1

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 cat.

4 the quick brown fox jumps over the lazy dog.

5 the quick brown fox jumps over the lazy dog.

文件内容修改操作--更改,将一行中匹配的内容替换为新的数据,使用命令c。

演示案例

将data1文件中的所有行的内容更改为: change data "data"

[root@www ~]# sed 'c\change data "haha"' data1

change data "haha"

change data "haha"

change data "haha"

change data "haha"

change data "haha"

将data1文件第二行的内容更改为: change data "haha"

[root@www ~]# sed '2c\change data "haha"' data1

1 the quick brown fox jumps over the lazy dog.

change data "haha"

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.

将data1文件中的第二、三、四行的内容更改为:change data "haha"

[root@www ~]# sed '2,4c\change data "haha"' data1

1 the quick brown fox jumps over the lazy dog.

change data "haha"

5 the quick brown fox jumps over the lazy dog.

将data1文件中包含"3 the"的行内容更改为: change data "haha"

[root@www ~]# sed '/3 the/c\change data "data"' data1

1 the quick brown fox jumps over the lazy dog.

2 the quick brown fox jumps over the lazy dog.

change data "data"

4 the quick brown fox jumps over the lazy dog.

5 the quick brown fox jumps over the lazy dog.

文件内容修改操作--字符转换,将一行中匹配的内容替换为新的数据,使用命令y。

演示案例

将data1中的a b c字符转换为对应的 A B C字符

[root@www ~]# sed 'y/abc/ABC/' data1

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.

文件内容删除,将文件中的指定数据删除,使用命令d。

演示案例

删除文件data1中的所有数据

[root@www ~]# sed 'd' data1

删除文件data1中的第三行数据

[root@www ~]# sed '3d' data1

1 the quick brown fox jumps over the lazy dog.

2 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.

删除文件data1第三到第四行的数据

[root@www ~]# sed '3,4d' data1

1 the quick brown fox jumps over the lazy dog.

2 the quick brown fox jumps over the lazy dog.

5 the quick brown fox jumps over the lazy dog.

删除文件data1中包含字符串"3 the"的行

[root@www ~]# sed '/3 the/d' data1

1 the quick brown fox jumps over the lazy dog.

2 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.

文件内容查看,将文件内容输出到屏幕,使用命令p。

演示案例

打印data1文件内容

[root@www ~]# sed 'p' data1

1 the quick brown fox jumps over the lazy dog.

1 the quick brown fox jumps over the lazy dog.

2 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.

3 the quick brown fox jumps over the lazy dog.

4 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.

5 the quick brown fox jumps over the lazy dog.

打印data1文件第三行的内容

[root@www ~]# sed '3p' data1

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.

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.

打印data1文件第二、三、四行内容

[root@www ~]# sed '2,4p' data1

1 the quick brown fox jumps over the lazy dog.

2 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.

3 the quick brown fox jumps over the lazy dog.

4 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.

打印data1文件包含字符串"3 the"的行

[root@www ~]# sed '/3 the/p' data1

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.

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.

可以看得出,打印内容是重复的行,原因是打印了指定文件内容一次,又将读入缓存的所有数据打印了一次,所以会看到这样的效果,

如果不想看到这样的结果,可以加命令选项-n抑制内存输出即可。

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

请登录后发表评论 登录
全部评论

注册时间:2019-12-18

  • 博文量
    492
  • 访问量
    11996