linux学习之grep和sed的简单用法.pptVIP

  • 11
  • 0
  • 约6.02千字
  • 约 50页
  • 2016-08-22 发布于河南
  • 举报
linux学习之grep和sed的简单用法

n/N命令 n N Read/append the next line of input into the pattern space. 打印奇数行: cat -n express | sed -n p;n 打印偶数行: cat -n express | sed -n n;p 思考下面的功能: cat -n express | sed -n p;N cat -n express | sed -n N;p cat -n express | sed -n p;d cat -n express | sed d;n cat -n express | sed n;d cat -n express | sed 1d;n;d 案例 输出匹配行的下一行 sed -n /apple/{n;p} express 区别: sed -n /apple/n;p express sed更多功能 h :拷贝模板块的内容到内存中的缓冲区。 H :追加模板块的内容到内存中的缓冲区 g :获得内存缓冲区的内容并替代当前模板块中的文本。 G :获得内存缓冲区的内容并追加到当前模板块文本的后面 =: 出行号 x :互换模板块中的文本和缓冲区中的文本。 案例 为文件加行号 sed = express | sed N;s/\n/:/ 给非空行加行号 sed /./= express | sed /./N;s/\n/:/ 案例 删除连续重复行 sed $!N;/^\(.*\)\n\1$/!P;D chongfu 案例 用sed实现tac功能 sed -e 1!G;h;$!d express 地址限定--匹配 将sample_one文件中包含‘two’行的‘1’替换为‘2’ $ sed /two/ s/1/2/‘ sample_one 地址限定--限制行 编辑器默认查看输入到流编辑器中的每一行,且默认在输入到流编辑器中的每一行上进行编辑 $ sed 5,6 s/1/2/ sample_one 地址限定—除什么外 要删除除包含单词 two 的行之外的所有行: $ sed /two/ !d sample_one two 1 two 1 two 1 $ 保存输出文件 $ sed /two/ s/1/2/` sample_one sample_two 直接修改原文件:-i sed -i $d express 脚本文件 $ cat sedlist /two/ s/1/2/ /three/ s/1/3/ 注意当调用 -f 选项时,在源文件内或命令行中不使用撇号 $ sed -f sedlist sample_one one 1 two 2 three 3 one 1 two 2 two 2 three 3 $ “-f” 选项 安静模式 -n 实例: $ sed -n -f sedlist sample_one $ $ sed -n -f sedlist sample_one sample_two $ cat sample_two $ -n和-p的配合使用 $ cat sedlist /two/ s/1/2/p /three/ s/1/3/p $ $ sed -n -f sedlist sample_one two 2 three 3 two 2 two 2 three 3 $ Sed功能3: 增加和插入文本 $ sed $a \ This is where we stop \ the test sample_one one 1 two 1 three 1 one 1 two 1 two 1 three 1 This is where we stop the test $ 注意: 美元符号 ($) 表示文本将被添加到文件的末尾。反斜线 (\) 是必需的,它表示将插入一个回车符。 Sed功能3: 增加和插入文本 $ sed 3a \ This is where we stop \ the test sample_one one 1 two 1 three 1 This is where we stop the test one 1 two 1 two 1 three 1 $ Sed功能3: 增加和插入文本 $ sed 3i \ This is where we stop \ the test sample_one one 1 two 1 This is where we stop the test three 1 one 1 two 1 two 1 three 1

文档评论(0)

1亿VIP精品文档

相关文档