sed数据处理工具
sed数据处理工具其实也是一个管道命令
sed 【-nerf】 操作
-n使用安静模式 不在屏幕输出
-e 在命令行模式进行操作
-f 执行文件内的操作
-r 使用扩展正则表达式方法
-i 直接修改文件内容不在屏幕输出
n1,n2 [操作] 选择进行操作的行数
-a 目前行下新增一行
-c 替换某行
-d删除某行
-i 在目前的一行插入
-p 将选择的数据在屏幕输出
-s 替换 可以接正则表达式 20s/old/new/g
例1 删除2-5行
[Awei@localhost ~]$ nl regular_express.txt | sed '2,5d'
1"Open Source" is a good mechanism to develop programs.
6GNU is free air not free beer.
例2 在第三行下加入 abcd
[Awei@localhost ~]$ nl regular_express.txt | sed '3a abcd'
1"Open Source" is a good mechanism to develop programs.
2apple is my favorite food.
3Football game is not use feet only.
abcd
例3 加入多行 要用\ 分割
[Awei@localhost ~]$ nl regular_express.txt | sed '3a abcd\
ddd\
> 123'
1"Open Source" is a good mechanism to develop programs.
2apple is my favorite food.
3Football game is not use feet only.
abcd
ddd
123
例4 替换1-2行为12345
[Awei@localhost ~]$ nl regular_express.txt | sed '1,2c 123456'
123456
例5 将三行开头替换abcd
[Awei@localhost ~]$ nl regular_express.txt | sed '3s/Football/abcd/g'
1"Open Source" is a good mechanism to develop programs.
2apple is my favorite food.
3abcd game is not use feet only.
例6 将#号开头删除
[Awei@localhost ~]$ cat regular_express.txt | sed 's/^#//g'
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
例子7 在文件行尾加一行#123456
[Awei@localhost ~]$ sed -i '$a #123456' regular_express.txt | tail regular_express.txt -n 1
#123456
扩展正则表达式
就是比基础正则表达式多一些符号而已
扩展的egrep的基础使用:
1、?:匹配前面0次或者1次
2、+ :匹配前面1次或者以上
3、| :表示或
4、():需要与|联合使用
需要配合使用egrep