ITPub博客

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

shell对文件的操作

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

shell对文件的操作

简介

sed命令

sed小技巧

一、简介

在shell脚本编写中,时常会用到对文件的相关操作,比如增加内容,修改内容,删除部分内容,查看部分内容等,但是上述举例的这些操作一般都是需要在文本编辑器中才能操作,常用的文本编辑器如:gedit、vim、nano等又是交互式文本编辑器,脚本无法自己独立完成,必须有人参与才可以完成。如果这样的话又违背了我们编写脚本的意愿(全部由机器来完成,减少人的工作压力,提升工作效率)。emm....如何才能让这些操作全部脚本自己就搞定,而不需要人的参与,而且又能按照我们的脚本预案来完成呢?

为了解决上述问题,linux为大家提供了一些命令,比如Perl、sed等命令,今天我就着重为大家介绍一下sed命令。

二、sed命令

sed是linux中提供的一个外部命令,它是一个行(流)编辑器,非交互式的对文件内容进行增删改查的操作,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。它和文本编辑器有本质的区别。

区别是:

文本编辑器: 编辑对象是文件

行编辑器:编辑对象是文件中的行

也就是前者一次处理一个文本,而后者是一次处理一个文本中的一行。这个是我们应该弄清楚且必须牢记的,否者可能无法理解sed的运行原理和使用精髓。

2.1)sed数据处理原理

2.2)sed命令

sed 命令

语法:

sed [options] '{command}[flags]' [filename]

命令选项

-e script 将脚本中指定的命令添加到处理输入时执行的命令中 多条件,一行中要有多个操作

-f script 将文件中指定的命令添加到处理输入时执行的命令中

-n 抑制自动输出

-i 编辑文件内容

-i.bak 修改时同时创建.bak备份文件。

-r 使用扩展的正则表达式

! 取反 (跟在模式条件后与shell有所区别)

sed常用内部命令

a 在匹配后面添加

i 在匹配前面添加

p 打印

d 删除

s 查找替换

c 更改

y 转换 N D P

flags

数字 表示新文本替换的模式

g: 表示用新文本替换现有文本的全部实例

p: 表示打印原始的内容

w filename: 将替换的结果写入文件

2.2.1)sed内部命令说明

演示实例文档

[root@www ~]# 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 dog.

4 the quick brown fox jumps over the lazy dog.

5 the quick brown fox jumps over the lazy dog.

文件内容增加操作,将数据追加到某个位置之后,使用命令a。

演示案例

在data1的每行后追加一行新数据内容: append data "haha"

[root@www ~]# sed 'a\append data "haha"' data1

1 the quick brown fox jumps over the lazy dog.

append data "haha"

2 the quick brown fox jumps over the lazy dog.

append data "haha"

3 the quick brown fox jumps over the lazy dog.

append data "haha"

4 the quick brown fox jumps over the lazy dog.

append data "haha"

5 the quick brown fox jumps over the lazy dog.

append data "haha"

在第二行后新开一行追加数据: append data "haha"

[root@www ~]# sed '2a\append data "haha"' data1

1 the quick brown fox jumps over the lazy dog.

2 the quick brown fox jumps over the lazy dog.

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

在第二到四行每行后新开一行追加数据: append data "haha"

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

1 the quick brown fox jumps over the lazy dog.

2 the quick brown fox jumps over the lazy dog.

append data "haha"

3 the quick brown fox jumps over the lazy dog.

append data "haha"

4 the quick brown fox jumps over the lazy dog.

append data "haha"

5 the quick brown fox jumps over the lazy dog.

匹配字符串追加: 找到包含"3 the"的行,在其后新开一行追加内容: append data "haha"

[root@www ~]# sed '/3 the/a\append data "haha"' 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.

append data "haha"

4 the quick brown fox jumps over the lazy dog.

5 the quick brown fox jumps over the lazy dog.

//开启匹配模式 /要匹配的字符串/

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

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

注册时间:2019-12-18

  • 博文量
    492
  • 访问量
    12002