美文网首页
awk命令使用

awk命令使用

作者: 慧琴如翌 | 来源:发表于2018-04-23 01:37 被阅读4次

    用法一:

    awk '{[pattern] action}' {filenames} # 行匹配语句 awk '' 只能用单引号
    实例

    [root@k8s-uat-node73 riskbell]# cat 2.txt 
    2 this is a test
    3 Are you like awk
    This's a test
    10 There are orange,apple,mongo
    [root@k8s-uat-node73 riskbell]# awk '{print $1,$4}' 2.txt
    2 a
    3 like
    This's 
    10 orange,apple,mongo
    # 格式化输出
    [root@k8s-uat-node73 riskbell]# awk '{printf "%-8s %-10s\n",$1,$4}' 2.txt
    2        a         
    3        like      
    This's             
    10       orange,apple,mongo
    

    用法二:

    awk -F #-F相当于内置变量FS, 指定分割字符

    [root@k8s-uat-node73 riskbell]# cat 3.txt 
    a,1,q
    b,2,w
    c,3,e
    d,4,r
    [root@k8s-uat-node73 riskbell]# awk -F, '{print $1,$2}'   3.txt
    a 1
    b 2
    c 3
    d 4
    # 使用多个分隔符.先使用空格分割,然后对分割结果再使用","分割
     $ awk -F '[ ,]'  '{print $1,$2,$5}'   log.txt
    

    用法三:

    awk -v # 设置变量

    [root@k8s-uat-node73 riskbell]# cat 2.txt 
    2 this is a test
    3 Are you like awk
    This's a test
    10 There are orange,apple,mongo
    [root@k8s-uat-node73 riskbell]# awk -va=1 '{print $1,$1+a}' 2.txt 
    2 3
    3 4
    This's 1
    10 11
    

    http://www.runoob.com/linux/linux-comm-awk.html

    相关文章

      网友评论

          本文标题:awk命令使用

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