zoukankan      html  css  js  c++  java
  • SED LEARN NOTE

    SED LEARN NOTE

    参考资料

    SED LEARN NOTE

    sed基本格式

    • sed [option] [sed-command] input-file
    • sed对文本的每一行依次执行sed-command,最终返回处理结果
    • 通常sed-command都在单引号中
    #options
    
    -e<script>或--expression=<script>:以选项中的指定的script来处理输入的文本文件;
    -f<script文件>或--file=<script文件>:以选项中指定的script文件来处理输入的文本文件;
    -h或--help:显示帮助;
    -n或--quiet或——silent:仅显示script处理后的结果;
    -V或--version:显示版本信息。
    

    sed -f

    • sed [ options ] –f {sed-commands-in-a-file} {input-file}
    • 将sed-command放在file.sed文件中
    • sed -f file.sed input-file对文本进行多个命令的处理

    sed -e

    • sed [ options ] –e {sed-command-1} –e {sed-command-2} {input-file}
    • 下面code block中是耗子叔在其sed tutorial中的写法
    $ sed '1,3s/my/your/g; 3,$s/This/That/g' my.txt
    This is your cat, your cat's name is betty
    This is your dog, your dog's name is frank
    That is your fish, your fish's name is george
    That is my goat, my goat's name is adam
    
    • sed -e多命令写法
    #第一种
    
    sed -e 'command1' -e 'command2' input-file
    
    #第二种
    
    sed -e		
    `command1`	
    `command2`	
    input-file
    
    #第三种
    
    sed 'command1;command2' input-file
    
    #sed and awk hack 101
    
    sed [options] ‘{
    sed-command-1
    sed-command-2
    }’ input-file
    

    sed打印命令

    sed ‘p’结果

    image-20210227213757491.png

    上述处理方式会打印文本两次

    sed -n ‘p’ result

    -n或--quiet或——silent:仅显示script处理后的结果

    image-20210227214015510.png

    打印指定范围

    • sed -n ‘num p’:只打印第num行
    • sed -n ‘num1,num2 p’:打印num1到num2行
    • sed -n ‘^,num’:打印首行至num行
    • sed -n ‘num,$ p’:打印num行至尾行
    • sed -n ‘num1,+num2 p’:打印第num1行及其后num2行
    • sed -n ‘num1~num2 p’:打印num1行,跳过num2行打印
    #input-file
    
    Lina is a good girl
    Lina will be someones wife
    Lina love anime
    Lina will get a good job
    Lina will live a happy life
    Lina,see you again
    

    打印指定行

    image-20210227220055347.png

    打印指定范围文本

    image-20210227220307387.png

    打印某行及其后几行

    image-20210227220432238.png

    打印奇数行

    image-20210227220550418.png

    打印匹配

    • sed有匹配内容行
    • sed -n ‘/pattern/p’ input-file

    打印匹配字符行

    image.png

    • sed -n ‘/pattern/,num p’ input-file

    打印第一次匹配字符至第num行,如果前面未匹配到字符则从后面找并匹配打印

    打印第一次匹配字符至第num行

    image.png

    • sed -n ‘/pattern1/,/pattern2/p’ input-file

    打印匹配pattern1到匹配pattern2行间的内容

    image.png

    • sed -n ‘/pattern/,+num p’ input-file

    打印匹配到的pattern及其后num行

    image.png

    sed写入文件命令

    写入指定范围内容

    • sed -n ‘w output-file’ input-file:将input-file内容写到output-file中

    image.png

    • sed -n ‘num w output-file’ input-file:将input-file num行内容写到output-file中

    image.png

    • sed -n ‘num1,num2 w output-file’ input-file:将input-file num1行到num2行内容写到output-file中

    image.png

    • sed -n ‘^,num w output-file’ input-file:将input-file 首行到num行内容写到output-file中

    • sed -n ‘num,$ w output-file’ input-file:将input-file num行到尾行内容写到output-file中

    • sed -n ‘num,+num2 w output-file’ input-file:将input-file num1行及其后num2行内容写到output-file中

    image.png

    • sed -n ‘num1~num2 w output-file’ input-file:将input-file num1行及每间隔num2行内容写到output-file中

    image-20210228182637446.png

    匹配后写入

    • sed -n ‘/pattern/ w output-file’ input-file:将匹配到pattern的行写入到output-file中

    image.png

    • sed -n ‘/pattern1/,/pattern2/ w output-file’ input-file:将匹配到pattern1到pattern2的行写入到output-file中

    image.png

    • sed -n ‘/pattern/,num w output-file’ input-file:将匹配到pattern的行到num行写入到output-file中

    image.png

    • sed -n ‘/pattern/,+num w output-file’ input-file:将匹配到pattern的行及其后num行写入到output-file中

    image.png

    sed删除命令

    sed ‘d’结果

    • 将模式空间中的内容全部删除但是不改变源文件

    sed删除范围

    • sed -e ‘num d’ input-file:删除第num行文本

    image.png

    • sed -e ‘num1,num2 d’ input-file:删除num1到num2行的内容

    image.png

    • sed -e ‘^,num d’ input-file:删除行首到num行的内容
    • sed -e ‘num,$ d’ input-file:删除num行到行尾的内容
    • sed -e ‘num1,+num2 d’ input-file:删除num1行及其后的num2行

    image.png

    • sed -e ‘num1~num2 d’ input-file:删除num1行,跳过num2行继续删除

    image.png

    sed匹配后删除

    • sed -e ‘/pattern/ d’ input-file:第一次匹配到pattern后将其删除

    image.png

    • sed -e ‘/pattern/,num d’ input-file:第一次匹配到pattern后删除改行到num行

    image.png

    • sed -e ‘/pattern1/,/pattern2/ d’ input-file:删除第一次匹配到pattern1的行到第一次匹配到pattern2的行

    image.png

    • sed -e ‘/pattern/,+num/ d’ input-file:删除第一次匹配到pattern的行及其后num行

    image.png

    sed替换命令

    sed s command

    • sed ‘[address-range | pattern-range] s/original-pattern/replace-pattern/[replace-flag]’ input-file

    sed替换标志

    • 全局标志g,默认情况值替换第一次匹配的pattern,g标志可以匹配当前行所有匹配的pattern

      #替换所有出现的a
      
      sed -i 's/a/A/g' input-file
      
    • 数字标志,original-sring出现次数触发替换

      #条件触发
      
      sed -i 's/a/A/num' input-file
      
      #第num次出现的a被替换为A
      
    • 打印标志p

      #替换后打印替换的行
      
      sed -n 's/a/A/p' input-file
      
    • 写标志w

      #替换后写入文件
      
      sed -n 's/a/A/w output-file' input-file
      
    • 忽略大小写标志i

      #忽略original-pattern大小写
      
      sed -n 's/a/A/i' input-file
      
    • 执行标志e

      #替换后执行
      
      sed -n 's/^/cat /' input-file
      
      替换后执行cat命令
      
    • pattern中包含/符号

      #pattern中包含/符号
      
      sed -n 's@/path1@/path2@' input-file
      
    • 替换指定行内容

      sed -i 'num s/pattern1/pattern2/' input-file
      

      image.png

    • 替换指定范围内容

      image.png

    • 替换奇数行内容

      image.png

    • 替换包含pattern1的行中的内容

      image.png

    sed正则表达式

    元字符

    ^ # 匹配行开始,如:/^sed/匹配所有以sed开头的行。
    $ # 匹配行结束,如:/sed$/匹配所有以sed结尾的行。
    . # 匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d。
    * # 匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行。
    [] # 匹配一个指定范围内的字符,如/[sS]ed/匹配sed和Sed。  
    [^] # 匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行。
    (..) # 匹配子串,保存匹配的字符,如s/(love)able/1rs,loveable被替换成lovers。
    & # 保存搜索字符用来替换其他字符,如s/love/ **&** /,love这成 **love** 。
    < # 匹配单词的开始,如:/<love/匹配包含以love开头的单词的行。
    > # 匹配单词的结束,如/love>/匹配包含以love结尾的单词的行。
    x{m} # 重复字符x,m次,如:/0{5}/匹配包含5个0的行。
    x{m,} # 重复字符x,至少m次,如:/0{5,}/匹配至少有5个0的行。
    x{m,n} # 重复字符x,至少m次,不多于n次,如:/0{5,10}/匹配5~10个0的行。 
    

    附加命令

    insert命令

    • insert是在行前插入
    • sed -i ‘num i string-wanna-insert’ input-file

    某行前插入文本

    image.png

    匹配到字符行前插入文本

    image.png

    append命令

    在某行后追加文本

    image.png

    匹配到字符行后插入文本

    image.png

    修改命令

    修改某行的文本

    image.png

    修改包含pattern的行

    image.png

  • 相关阅读:
    mysql truncate
    蠕虫复制
    mysql 一对多,多对多
    php实现文件下载
    JetBrains PhpStorm 整个项目中查找一个词语
    vim
    程序员减少代码BUG的7种方法,拒绝编程5分钟,查代码2小时!
    创建你的第一个Composer/Packagist包
    Elasticsearch
    Laravel 实践之路: 数据库迁移与数据填充
  • 原文地址:https://www.cnblogs.com/movit/p/14457134.html
Copyright © 2011-2022 走看看