zoukankan      html  css  js  c++  java
  • linux 基础7-正则表达式

    1. 基础正规表示法

    1.1 以grep获取字符串:

    在万用字符*是0-无限个字符,?是一个字符;在正则表达式中是0-无限个字符前一个相同字符。.一个前一个相同字符

    grep '^[a-z]' greptest #搜索以a-z开始的行
    grep '!$' greptest     #搜索以!结尾的行
    grep '.$' greptest    #搜索以.结尾的行,需要加入转义
    grep 'g..d' greptest   #.只代表一个字符
    grep 'goo*g' greptest  #*代表重复前面0个或者多个字符
    grep '[.!]$' greptest  #以.!结尾的行
    grep '[^.!]$' greptest  #不以.!结尾的行'
    grep 'go{2}g' greptest #以g开头,2个o和g结尾的行
    grep ‘go{2,5}’ greptest #以g开头2到5个o的行
    grep 'go{2,}g' greptest #以g开头,2个以上的o,以g结尾的行
    

    1.2 万用字符表示规则:

    ls -l ip?
    ls -l ip*
    

    1.3 ?和$符号的其他含义:

    ?:上一个执行命令所返回值,如果执行正确返回值为0,错误返回值非0

    $:表示当前shell的进程号(pid)

    echo $?
    echo $$
    

    2. printf

    3. sed工具简介

    sed是一个非交互文本编辑器,必须通过行号或正则表达式制定需要改变的行

    sed如何读取数据:sed从文件的一个文本行或者从标准输入读取数据,将读取的数据拷贝到一个编辑缓冲区,然后读命令行或者脚本的第一条命令,并使用这些查找模式或者定位行号来编辑它。重复此过程直到命令结束。

    cat -n greptest | sed '2a this line is added'
    cat -n greptest | sed '2i this line is inset'
    cat -n greptest | sed '2,3c this line is 2,3 replace'
    cat -n greptest | sed '2,8d'
    cat -n greptest | sed '2a this line is book'
    cat -n greptest | sed '2a this line is book' > greptestd #用数据流重导向可以改变源文件
    cat -n greptest | sed '1,8p'
    cat -n greptest | sed '$p' #显示最后一行
    cat -n greptest | sed 's /is//IS/'#把is替换为IS
    cat -n greptest | sed 's /is//IS/g'#把is替换为IS,全局替换
    

    可以对照1.1来比对。

    sed -n '^/a-z/'p greptest
    sed -n '^/!$/'p greptest
    sed -n '/g..d/'p greptest
    sed -n '/too*/'p greptest
    sed -n '/.$/'p greptest
    sed -n '/go{2}g/'p greptest
    sed -n '/go{2,}g/'p greptest
    sed -n '2,/is/'p greptest
    sed -n '1,/is/'p greptest #第一行不会匹配
    sed -n '/good/=' greptest
    
    
    
    
    cat -n greptest | sed '/good/ a add a line in the next line'
    
    cat -n greptest | sed '/good/ i add a line in the before line'
    cat -n greptest | sed '/good/ a add a line in the next line'
    

    sed -n '/glad/=' -e '/glad/p' greptest
    sed -n '/glad/=';'/glad/p' greptest
    sed -n '
    >/glad/=
    >/glad/p’ greptest
    
    sed -e 's/is/IS/' -e 's/am/WAS/' greptest
    sed -e 's/is/IS/g';'s/am/WAS/' greptest
    sed '
    >s/is/IS/g
    >s/am/WAS/' greptest
    

    #!/bin/sed -f
    /good/ a
    this line is append by good
    
    
    chmod u+x tst.sed
    ./tst.sed
    

    sed 's/is/IS/gw greptest' greptest
    

    sed '1G' greptest
    sed '/good/G' greptest
    sed -n 's/great/man &/' greptest
    

    4. awk工具简介

    如果设置了-F选项,则awk每次读一条记录或一行,并使用制定的分隔符分隔指定域,如果没有设置,awk则嘉定空格为域分隔符。

    awk 'BEGIN{print "this is the start----"} {print $1,$2,$3} END{print "this is the end"}' filename
    

    awk '$0~/good/' greptest
    awk 'if($0~/good/) print $0' greptest
    awk 'if($0~/^[^a-zA-Z]/) print $0' greptest
    awk 'if($0~/!$/) print $0' greptest
    awk 'if($0~/g..d/) print $0' greptest
    awk 'if($0~/goo*/) print $0' greptest
    awk 'if($0~/good|glad/) print $0' greptest #|是或的意思
    awk 'if($0~/g00+/) print $0' greptest
    awk 'if($0~/g00?/) print $0' greptest
    

    awk '{print NF,NR,$NF,$NR,$0}' greptest
    awk '{print NF,NR,$NF,$NR,$0}END{print "=====";print"FLIENAME"}' greptest
    awk 'BEGIN{FS=":"}{print NF,NR,$NF,$NR,$0}END{print "=====";print"FLIENAME"}' greptest
    

    awk 'gsub("good","GOOD"){print $0}' greptest
    

    5. 文件数据对比与打印的相关功能

    5.1 diff:

    5.2 cmp:

  • 相关阅读:
    68
    56
    Django manager 命令笔记
    Django 执行 manage 命令方式
    Django 连接 Mysql (8.0.16) 失败
    Python django 安装 mysqlclient 失败
    H.264 SODB RBSP EBSP的区别
    FFmpeg—— Bitstream Filters 作用
    MySQL 远程连接问题 (Windows Server)
    MySQL 笔记
  • 原文地址:https://www.cnblogs.com/agui125/p/9986257.html
Copyright © 2011-2022 走看看