美文网首页
37 sed (二)

37 sed (二)

作者: StarShift | 来源:发表于2016-10-29 00:27 被阅读32次

N命令

先来看N命令 —— 把下一行的内容纳入当成缓冲区做匹配。
下面的的示例会把原文本中的偶数行纳入奇数行匹配,而s只匹配并替换一次,所以,就成了下面的结果。

假设要匹配的文件:

This is my cat\n  my cat's name is betty
This is my dog\n  my dog's name is frank
This is my fish\n  my fish's name is george
This is my goat\n  my goat's name is adam
$ sed 'N;s/\n/,/' pets.txt
This is my cat,  my cat's name is betty
This is my dog,  my dog's name is frank
This is my fish,  my fish's name is george
This is my goat,  my goat's name is adam

a命令和i命令

a命令就是append, i命令就是insert,它们是用来添加行的。如:

# 其中的1i表明,其要在第1行前插入一行(insert)
$ sed "1 i This is my monkey, my monkey's name is wukong" my.txt
This is my monkey, my monkey's name is wukong
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
$ sed "$ a This is my monkey, my monkey's name is wukong" my.txt
This is my cat, my cat's name is betty
This is my monkey, my monkey's name is wukong
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
$ sed "/fish/a This is my monkey, my monkey's name is wukong" my.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my monkey, my monkey's name is wukong
This is my goat, my goat's name is adam

c命令

c 命令是替换匹配行

$ sed "2 c This is my monkey, my monkey's name is wukong" my.txt
This is my cat, my cat's name is betty
This is my monkey, my monkey's name is wukong
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
 
$ sed "/fish/c This is my monkey, my monkey's name is wukong" my.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my monkey, my monkey's name is wukong
This is my goat, my goat's name is ada

d命令

删除匹配行

$ sed '/fish/d' my.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my goat, my goat's name is adam
 
$ sed '2d' my.txt
This is my cat, my cat's name is betty
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
 
$ sed '2,$d' my.txt
This is my cat, my cat's name is betty

p命令

打印命令

# 匹配fish并输出,可以看到fish的那一行被打了两遍,
# 这是因为sed处理时会把处理的信息输出
$ sed '/fish/p' my.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
 
# 使用n参数就好了
$ sed -n '/fish/p' my.txt
This is my fish, my fish's name is george
 
# 从一个模式到另一个模式
$ sed -n '/dog/,/fish/p' my.txt
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
 
#从第一行打印到匹配fish成功的那一行
$ sed -n '1,/fish/p' my.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george

其它

# 其中的+3表示后面连续3行
$ sed '/dog/,+3s/^/# /g' pets.txt
This is my cat
  my cat's name is betty
# This is my dog
#   my dog's name is frank
# This is my fish
#   my fish's name is george
This is my goat
  my goat's name is adam

命令打包

$ cat pets.txt
This is my cat
  my cat's name is betty
This is my dog
  my dog's name is frank
This is my fish
  my fish's name is george
This is my goat
  my goat's name is adam
 
# 对3行到第6行,执行命令/This/d
$ sed '3,6 {/This/d}' pets.txt
This is my cat
  my cat's name is betty
  my dog's name is frank
  my fish's name is george
This is my goat
  my goat's name is adam
 
# 对3行到第6行,匹配/This/成功后,再匹配/fish/,成功后执行d命令
$ sed '3,6 {/This/{/fish/d}}' pets.txt
This is my cat
  my cat's name is betty
This is my dog
  my dog's name is frank
  my fish's name is george
This is my goat
  my goat's name is adam
 
# 从第一行到最后一行,如果匹配到This,则删除之;如果前面有空格,则去除空格
$ sed '1,${/This/d;s/^ *//g}' pets.txt
my cat's name is betty
my dog's name is frank
my fish's name is george
my goat's name is adam
root@ubuntu:~# sed '1,${/This/d;s/^ *//g}' cats.txt 
my cat's name is betty
my dog's name is frank
my fish's name is george
my goat's name is adam
root@ubuntu:~# sed '1,${/This/d;s/^ //g}' cats.txt 
 my cat's name is betty
 my dog's name is frank
 my fish's name is george
 my goat's name is adam
root@ubuntu:~# sed '1,${/This/d;s/^ *//g}' cats.txt 
my cat's name is betty
my dog's name is frank
my fish's name is george
my goat's name is adam
root@ubuntu:~# sed '1,${/This/d;s/^ .//g}' cats.txt 
my cat's name is betty
my dog's name is frank
my fish's name is george
my goat's name is adam
root@ubuntu:~# sed '1,${/This/d;s/^\s+//g}' cats.txt 
  my cat's name is betty
  my dog's name is frank
  my fish's name is george
  my goat's name is adam
root@ubuntu:~# sed '1,${/This/d;s/^\s//g}' cats.txt 
 my cat's name is betty
 my dog's name is frank
 my fish's name is george
 my goat's name is adam
root@ubuntu:~# sed '1,${/This/d;s/^\s*//g}' cats.txt 
my cat's name is betty
my dog's name is frank
my fish's name is george
my goat's name is adam
root@ubuntu:~# sed '1,${/This/d;s/^\s.//g}' cats.txt 
my cat's name is betty
my dog's name is frank
my fish's name is george
my goat's name is adam

Hold Space

接下来,我们需要了解一下Hold Space的概念,我们先来看四个命令:

  • g: 将hold space中的内容拷贝到pattern space中,原来pattern space里的内容清除
  • G: 将hold space中的内容append到pattern space\n后
  • h: 将pattern space中的内容拷贝到hold space中,原来的hold space里的内容被清除
  • H: 将pattern space中的内容append到hold space\n后
  • x: 交换pattern space和hold space的内容
$ cat t.txt
one
two
three
$ sed '1!G;h;$!d' t.txt
three
two
one

其中的 ‘1!G;h;$!d’ 可拆解为三个命令

  • 1!G —— 只有第一行不执行G命令,将hold space中的内容append回到pattern space
  • h —— 第一行都执行h命令,将pattern space中的内容拷贝到hold space中
  • $!d —— 除了最后一行不执行d命令,其它行都执行d命令,删除当前行
Paste_Image.png

相关文章

  • 37 sed (二)

    N命令 先来看N命令 —— 把下一行的内容纳入当成缓冲区做匹配。下面的的示例会把原文本中的偶数行纳入奇数行匹配,而...

  • Sed(二)

    2018-12-05笔记 Sed附加命令 插入命令 语法为,sed '[address] action the_l...

  • Linux命令-sed

    删除:d命令 $ sed ‘2d’example —–删除example文件的第二行。 $ sed ‘2,$d’e...

  • 2019-01-12三剑客之sed

    一、sed格式 二、sed和正则表达式 三、sed常用命令 d 删除命令 解释第一个:查找文件passwd中含有r...

  • 10-文本处理三剑客之sed

    本章内容 ◆ Sed介绍◆ Sed用法◆ Sed高级用法 处理文本的工具sed sed工具 练习 1、删除cent...

  • sed命令(二)

    -p打印命令(一般和-n选项配合使用) 例如:sed 'p' passwd-----------打印passwd...

  • sed使用

    94 sed(上) sed工具 sed -n '/root/'p 匹配字符串root sed -n '2'p ...

  • Sed and awk 笔记之 sed 篇:高级命令(一)

    sed篇总共分成6章:(简书版) Sed&awk笔记之sed篇:简单介绍 Sed&awk笔记之sed篇:模式空间与...

  • Sed and awk 笔记之 sed 篇:基础命令

    sed篇总共分成6章:(简书版) Sed&awk笔记之sed篇:简单介绍 Sed&awk笔记之sed篇:模式空间与...

  • Sed and awk 笔记之 sed 篇:高级命令(二)

    sed篇总共分成6章:(简书版) Sed&awk笔记之sed篇:简单介绍 Sed&awk笔记之sed篇:模式空间与...

网友评论

      本文标题:37 sed (二)

      本文链接:https://www.haomeiwen.com/subject/weneuttx.html