Awk notes, awk learning notes

Source: Internet
Author: User
Tags natural logarithm

Awk notes, awk learning notes
Awk:

  • Awk names: The abbreviations of the three people: Aho, (Peter) weberger and (Brain) Kernighan;
  • In-row fields can be processed during row processing. This is not allowed by grep/sed. In special cases, $0 is used to indicate the row;
  • Usage: awk [-F re] [parameter...] ['prog'] [-f progfile] [in_file...]
    • -F column separator;
    • Prog: 'pattern' {action} '. pattern is a specific string and supports regular expressions, such as cat uids | awk'/[0-9] + \. [0-9] */{print }';
    • -F: a script file that complies with the awk syntax;
    • In_file: input file. Multiple Input files are allowed ;???
  • Pattern character representation:
    • \ Code change sequence
    • ^ Start matching at the beginning of the string
    • $ Start matching at the end of the string
    • . Match with any single string
    • [ABC] matches any character in []
    • [A-Ca-c] matches characters in the A-C and a-c range (in alphabetical order)
    • [^ ABC] matches any character other than all characters in [].
    • Desk | Chair matches any one of Desk and Chair
    • [ABC] [DEF] Association. Matches any character in A, B, and C, and must be followed by any character in D, E, and F.
    • [ABC] * matches any character in A, B, or C that appears 0 or multiple times
    • [ABC] + matches any one or more characters in A, B, or C.
    • ? Matches an empty string or A, B, or C in any character
    • (Blue | Black) the regular expression of berry merging, which matches with Blueberry or Blackberry.
  • Arithmetic Operators:
    • Y Power of x ^ y x
    • X ** y same as above
    • X % y calculate the remainder of x/y (Modulo)
    • X + y
    • X-y x minus y
    • X * y x multiplied by y
    • X/y x except y
    • -Y negative y (the switch symbol of y); also known as one minus
      + + Y + 1 and then use y (prefix)
    • Add 1 (suffix ),
    • -- Y minus 1 and use y (minus)
    • Y -- after use, y minus 1 (suffix minus)
    • X = y: Assign the value of y to x.
    • X + = y: Assign the value of x + y to x.
    • X-= y: Assign the value of x-y to x.
    • X * = y: Assign the value of x * y to x.
    • X/= y: Assign the value of x/y to x.
    • X % = y: Assign the value of x % y to x
    • X ^ = y: Assign the value of x ^ y to x
    • X ** = y: Assign the value of x ** y to x.
  • Logical operators:
    • X = y
    • X! = Y
    • X> y
    • X> = y
    • X <y
    • X <= y
    • X ~ Re
    • X !~ Re
  • Built-in variables:
    • ARGC: Number of command line parameters.
    • ARGIND: the location of the current file in the command line (starting from 0 ).
    • ARGV: An array containing command line parameters.
    • CONVFMT: Numeric conversion format (default value: %. 6g)
    • ENVIRON: environment variable associated array.
    • ERRNO: Description of the last system error.
    • FIELDWIDTHS: field width list (separated by Space key ).
    • FILENAME: The current file name.
    • FNR: Same as NR, but relative to the current file.
    • FS: Field separator (any space by default ).
    • IGNORECASE: if it is true, case-insensitive matching is performed.
    • NF: number of fields in the current record.
    • NR: Number of current records.
    • OFMT: Number output format (default value: %. 6g ).
    • OFS: delimiter of the output field (the default value is a space ).
    • ORSL: Output record delimiter (default value is a line break ).
    • RLENGTH: the length of the string matched by the match function.
    • RS: Record separator (a line break by default ).
    • RSTART: the first position of the string matched by the match function.
    • SUBSEP: array subscript separator (default value: \ 034)
  • Built-in functions: Class C style usage;
    • Printf: format the output, such as awk '{printf "% 03d % s \ n", NR, $1}' myfile
    • N gsub (reg, string, target) replaces the string
    • N index (search, string) returns the position of the search string in string
    • A length (string) calculates the number of characters in the string.
    • N match (string, reg) returns the position in the string matching the regular expression reg
    • N printf (format, variable) formats the output, and outputs the variable in the format provided by format.
    • N split (string, store, delim) Splits string into store array elements based on delim.
    • N sprintf (format, variable) returns a format-based formatted data. variables is the data to be placed in the string.
    • G strftime (format, timestamp) returns a date or time String Based on format. timestmp is the time returned by the systime () function.
    • The first time that N sub (reg, string, target) matches a regular expression reg, it replaces the string in the target string.
    • A substr (string, position, len) returns A substring starting with position len
    • P tolower (string) returns the lowercase characters in string.
    • P toupper (string) returns the uppercase characters in string.
    • Cotangent (radians) of A atan (x, y) x)
    • Cosine (radian) of N cos (x) x)
    • X power of A exp (x) e
    • Integer part of A int (x) x
    • Natural logarithm of A log (x) x
    • Random Number between N rand () 0-1
    • Sine (radian) of N sin (x) x)
    • Square root of A sqrt (x) x
    • A srand (x) initializes the random number generator. If x is ignored, system () is used ()
    • G system () returns the time elapsed since January 1, January 1, 1970 (in seconds)
  • Process control:
    • Logical operators :......
    • BEGIN/END:
    • If-else:
    • While/do-while//
    • Break/continue/exit (exit but do not skip END)
    • Next: Read the next record;
  • Content Control:
    • Read File: getline
    • Close file: close, which can be opened for getline files;
    • Export File: printf ("hello word! \ N ")>" datafile"
    • Export to other commands: printf ("hello word! \ N ") |" sort-t ','"
    • Read Other command results: who-u | awk '{printf ("% s is executing % s \ n", $2, $1 )}'
    • Run shell command line in awk: Embedded function system (). The parameters passed to this function are considered as command execution: END {close ("myreport.txt "); system ("lp myreport.txt ");}
  • Example:
    • Show text 7th ~ Row 15: awk-F % 'nr = 7, NR = 15 {printf $1 $3 $7 }'

  • Tips or note:
    • 1. If you want to explicitly type the variables used by the awk, you should assign the initial value to it in the program; if the awk does not know, the default string type is used;
    • 2. If a column is read as a numeric value and a numeric value is processed, awk treats it as 0 if the column is not a legal number;
    • 3 for (x in myarray) when printing an array, the output may be out of order;
    • 4 if (1 in fooarray), does the array contain this value ???? It seems to be ineffective.
    • 5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.