zoukankan      html  css  js  c++  java
  • Linux-AWK命令

    参考菜鸟教程

    简介

    AWK 是一种处理文本文件的语言,是一个强大的文本分析工具。

    之所以叫 AWK 是因为其取了三位创始人 Alfred Aho,Peter Weinberger, 和 Brian Kernighan 的 Family Name 的首字符。

    语法

    awk [选项参数] 'script' var=value file(s)
    或
    awk [选项参数] -f scriptfile var=value file(s)
    

    基本用法

    用法1 awk '{[pattern] action}' {filenames}

    每行按空格或TAB分割,输出文本中的1、4项

    [root@node1 ~]# cat log.txt 
    2 this is a test
    3 Are you like awk
    This's a test
    10 There are orange,apple,mongo
    [root@node1 ~]# awk '{print $1, $4}' log.txt  # 行匹配语句 awk '' 只能用单引号
    2 a
    3 like
    This's 
    10 orange,apple,mongo
    [root@node1 ~]#
    [root@node1 ~]# awk '{printf "%-8s, %-10s
    ", $1, $4}' log.txt  # 格式化输出
    2       , a         
    3       , like      
    This's  ,           
    10      , orange,apple,mongo
    

    用法2 awk -F

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

    使用多个分隔符.先使用空格分割,然后对分割结果再使用","分割

    [root@node1 ~]# cat log.txt 
    2 this is a test
    3 Are you like awk
    This's a test
    10 There are orange,apple,mongo
    [root@node1 ~]# awk -F '[ ,]' '{print $1,$2,$5}' log.txt 
    2 this test
    3 Are awk
    This's a 
    10 There apple
    

    用法3 awk -v

    awk -v # 设置变量

    定义了两个变量,一个是a=1,一个是b=s

    [root@node1 ~]# awk -va=1 -vb=s '{print $1, $1+a, $1b}' log.txt 
    2 3 2s
    3 4 3s
    This's 1 This'ss
    10 11 10s
    

    运算符

    例子: 过滤出第一列大于2,并且第二列等于Are的

    [root@learn200 ~]# awk '$1>2 && $2="Are" {print $1,$2,$3}' log.txt 
    3 Are you
    This's Are test
    There Are orange,apple,mongo
    

    正则匹配

    ~ 表示模式开始。// 中是模式。

    1. 输出包含re单词的
    2. 输出第二行包含‘th’,打印第二行
    [root@learn200 ~]# cat log.txt 
    2 this is a test 
    3 Are you like awk 
    This's a test 10 
    There are orange,apple,mongo
    [root@learn200 ~]# awk '/re/' log.txt 
    3 Are you like awk 
    There are orange,apple,mongo
    [root@learn200 ~]# awk '$2 ~ /re/ {print $2}' log.txt 
    Are
    are
    [root@learn200 ~]# 
    

    忽略大小写

    [root@learn200 ~]# awk 'BEGIN{IGNORECASE=1} /this/' log.txt 
    2 this is a test 
    This's a test 10 
    [root@learn200 ~]# 
    

    模式取反

    [root@learn200 ~]# cat log.txt 
    2 this is a test 
    3 Are you like awk 
    This's a test 10 
    There are orange,apple,mongo
    [root@learn200 ~]# 
    [root@learn200 ~]# awk '$2 !~ /th/' log.txt 
    3 Are you like awk 
    This's a test 10 
    There are orange,apple,mongo
    [root@learn200 ~]# 
    [root@learn200 ~]# awk '!/th/' log.txt 
    3 Are you like awk 
    This's a test 10 
    There are orange,apple,mongo
    [root@learn200 ~]# 
    
    

    awk脚本

    • BEGIN{ 这里面放的是执行前的语句 }
    • END {这里面放的是处理完所有的行后要执行的语句 }
    • {这里面放的是处理每一行时要执行的语句}
    计算文件大小
    [root@learn200 ~]# ls -l *.txt
    -rw-r--r--. 1 root root   182 Oct 15  2019 cookies.txt
    -rw-r--r--. 1 root root    85 Jun 28 22:32 log.txt
    -rw-r--r--. 1 root root 17391 Dec 21  2019 top.txt
    [root@learn200 ~]# ls -l *.txt | awk '{sum+=$5} END {print sum}'
    17658
    [root@learn200 ~]# 
    [root@learn200 ~]# ll *.txt | awk 'BEGIN {sum = -1000} {sum+=$5} END{print sum}'
    16658
    
    
  • 相关阅读:
    Yii CGridView 之 SQL 语句
    Yii的srbac拓展中“用户已经获授权项”无法查看
    MyBatis通过注解实现映射中的嵌套语句和嵌套结果
    MyBatis一对多映射简单查询案例(嵌套结果)
    MyBatis绑定Mapper接口参数到Mapper映射文件sql语句参数
    将前端请求中的数据绑定到Spring MVC响应方法中参数的四种方法
    MyBatis一对多映射简单查询案例(嵌套Mapper映射文件中的sql语句)
    Spring MVC与html页面的交互(以传递json数据为例)
    使用Spring JDBC连接数据库(以SQL Server为例)
    spring mvc访问html页面404报错解决
  • 原文地址:https://www.cnblogs.com/AganRun/p/13205629.html
Copyright © 2011-2022 走看看