sed高级使用
2015-09-17 20:46
316 查看
1、名reverse脚本用sed编辑器脚本反转数据流的文本行
h 将模式空间复制到保持空间
H 将模式空间附加到保持空间
g 将保持空间复制到模式空间
G 将保持空间附加到模式空间
x 交换模式空间和保持空间的内容
p 打印模式空间
n 提取数据流的下一行
2、加位行间距
3、删除已有空白行
4、给文件中的行编号
5、删除空白行
6、单行next的命令
7、合并文本行
N的命令将下一行合并到那一行,然后用替换命令s将换行符转换成空格
8、删除结尾空白行
data1末尾多两行空白行
9、模式替代
and符号(&)用来代表替换命令中匹配的模式
替换单独的单词
sed编辑器用圆括号来定义替换模式的子字符串,替代字符由反斜线和数字组成,数字表明字符串模块的位置,sed的编辑器给第一个模块分配字符
\1,给第二个模块分配字符\2,依此类推
10、在两个或多个字符串模式中插入文本
本文出自 “散人” 博客,请务必保留此出处http://zouqingyun.blog.51cto.com/782246/1695822
h 将模式空间复制到保持空间
H 将模式空间附加到保持空间
g 将保持空间复制到模式空间
G 将保持空间附加到模式空间
x 交换模式空间和保持空间的内容
p 打印模式空间
n 提取数据流的下一行
[root@logicserver tmp]# vim reverse.sh #!/bin/bash # sed -n '{ 1!G h $p }' $1 ~ [root@logicserver tmp]# cat data1 the quick brown fox jumps over the lazy dog1 a quick brown fox jumps over the lazy dog2 the quick brown fox jumps over the lazy dog3 the quick brown fox jumps over the lazy dog4 the quick brown fox jumps over the lazy dog5 the quick brown fox jumps over the lazy dog6 [root@logicserver tmp]# sh reverse.sh data1 the quick brown fox jumps over the lazy dog6 the quick brown fox jumps over the lazy dog5 the quick brown fox jumps over the lazy dog4 the quick brown fox jumps over the lazy dog3 a quick brown fox jumps over the lazy dog2 the quick brown fox jumps over the lazy dog1
2、加位行间距
[root@logicserver tmp]# sed 'G' data1 the quick brown fox jumps over the lazy dog1 a quick brown fox jumps over the lazy dog2 the quick brown fox jumps over the lazy dog3 the quick brown fox jumps over the lazy dog4 the quick brown fox jumps over the lazy dog5 the quick brown fox jumps over the lazy dog6如果末尾不想这个空白行
[root@logicserver tmp]# sed '$!G' data1 the quick brown fox jumps over the lazy dog1 a quick brown fox jumps over the lazy dog2 the quick brown fox jumps over the lazy dog3 the quick brown fox jumps over the lazy dog4 the quick brown fox jumps over the lazy dog5 the quick brown fox jumps over the lazy dog6
3、删除已有空白行
[root@logicserver tmp]# sed '/^$/d' data1 the quick brown fox jumps over the lazy dog1 a quick brown fox jumps over the lazy dog2 the quick brown fox jumps over the lazy dog3 the quick brown fox jumps over the lazy dog4 the quick brown fox jumps over the lazy dog5 the quick brown fox jumps over the lazy dog6
4、给文件中的行编号
[root@logicserver tmp]# sed '=' data1 1 the quick brown fox jumps over the lazy dog1 2 a quick brown fox jumps over the lazy dog2 3 4 the quick brown fox jumps over the lazy dog3 5 the quick brown fox jumps over the lazy dog4 6 the quick brown fox jumps over the lazy dog5 7 the quick brown fox jumps over the lazy dog6这样可观性不是很好
[root@logicserver tmp]# sed '=' data1 | sed 'N;s/\n/ /' 1 the quick brown fox jumps over the lazy dog1 2 a quick brown fox jumps over the lazy dog2 3 the quick brown fox jumps over the lazy dog3 4 the quick brown fox jumps over the lazy dog4 5 the quick brown fox jumps over the lazy dog5 6 the quick brown fox jumps over the lazy dog6
5、删除空白行
[root@logicserver tmp]# sed '/^$/d' data1 the quick brown fox jumps over the lazy dog1 a quick brown fox jumps over the lazy dog2 the quick brown fox jumps over the lazy dog3 the quick brown fox jumps over the lazy dog4 the quick brown fox jumps over the lazy dog5 the quick brown fox jumps over the lazy dog6
[root@logicserver tmp]#
6、单行next的命令
[root@digitcube-test1 tmp]# cat data1 the quick green fox jumps over the lazy cat1 the quick green fox jumps over the lazy cat2 the quick green fox jumps over the lazy cat3 the quick green fox jumps over the lazy cat4 the quick green fox jumps over the lazy cat5 the quick green fox jumps over the lazy cat6 [root@digitcube-test1 tmp]# sed '/cat2/{n;d}' data1 the quick green fox jumps over the lazy cat1 the quick green fox jumps over the lazy cat2 the quick green fox jumps over the lazy cat4 the quick green fox jumps over the lazy cat5 the quick green fox jumps over the lazy cat6
7、合并文本行
N的命令将下一行合并到那一行,然后用替换命令s将换行符转换成空格
[root@digitcube-test1 tmp]# sed '/cat1/{N;s/\n/ /}' data1 the quick green fox jumps over the lazy cat1 the quick green fox jumps over the lazy cat2 the quick green fox jumps over the lazy cat3 the quick green fox jumps over the lazy cat4 the quick green fox jumps over the lazy cat5 the quick green fox jumps over the lazy cat6
8、删除结尾空白行
data1末尾多两行空白行
[root@digitcube-test1 tmp]# vim sed1.sh #!/bin/bash # sed '{ :start /^\n*$/{$d;N;b start} }' $1 [root@digitcube-test1 tmp]# sh sed1.sh data1 the quick green fox jumps over the lazy cat1 the quick green fox jumps over the lazy cat2 the quick green fox jumps over the lazy cat3 the quick green fox jumps over the lazy cat4 the quick green fox jumps over the lazy cat5 the quick green fox jumps over the lazy cat6
9、模式替代
and符号(&)用来代表替换命令中匹配的模式
[root@logicserver tmp]# echo "The cat sleeps in his hat."|sed 's/.at/"&"/g' The "cat" sleeps in his "hat".当模式匹配了了单词cat,“cat”就出现在替换后的单词里,当它匹配了hat,“hat”就出现在替换后单词里
替换单独的单词
sed编辑器用圆括号来定义替换模式的子字符串,替代字符由反斜线和数字组成,数字表明字符串模块的位置,sed的编辑器给第一个模块分配字符
\1,给第二个模块分配字符\2,依此类推
[root@logicserver tmp]# echo "my lover is pangfeng"|sed ' > s/my \(lover\)/qingyun \1/' qingyun lover is pangfeng
10、在两个或多个字符串模式中插入文本
[root@node3 conf]# echo "hi tom what are you doing"|sed '{ > :start > s/hi \(tom\)/hello \1 miki/ > t start > }' hello tom miki what are you doing
[root@node3 conf]# echo "121234"|sed '{ :start s/\(.*[0-9]\)\([0-9]\{3\}\)/\1.\2/ t start }' 121.234
本文出自 “散人” 博客,请务必保留此出处http://zouqingyun.blog.51cto.com/782246/1695822
相关文章推荐
- 第104讲:通过案例解析Akka中的Actor的不同类型的Constructor学习笔记
- jaxp解析器 使用DOM对象来解析XML
- 安卓的生命周期(初学)
- linux找不到动态链接库 .so文件的解决方法
- Jmeter代理录制脚本
- C学习之指针强化
- Java基础知识强化23:Java中数据类型转换(面试题)
- hdu1874
- HDU1213——Farm Irrigation(并查集)
- BZOJ 1133 [POI2009]Kon DP
- 有意练习--Rails RESTful(一)
- Linux 内核的测试和调试
- JS插件之——bootstrap-suggest.js
- 中国剩余定理
- Python的assert
- ios 爱行华夏 技术支持
- 单向电表模块焊接完成
- android 使用activity自定义下拉选择列表
- Nutch第一次搭建部署总结
- 编写一个程序,检查是否一个给定的数字是一个特殊的号码(定义数组版)