zoukankan      html  css  js  c++  java
  • 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模式

  • 相关阅读:
    Python内置的操作系统模块(os)与解释器交互模块(sys)
    Python常用模块-常见的加密算法模块使用
    Python常用模块-随机数模块(random)
    Python常用模块-时间模块(time&datetime)
    Python递归函数介绍
    Python内置函数之匿名(lambda)函数
    Python远程连接模块-Telnet
    Python的常用内置函数介绍
    Python的生成器进阶玩法
    Python中的列表解析和生成器表达式
  • 原文地址:https://www.cnblogs.com/lianghong881018/p/10338140.html
Copyright © 2011-2022 走看看