zoukankan      html  css  js  c++  java
  • Linux三剑客之awk

    Linux三剑客之grep

    Linux三剑客之sed

    awk

    1、描述

      awk等同于gawk,擅长处理列以及数据信息的统计

    2、语法结构
    awk [参数] '模式-动作'  文件
    3、awk内置变量
    $NF    The number of fields in the current input record.
    NR     The total number of input records seen so far.
    FS     The input field separator, a space by default.  See Fields, above.
    $N     指定列数,$3代表第三列
    $0     整行的文本内容;
    4、实际操作过程

    创建测试文件,下列文件使用空格分隔,不是table

    [root@doctor-10 ~]# cat test.txt
    Zhang  tingting java   192.168.12.10
    Wang   lili     python 192.168.12.11
    Wang   haha     scala  192.168.12.13
    Zhang  heihei   php    192.168.10.14
    1. 根据指定行号查询

      #打印指定行的内容
      [root@doctor-10 ~]# awk 'NR==3' test.txt
      Wang   haha     scala  192.168.12.13
      #打印第二行到第四行的信息
      [root@doctor-10 ~]# awk 'NR==2,NR==4' test.txt
      Wang   lili     python 192.168.12.11
      Wang   haha     scala  192.168.12.13
      Zhang  heihei   php    192.168.10.14
      #打印第二行,第四行的信息
      [root@doctor-10 ~]# awk 'NR==2;NR==4' test.txt
      Wang   lili     python 192.168.12.11
      Zhang  heihei   php    192.168.10.14
    2. 根据指定内容查找

      #打印包含指定信息的内容
      [root@doctor-10 ~]# awk '/Wang/' test.txt
      Wang   lili     python 192.168.12.11
      Wang   haha     scala  192.168.12.13
      #打印包含指定信息之间的内容
      [root@doctor-10 ~]# awk '/Zhang/,/Wang/' test.txt
      Zhang  tingting java   192.168.12.10
      Wang   lili     python 192.168.12.11
      Zhang  heihei   php    192.168.10.14
      #打印指定信息的内容,不连续
      [root@doctor-10 ~]# awk '/Zhang/;/Wang/' test.txt
      Zhang  tingting java   192.168.12.10
      Wang   lili     python 192.168.12.11
      Wang   haha     scala  192.168.12.13
      Zhang  heihei   php    192.168.10.14
    3. 查看指定列的信息

      #查看姓王的同学的姓和名
      [root@doctor-10 ~]# awk  '/Zhang/{print $1,$2,$3}' test.txt
      Zhang tingting java
      Zhang heihei php
      #查看姓张的同学的IP地址
      [root@doctor-10 ~]# awk '/Zhang/{print $NF}' test.txt
      192.168.12.10
      192.168.10.14
    4. 使用 -F 参数 指定分割符号

      [root@doctor-10 ~]# awk -F "." '{print $1","$NF}' test.txt 
      Zhang  tingting java   192,10
      Wang   lili     python 192,11
      Wang   haha     scala  192,13
      Zhang  heihei   php    192,14

      使用多个分隔符

      [root@doctor-10 ~]#  awk  -F "[ .]+" '/Zhang/{print $1,$2,$4,$5,$6,$NF}' test.txt
      Zhang tingting 192 168 12 10
      Zhang heihei 192 168 10 14
    5. 找出职业是 java 或者 python的人的名字

      [root@doctor-10 ~]# awk  '$3~/^java|^python/{print $1,$2}' test.txt
      Zhang tingting
      Wang lili 
    6. 找出第三列中以字母 a 或者字母 n 结尾的人的名字以及职业

      #第一种方式
      [root@doctor-10 ~]# awk  '$3~/a$|n$/{print $1,$2,$3}' test.txt
      Zhang tingting java
      Wang lili python
      Wang haha scala
      #第二种方式
      [root@doctor-10 ~]# awk  '$3~/[an]$/{print $1,$2,$3}' test.txt
      Zhang tingting java
      Wang lili python
      Wang haha scala
      #第三种方式
      [root@doctor-10 ~]# awk  '$3~/(a|n)$/{print $1,$2,$3}' test.txt
      Zhang tingting java
      Wang lili python
      Wang haha scala
    7. 使用awk实现替换操作

      #gsub  g:gawk   sub:substitue
      #语法格式: gsub(/要替换的内容/,"替换为……",替换的列)

      将姓张的同学的最后一列中的 '.' 替换为 $

      #两个命令之间使用; 分割
      [root@doctor-10 ~]# awk  '/Zhang/{gsub(/./,"$",$NF);print $NF}' test.txt
      192$168$12$10
      192$168$10$14
    8. 打印非注释行以及非空行

      [root@doctor-10 ~]# awk '/^$|^#/{print $0}' test.txt
      #注释信息
      ​
      ​
      ​
      ​
      #文件结束了
      [root@doctor-10 ~]# awk '!/^$|^#/{print $0}' test.txt
      Zhang  tingting java   192.168.12.10
      Wang   lili     python 192.168.12.11
      Wang   haha     scala  192.168.12.13
      Zhang  heihei   php    192.168.10.14  
    5、BEGIN 和END的使用

    BEGIN:在指定awk命令之前完成的动作;

    END:在指定完awk命令所完成的动作;

    使用BEGIN制作表头

    [root@doctor-10 ~]# awk 'BEGIN{print "姓","名","职业","IP"}{print $0}' test.txt |column -t
    姓           名        职业    IP
    Zhang        tingting  java    192.168.12.10
    Wang         lili      python  192.168.12.11
    Wang         haha      scala   192.168.12.13
    Zhang        heihei    php     192.168.10.14

    使用END统计/etc/services中的空行个数

    [root@doctor-10 ~]# grep -c '^$' /etc/services 
    17
    [root@doctor-10 ~]# i=0
    [root@doctor-10 ~]#  awk '/^$/{i++}END{print i}' /etc/services
    17

    使用END实现1-100之间的求和

    [root@doctor-10 ~]# seq 100 | awk '{sum=sum+$1}END{print sum}'
    5050

    未完待续……

     

  • 相关阅读:
    【SQLSERVER】动态游标的实现
    【Oracle】动态游标的实现
    【SQLSERVER】拷贝表数据
    LeetCode: Count and Say
    LeetCode: Construct Binary Tree from Preorder and Inorder Traversa
    LeetCode: Combinations
    LeetCode: Convert Sorted List to Binary Search Tree
    LeetCode: Decode Ways
    LeetCode: Combination Sum II
    LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
  • 原文地址:https://www.cnblogs.com/Yly-G/p/12382924.html
Copyright © 2011-2022 走看看