AWK is a powerful text analysis tool. Compared with grep lookup and SED editing, AWK is particularly powerful in data analysis and report generation. Simply put, AWK reads a file line by line, slicing each line with a space as the default separator, and then parsing it.

Introduction to the

AWK takes its name from the first letter of the surnames of its founders, Alfred Aho, Peter Weinberger and Brian Kernighan. AWK does, in fact, have its own language: AWK Programming Language, which the three creators have officially defined as a “style scanning and processing language.” It allows you to create short programs that read input files, sort data, process data, perform calculations on input, generate reports, and do countless other things.

Method of use

Basic usage

awk [-F FS] 'pattern{action}' filename
Copy the code

Parameters that

  • Pattern represents what AWK looks for in the data
  • Action A series of commands to execute when a match is found
  • -F FSSpecify the domain divider as FS(One row represents a record, which is split into fields according to FS)

The core action

  • Built-in variables
    • ARGCNumber of command lines
    • ARGVCommand line argument array
    • FILENAMECurrent input file name
    • NRRecord number (line number) in the current file
    • FSSets the field separator, equivalent to -f
    • RSSet record separator, default is ‘\n’
    • NFNumber of domains in the browsing history
    • NRNumber of read records
    • OFSOutput field separator
    • ORSOutput record delimiters
    • $0The whole record
    • $1/21/2 domain
  • Variables and assignments
    • The variable value can be a number or a string, but the key is how to use it.
  • Conditional and loop statements are borrowed entirely from C
    • Built-in function
      • digitalatan2/cos/exp/int/log/rand/sin/sqrt/srand
      • stringindex/length/match/toupper/tolower/substr/system/systime

The sample demo

  • /etc/passwd: file name, row number, column number, and complete row content

    awk -F ':' '{printf("filename:%11s,linenumber:%s,columns:%s,linecontent:%s\n",FILENAME,NR,NF,$0)}' /etc/passwd
    Copy the code

  • Count oo occurrences in /etc/passwd

    awk -F ':' 'BEGIN{count=0; printf("login_name\tpasswd\n")} /oo/{printf("%-15s\t%s\n",$1,$2); count++} END{printf("count is %d\n",count)}' /etc/passwd
    Copy the code

    Note: BEGIN{} indicates the action that takes place before processing any rows. END{} represents what happens after all rows are processed.

  • Count the row starting with s in the first column of /etc/passwd

    awk -F ':' '$1~/^s/{printf("%s\n",$1)}' /etc/passwd
    Copy the code

  • Count the first sys line in /etc/passwd

    awk -F ':' '$1=="sys"{printf("%s\n",$1)}' /etc/passwd
    Copy the code

    Note: both == and == are matches, but are not exact comparisons. They are usually used with regex

reference

  1. Wikipedia AWk
  2. Gawk website

If this article is helpful to you, or you are interested in technical articles, you can follow the wechat official number: Technical Tea Party, and you can receive relevant technical articles in the first time. Thank you!

This article was automatically published by ArtiPub