• Awk 从入门到放弃(1)–学习笔记


    参考:朱双印博客

    1. 将test文件中的内容打印出来:
    vmuser@vmuser-virtual-machine:~/panzidong/awk$ echo ddd > test
    vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print}’ test
    ddd

    2. 输出文件第一列及第二列:
    vmuser@vmuser-virtual-machine:~/panzidong/awk$ cat test
    111 222 333 444
    555 666 777 888
    vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print $1,$2}’ test
    111 222
    555 666

    3 一次性输出多个指定的列, 第一列没有第五列就没有输出
    vmuser@vmuser-virtual-machine:~/panzidong/awk$ cat test
    111 222 333 444
    555 666 777 888 999
    vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print $1,$2,$5}’ test
    111 222
    555 666 999

    4 可以在输出中添加自已的字段:
    vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print $1,$2,”string”}’ test
    111 222 string
    555 666 string
    vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print $1,$2,666}’ test
    111 222 666
    555 666 666
    vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print “first row:”$1,”second row:”$2}’ test
    first row:111 second row:222
    first row:555 second row:666
    vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print “first row:” $1,”second row:” $2}’ test
    first row:111 second row:222
    first row:555 second row:666
    vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print “first row:” $1,”middle”,”second row:” $2}’ test
    first row:111 middle second row:222
    first row:555 middle second row:666

    5 内置变量的两侧不能有引号,否则会当作普通字符输出,如下所示:

    6 整行输出的方法

    7 awk  的两种特殊的模式 BEGIN t和 END模式

  • 相关阅读:
    安全和加密
    awk
    CentOS7练习
    CentOS7系统引导顺序以及排障
    网络配置
    RAID阵列搭建
    LVM逻辑卷
    java-web——第九课 request
    java-web——第八课 JSTL的显示格式
    java-web——第七课 JSTL
  • 原文地址:https://www.cnblogs.com/lianghong881018/p/10338140.html
Copyright © 2020-2023  润新知