• shell编程基础(七): 处理文件命令sed与awk


    一、sed(以行为单位处理文件)

      sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用非常普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑命令转换为另一种格式输出。sed和vi都源于早期UNIX的ed工具,所以很多sed命令和vi的末行命令是相同的。

    0、sed命令格式

    sed命令行的基本格式为:

    sed option 'script' file1 file2 ...
    sed option -f scriptfile file1 file2 ...

    选项含义:

    --version              显示sed版本。
    --help                 显示帮助文档。
    -n,--quiet,--silent    静默输出,默认情况下,sed程序在所有的脚本指令执行完毕后,将自动打印模式空间中的内容,这些选项可以屏蔽自动打印。
    -e script              允许多个脚本指令被执行。
    -f script-file, 
    --file=script-file     从文件中读取脚本指令,对编写自动脚本程序来说很棒!
    -i,--in-place          直接修改源文件,经过脚本指令处理后的内容将被输出至源文件(源文件被修改)慎用!
    -l N, --line-length=N   该选项指定l指令可以输出的行长度,l指令用于输出非打印字符。
    --posix                禁用GNU sed扩展功能。
    -r, --regexp-extended   在脚本指令中使用扩展正则表达式
    -s, --separate         默认情况下,sed将把命令行指定的多个文件名作为一个长的连续的输入流。而GNU sed则允许把他们当作单独的文件,这样如正则表达式则不进行跨文件匹配。
    -u, --unbuffered       最低限度的缓存输入与输出。

    以上仅是sed程序本身的选项功能说明,至于具体的脚本指令(即对文件内容做的操作)后面我们会详细描述,这里就简单介绍几个脚本指令操作作为sed程序的例子。

    a,append        追加
    i,insert        插入
    d,delete        删除
    s,substitution  替换

    如:在输出testfile内容的第二行后添加"mmzs":

    [root@VM_0_5_centos test]# cat testfile 
    mmzs
    mmzsblog
    mmzsit
    [root@VM_0_5_centos test]#  sed "2a mmzs" ./testfile
    mmzs
    mmzsblog
    mmzs
    mmzsit
    //删除2-5行后,输出删除后的结果
    [root@VM_0_5_centos test]# sed "2,5d" testfile
    mmzs

    sed处理的文件既可以由标准输入重定向得到,也可以当命令行参数传入,命令行参数可以一次传入多个文件,sed会依次处理。

    sed的编辑命令可以直接当命令行参数传入,也可以写成一个脚本文件然后用-f参数指定,编辑命令的格式为:

    /pattern/action
    注:其中pattern是正则表达式,action是编辑操作

    sed程序一行一行读出待处理文件,如果某一行与pattern匹配,则执行相应的action,如果一条命令没有pattern而只有action,这个action将作用于待处理文件的每一行。

    1、常用的sed命令

    /pattern/p  打印匹配pattern的行
    /pattern/d 删除匹配pattern的行
    /pattern/s/pattern1/pattern2/ 查找符合pattern的行,将该行第一个匹配pattern1的字符串替换为pattern2
    /pattern/s/pattern1/pattern2/g 查找符合pattern的行,将该行所有匹配pattern1的字符串替换为pattern2

    使用p命令需要注意,sed是把待处理文件的内容连同处理结果一起输出到标准输出的,因此p命令表示除了把文件内容打印出来之外还额外打印一遍匹配pattern的行。例如:

    一个文件testfile的内容是:
    [root@VM_0_5_centos test]# cat testfile 
    mmzs
    mmzsblog
    mmzsit
    abc
    123
    打印其中包含abc的行
    [root@VM_0_5_centos test]# sed '/abc/p' testfile
    mmzs
    mmzsblog
    mmzsit
    abc
    abc
    123
    要想只输出处理结果,应加上-n选项,这种用法相当于grep命令
    [root@VM_0_5_centos test]# sed -n '/abc/p' testfile
    abc
    使用d命令就不需要-n参数了,比如删除含有abc的行
    [root@VM_0_5_centos test]# sed '/abc/d' testfile
    mmzs
    mmzsblog
    mmzsit
    123

    注意:sed命令不会修改原文件,删除命令只表示某些行不打印输出,而不是从原文件中删去。

    使用查找替换命令时,可以把匹配pattern1的字符串复制到pattern2中,例如:

    [root@VM_0_5_centos test]# sed 's/bc/-&-/' testfile
    mmzs
    mmzsblog
    mmzsit
    a-bc-
    123
    pattern2中的&表示原文件的当前行中与pattern1相匹配的字符串
    再比如:
    [root@VM_0_5_centos test]# sed 's/([a-z])([a-z])/-1-~2~/' testfile
    -m-~m~zs
    -m-~m~zsblog
    -m-~m~zsit
    -a-~b~c
    123
    pattern2中的1表示与pattern1的第一个()括号相匹配的内容,2表示与pattern1的第二个()括号相匹配的内容。

    注意:sed默认使用Basic正则表达式规范,如果指定了-r选项则使用Extended规范,那么()括号就不必转义了。

    [root@VM_0_5_centos test]#  sed  's/yes/no/;s/mm/MM/'  ./testfile
    注:使用分号隔开指令。
        
    [root@VM_0_5_centos test]# sed -e 's/yes/no/' -e 's/mm/MM/' testfile
    注:使用-e选项。

    2、练习:

    如果testfile的内容是:

    <html><head><title>Hello World</title></head>
    <body>Welcome to the world of mmzs!</body></html>
    

    现在要去掉所有的HTML标签,使输出结果为:

    Hello World
    Welcome to the world of mmzs!
    

    怎么做呢?如果用下面的命令:

    [root@VM_0_5_centos test]# sed 's/<.*>//g' testfile
    

    结果是两个空行,把所有字符都过滤掉了。这是因为,正则表达式中的数量限定符会匹配尽可能长的字符串,这称为贪心的(Greedy)。比如sed在处理第一行时,<.*>匹配的并不是或这样的标签,而是:

    <html><head><title>Hello World</title>
    

    这样一整行,因为这一行开头是<,中间是若干个任意字符,末尾是>。那么这条命令怎么改才对呢?答案如下:

    错误:得到两个空行,匹配过多
    [root@VM_0_5_centos test]# sed 's/<.*>//g' testfile
    
    
    限定了匹配的行
    [root@VM_0_5_centos test]# sed '/Hello World/s/<[/ a-z]*>//g' testfile 
    Hello World
    <body>Welcome to the world of mmzs!</body></html>
    不完美示例,因为{4,5}导致可扩展性差
    [root@VM_0_5_centos test]# sed -r 's/<(/)?[a-z]{4,5}>//g' testfile 
    Hello World
    Welcome to the world of mmzs!
    正确示例0:
    [root@VM_0_5_centos test]# sed 's/<[/ a-z]*>//g' testfile 
    Hello World
    Welcome to the world of mmzs!
    正确示例1:
    [root@VM_0_5_centos test]# sed 's/<[^>]*>//g' testfile 
    Hello World
    Welcome to the world of mmzs!

    注:上面代码中的红色部分表示pattern1部分的匹配规则,此处的/也不表示转义,是语法中的(有点类似分隔符的意思)

    二、awk(以列为单位处理文件)

      sed以行为单位处理文件,awk比sed强的地方在于不仅能以行为单位还能以列为单位处理文件。awk缺省的行分隔符是换行,缺省的列分隔符是连续的空格和Tab,但是行分隔符和列分隔符都可以自定义,比如/etc/passwd文件的每一行有若干个字段,字段之间以:分隔,就可以重新定义awk的列分隔符为:并以列为单位处理这个文件。awk实际上是一门很复杂的脚本语言,还有像C语言一样的分支和循环结构,但是基本用法和sed类似。

    0、awk命令格式

    awk命令行的基本形式为:

    awk option 'script' file1 file2 ...
    awk option -f scriptfile file1 file2 ...

    和sed一样,awk处理的文件既可以由标准输入重定向得到,也可以当命令行参数传入,编辑命令可以直接当命令行参数传入,也可以用-f参数指定一个脚本文件,编辑命令的格式为:

    /pattern/{actions}
    condition{actions}
    和sed类似,pattern是正则表达式,actions是一系列操作

    解释:awk程序一行一行读出待处理文件,如果某一行与pattern匹配,或者满足condition条件,则执行相应的actions,如果一条awk命令只有actions部分,则actions作用于待处理文件的每一行。

    1、常用命令

    比如文件testfile的内容表示某商店(产品-价格-销量):

    [root@VM_0_5_centos test]# cat testfile 
    ProductA  30 13
    ProductB  76 46
    ProductC  55 32

    打印每一行的第二列:

    [root@VM_0_5_centos test]# awk '{print $2;}' testfile
    30
    76
    55

    自动变量$1、$2分别表示第一列、第二列等,类似于Shell脚本的位置参数,而$0表示整个当前行。再比如,如果某种产品的库存量低于75则在行末标注需要订货:

    [root@VM_0_5_centos test]# awk '$2<75 {printf "%s	%s
    ", $0, "REORDER";} $2>=75 {print $0;}' testfile
    ProductA  30 13    REORDER
    ProductB  76 46
    ProductC  55 32    REORDER

    可见awk也有和C语言非常相似的printf函数。awk命令的condition部分还可以是两个特殊的condition-BEGIN和END,对于每个待处理文件,BEGIN后面的actions在处理整个文件之前执行一次,END后面的actions在整个文件处理完之后执行一次。

    awk命令可以像C语言一样使用变量(但不需要定义变量),比如统计一个文件中的空行数:

    [root@VM_0_5_centos test]# cat testfile 
    ProductA  30 13
    ProductB  76 46
    
    
    ProductC  55 32
    [root@VM_0_5_centos test]# awk '/^ *$/ {x=x+1;} END {print x;}' testfile
    2

    就像Shell的环境变量一样,有些awk变量是预定义的有特殊含义的:

    awk常用的内建变量:

    FILENAME   当前输入文件的文件名,该变量是只读的
    NR      当前行的行号,该变量是只读的,R代表record
    NF      当前行所拥有的列数,该变量是只读的,F代表field
    OFS     输出格式的列分隔符,缺省是空格
    FS      输入文件的列分融符,缺省是连续的空格和Tab
    ORS     输出格式的行分隔符,缺省是换行符
    RS      输入文件的行分隔符,缺省是换行符

    例如打印系统中的用户帐号列表:

    [root@VM_0_5_centos test]# awk 'BEGIN {FS=":"} {print $1;}' /etc/passwd
    root
    bin
    daemon
  • 相关阅读:
    资源-python 视频下载大全
    ubuntu 16.04(操作应用) -软件卸载
    资源-简历的相关知识
    centos (命令操作)-crontab命令
    ubuntu 16.04 (软件应用)-输入法
    ntp时间同步
    lvm空间扩容
    目录知识
    Linux下安装maven
    elasticsearch安装pinyin模块
  • 原文地址:https://www.cnblogs.com/mmzs/p/9336906.html
Copyright © 2020-2023  润新知