美文网首页
Tip-Linux Awk

Tip-Linux Awk

作者: cctoken | 来源:发表于2019-06-03 01:36 被阅读0次

    AWK 简介

    Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan.

    AWK的功能

    1.AWK Operations

    • (a) Scans a file line by line
    • (b) Splits each input line into fields
    • (c) Compares input line/fields to pattern
    • (d) Performs action(s) on matched lines

    2.Useful for

    • (a) Transform data files
    • (b) Produce formatted reports

    3.Programming Construct

    • (a) Format output lines
    • (b) Arithmetic and string operations
    • (c) Conditionals and loops

    syntax

    awk options 'selection _criteria {action }' input-file > output-file

    Options

    -f program-file : Reads the AWK program source from the file
    program-file, instead of from the
    first command line argument.
    -F fs : Use fs for the input field separator

    Examples

    awk '/manager/ {print}' employee.txt
    用于筛选出 匹配 manager的行,打印出来,其中 '' 里面被分成了 selection 和对应的action,selection即是 匹配 manager, action是 print 整个行

    awk '{print $1,$4}' employee.txt
    $1,$4 分别表示为该行的第一列和第四列,切记,此处列数是从1开始的,不是从0开始,且fields的默认分隔符为空格和tab,其中 $0 表示为整行

    Built In Variables In Awk
    内置的变量,有一下几个
    NR:表明每行的行数,比如

    awk '{print NR, 0}' employee.txt 即把每行都打印出来且,带上每行的行数 1 adsb ajfisdo 2 abda fjadios 3 fjdiao oajisdf NF: 表明当前行,fields的数量 awk '{printNF}' employee.txt
    表明将文件中每行最后一个field打印出来

    FS: FS command contains the field separator character which is used to divide fields on the input line. The default is “white space”, meaning space and tab characters. FS can be reassigned to another character (typically in BEGIN) to change the field separator.
    RS: RS command stores the current record separator character. Since, by default, an input line is the input record, the default record separator character is a newline.

    OFS: OFS command stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. Whenever print has several parameters separated with commas, it will print the value of OFS in between each parameter.

    ORS: ORS command stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character. print automatically outputs the contents of ORS at the end of whatever it is given to print.

    more examples

    awk 'NR==3, NR==6 {print NR,$0}' employee.txt
    打印出3~6行的数据,且每行带上行号

    Reference

    AWK command in Unix/Linux with examples

    相关文章

      网友评论

          本文标题:Tip-Linux Awk

          本文链接:https://www.haomeiwen.com/subject/enfqxctx.html