awk:begin_and_end
This is an old revision of the document!
Table of Contents
AWK - BEGIN and END
Actions can be specified to take place prior to the actual start of processing or after it has been completed with BEGIN and END statements respectively.
- BEGIN statements are most commonly used to establish variables or display a header.
- END statements, on the other hand, can be used to continue processing after the program has finished.
Basic BEGIN Statement
Issue the following command:
awk 'BEGIN {print "NUMBER ITEM QUANTITY PRICE"}' /sharewiz/awk/test2.txt
which will display something like:
NUMBER ITEM QUANTITY PRICE
This would be more useful when run as:
awk 'BEGIN {print "NUMBER ITEM QUANTITY PRICE"}{print NR,$1,$2,$3}' /sharewiz/awk/test2.txt
which will display something like:
NUMBER ITEM QUANTITY PRICE 1 pens 10 1.99 2 pencils 20 3.99 3 staplers 5 8.99 4 rulers 12 2.50
- The BEGIN has allowed the header to be added to the output.
- The NR variable used in the 2nd print numbers each line. It is a built-in variable of AWK.
Here is a list of some of the built-in variables supported by AWK:
Variable | Details |
---|---|
ARGC | The number of command line arguments. |
ARGIND | The index in ARGV of the current file being processed. |
ARGV | Array of command line arguments. The array is indexed from 0 to ARGC - 1. |
BINMODE | Specifies use of “binary” mode for all file I/O. |
CONVFMT | The conversion format for numbers, “%.6g”, by default. |
FIELDWIDTHS | A white-space separated list of field widths. When set the input is parsed into fields of fixed width, instead of using the value of the FS variable as the field separator. |
FNR | The input record number in the current input file. |
FPAT | A regular expression describing the contents of the fields in a record. When set the input is parsed into fields, where the fields match the regular expression, instead of using the value of the FS variable as the field separator. |
FS | The input field separator, a space by default. |
IGNORECASE | Controls the case-sensitivity of all regular expression and string operations. |
NF | The number of fields in the current input record. |
NR | The total number of input records seen so far. |
OFMT | The output format for numbers, “%.6g”, by default. |
OFS | The output field separator, a space by default. |
ORS | The output record separator, by default a newline. |
RS | The input record separator, by default a newline. |
RT | The record terminator. |
SUBSEP | The character used to separate multiple subscripts in array elements, by default “\034”. |
Basic END Statement
Issue the following command:
awk '{x=x+($2*$3)} END {print "Total Value of Inventory: "x}' /sharewiz/awk/test2.txt
which will display something like:
Total Value of Inventory: 174.65
This would be more useful when run as:
awk '{x=x+($2*$3)} {print $1,"QTY: "$2,"PRICE:"$3,"TOTAL: "$2*$3} END {print "Total Value of Inventory: " x}' /sharewiz/awk/test2.txt
which will display something like:
pens QTY: 10 PRICE:1.99 TOTAL: 19.9 pencils QTY: 20 PRICE:3.99 TOTAL: 79.8 staplers QTY: 5 PRICE:8.99 TOTAL: 44.95 rulers QTY: 12 PRICE:2.50 TOTAL: 30 Total Value of Inventory: 174.65
- The END has allowed a total line to be added to the output.
- It is only run at the end.
awk/begin_and_end.1588636394.txt.gz · Last modified: 2020/07/15 09:30 (external edit)