zoukankan      html  css  js  c++  java
  • awk命令

    awk用于操作文本文件中的列操作,下面为awk的一些常用操作

    测试文件awkTest.txt,文本如下:

    Bucks Milwaukee    60 22 0.732 
    Raptors Toronto    58 24 0.707 
    76ers Philadelphia 51 31 0.622
    Celtics Boston     49 33 0.598
    Pacers Indiana     48 34 0.585

    1、输出所有列的值$0
    [root@localhost shellTest]# awk '{print $0}' awkTest.txt
    
    #执行结果
    Bucks Milwaukee 60 22 0.732 
    Raptors Toronto 58 24 0.707 
    76ers Philadelphia 51 31 0.622
    Celtics Boston 49 33 0.598
    Pacers Indiana 48 34 0.585
    2、输出第3列的值$3
    [root@localhost shellTest]# awk '{print $3}' awkTest.txt

    #执行结果
    60 58 51 49 48

     3、输出内容包含0.5的行:包含内容使用双斜线//包含起来

    [root@localhost shellTest]# awk '/0.5/ {print $0}' awkTest.txt 
    #执行结果
    Celtics Boston     49 33 0.598
    Pacers Indiana     48 34 0.585

     4、输出数字开头的记录

    [root@localhost shellTest]# awk '/^[0-9]/ {print $0}' awkTest.txt 
    #执行结果
    76ers Philadelphia 51 31 0.622

     5、输出第二个字段包含ia的记录(包含使用~表示)

    awk '$2 ~/ia/ {print $0}' awkTest.txt 
    #执行结果
    76ers Philadelphia 51 31 0.622
    Pacers Indiana     48 34 0.585

    6、输出第三个字段≥50的记录

    [root@localhost shellTest]# awk '$3 >=50 {print $0}' awkTest.txt 
    #执行结果
    Bucks Milwaukee    60 22 0.732 
    Raptors Toronto    58 24 0.707 
    76ers Philadelphia 51 31 0.622


  • 相关阅读:
    Hashtable源码分析
    ConcurrentHashMap1.7源码分析
    JDK1.8新特性
    回炉Spring--Bean生命周期及AOP
    @DateTimeFormat 和 @JsonFormat 注解
    面向切面编程-日志切面应用及MDC使用
    maven 多模块开发
    maven 安装
    ztree树节点重叠问题
    Java问题解读系列之IO相关---Java深拷贝和浅拷贝
  • 原文地址:https://www.cnblogs.com/wzl0916/p/13924949.html
Copyright © 2011-2022 走看看