zoukankan      html  css  js  c++  java
  • 一天一个 Linux 命令(20):sed 命令

    一、简介

    Linux系统里的sed命令是一种强大的文本处理工具。sed(Stream EDitor)是一种流文件编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(Pattern Space),接着用 sed 命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕,接着处理下一行,直到文件末尾。

    文件内容并没有改变,除非使用-i选项。sed 可依照脚本的指令来处理、编辑文本文件。sed 主要用来编辑一个或多个文件,简化对文件的反复操作或者用来编写转换程序等。

    sed 功能同 awk 类似,差别在于sed 简单,对列处理的功能要差一些,awk功能复杂,对列处理的功能比较强大。

    二、格式说明

    sed [OPTION]... {script-only-if-no-other-script} [input-file]...
    
    Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
    
      -n, --quiet, --silent
                     suppress automatic printing of pattern space
      -e script, --expression=script
                     add the script to the commands to be executed
      -f script-file, --file=script-file
                     add the contents of script-file to the commands to be executed
      --follow-symlinks
                     follow symlinks when processing in place
      -i[SUFFIX], --in-place[=SUFFIX]
                     edit files in place (makes backup if SUFFIX supplied)
      -c, --copy
                     use copy instead of rename when shuffling files in -i mode
      -b, --binary
                     does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
                     open files in binary mode (CR+LFs are not treated specially))
      -l N, --line-length=N
                     specify the desired line-wrap length for the `l' command
      --posix
                     disable all GNU extensions.
      -r, --regexp-extended
                     use extended regular expressions in the script.
      -s, --separate
                     consider files as separate rather than as a single continuous
                     long stream.
      -u, --unbuffered
                     load minimal amounts of data from the input files and flush
                     the output buffers more often
      -z, --null-data
                     separate lines by NUL characters
      --help
                     display this help and exit
      --version
                     output version information and exit
    
    If no -e, --expression, -f, or --file option is given, then the first
    non-option argument is taken as the sed script to interpret.  All
    remaining arguments are names of input files; if no input files are
    specified, then the standard input is read.
    

    其中 OPTION 为命令选项,script-only-if-no-other-script 为处理动作,可以由-e指定多个 定,input-file为输入文件,可指定多个。

    三、选项说明

    参数说明:
    -e<script>或--expression=<script> 指定sed动作,以选项中指定的script来处理输入的文本文件,可以由多个-e指定多个动作
    -f<script文件>或--file=<script文件> 以选项中指定的script文件来处理输入的文本文件。直接将sed的动作写在一个文件内,-f filename则可以运行filename 内的sed动作;
    -h或--help 显示帮助。
    -n或--quiet或--silent 仅显示script处理后的结果。sed的一般用法中,所有来自STDIN的数据一般都会被打印到终端上,如果加上-n后,则只有经过sed特殊处理的那一行才会被列出来
    -r或--regexp-extended:sed支持扩展正则表达式(默认是基础正则表达式)
    -i :直接修改读取的文件内容,而不是输出到终端。
    -V或--version 显示版本信息。
    
    动作说明:
    a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
    c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
    d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
    i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
    p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
    s :取代,可以直接进行取代的工作哩!通常这个s的动作可以搭配正规表示法!例如 1,10s/old/new/g 就是啦!
    

    四、命令功能

    sed 主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。

    五、常见用法

    1.数据搜索并处理

    1.1.搜索 test.txt有joshua317关键字的行
    nl test.txt |sed '/joshua317/p'
    
    #results:
    # nl test.txt |sed '/joshua317/p'
         1  hello
         2  joshua317
         2  joshua317
         3  i
         4  love
         5  China
         6  ,
         7  joshua317
         7  joshua317
         8  my
         9  name
        10  yes
    

    如果joshua317找到,除了输出所有行,还会输出匹配行。

    1.2.使用-n的时候将只打印包含模板的行。
    nl test.txt |sed -n '/joshua317/p'
    
    #results:
    # nl test.txt |sed -n '/joshua317/p'
         2  joshua317
         7  joshua317
    
    1.3.显示test.txt 文件内的第 3,6行。
    nl test.txt | sed -n '3,6p'
    
    #results:
    # nl test.txt | sed -n '3,6p'
         3  i
         4  love
         5  China
         6  ,
    
    1.4.数据的查找并删除,删除test.txt所有包含joshua317的行,其他行输出
    nl test.txt | sed '/joshua317/d'
    
    #results:
    # nl test.txt | sed '/joshua317/d'
         1  hello
         3  i
         4  love
         5  China
         6  ,
         8  my
         9  name
        10  yes
    
    1.5.删除匹配的字符串
    # nl test.txt | sed  's/joshua317//g'
         1  hello
         2
         3  i
         4  love
         5  China
         6  ,
         7
         8  my
         9  name
        10  yes
    
    1.6.搜索并替换
    # nl test.txt | sed  's/joshua317/joshua319/g'
         1  hello
         2  joshua319
         3  i
         4  love
         5  China
         6  ,
         7  joshua319
         8  my
         9  name
        10  yes
    

    2.新增行操作

    2.1.在第二行后加上"what's you name?"
    nl test.txt |sed "2a what's you name?"#results:#nl test.txt |sed "2a what's you name?"     1  hello     2  joshua317what's you name?     3  i     4  love     5  China     6  ,     7  joshua317     8  my     9  name    10  yes
    
    2.2.第二行前加上"what's you name?"
    nl test.txt |sed "2i what's you name?"#results:# nl test.txt |sed "2i what's you name?"     1  hellowhat's you name?     2  joshua317     3  i     4  love     5  China     6  ,     7  joshua317     8  my     9  name    10  yes
    
    2.3.在第二行后面加入两行,"what's you name?"与"nice to meet you”
    nl test.txt |sed "2i what's you name?
    nice to meet you"#results:# nl test.txt |sed "2i what's you name?
    nice to meet you"     1  hellowhat's you name?nice to meet you     2  joshua317     3  i     4  love     5  China     6  ,     7  joshua317     8  my     9  name    10  yes
    
    2.4.每一行使用反斜杠来分开,就可以在命令行中将一条命令分开多行输入
    #results:# nl test.txt |sed "2i what's you name?> 
    > nice to meet you > "     1  hellowhat's you name?nice to meet you      2  joshua317     3  i     4  love     5  China     6  ,     7  joshua317     8  my     9  name    10  yes
    

    3.删除行操作

    3.1将 test.txt 的内容列出并且列印行号,同时,请将第 1~3 行删除。
    nl test.txt | sed '1,3d'#results:# nl test.txt | sed '1,3d'     4  love     5  China     6  ,     7  joshua317     8  my     9  name    10  yes
    

    注意:原本应该是要下达 sed -e 才对,当只有一个动作的时候,没有 -e 也行,但是多于一个动作时必须要使用-e选项来指定动作。

    3.2.只要删除第 2 行
    nl test.txt | sed '2d'#results:# nl test.txt | sed '2d'     1  hello     3  i     4  love     5  China     6  ,     7  joshua317     8  my     9  name    10  yes
    
    3.3.要删除第3行到最后一行
    nl test.txt | sed '3,$d'#results:# nl test.txt | sed '3,$d'     1  hello     2  joshua317
    

    4.替换行操作

    4.1将第2-5行的内容替换成为"you are joshua317"。
    nl test.txt | sed '2,5c you are joshua317'#results:# nl test.txt | sed '2,5c you are joshua317'     1  helloyou are joshua317     6  ,     7  joshua317     8  my     9  name    10  yes
    
    4.2.将搜索的内容替换。
    nl test.txt | sed  's/joshua317/joshua319/g'#results:# nl test.txt | sed 's/joshua317/joshua319/g'     1  hello     2  joshua319     3  i     4  love     5  China     6  ,     7  joshua319     8  my     9  name    10  yes
    

    5.多点编辑

    5.1.一条sed命令,删除test.txt第三行到末尾的数据,并把joshua317替换为joshua319
    nl test.txt | sed -e '3,$d' -e 's/joshua317/joshua319/'#results:# nl test.txt | sed -e '3,$d' -e 's/joshua317/joshua319/'     1  hello     2  joshua319
    

    说明:

    -e表示多点编辑,第一个编辑命令删除test.txt第三行到末尾的数据,第二条命令搜索joshua317替换为joshua319。

    6.直接修改文件

    sed 的-i选项可以直接修改文件内容,不必使用管道命令或数据流重导向!不过,由於这个动作会直接修改到原始的文件,所以请你千万不要随便拿系统配置来测试,使用时也要慎重。

    6.1.将 test.txt文件内容"joshua317"换成"joshua319"
    sed -i 's/joshua317/joshua319/g' test.txt #results:# sed -i 's/joshua317/joshua319/g' test.txt [root@service-01 ~]# cat test.txt hellojoshua319iloveChina,joshua319mynameyes
    
    6.2.在 test.txt 文件最后一行加入"welcome to China"。
    sed -i '$a welcome to China' test.txt #results:# sed -i '$a welcome to China' test.txt [root@service-01 ~]# cat test.txt hellojoshua319iloveChina,joshua319mynameyeswelcome to China
    
    6.3.删除test.txt文件中含有"joshua319"的行
    sed -i '/joshua319/d' test.txt #results:# sed -i '/joshua319/d' test.txt [root@service-01 ~]# cat test.txt helloiloveChina,mynameyeswelcome to China
    

     

  • 相关阅读:
    C#生成唯一码方法
    解剖常用软件程序都用什么语言开发
    Unity3D笔记七 GUILayout
    函数的递归
    函数
    函数的参数
    函数的返回值
    函数的定义
    文件处理
    集合
  • 原文地址:https://www.cnblogs.com/joshua317/p/15350638.html
Copyright © 2011-2022 走看看