User Tools

Site Tools


awk:begin_and_end

This is an old revision of the document!


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:

VariableDetails
ARGCThe number of command line arguments.
ARGINDThe index in ARGV of the current file being processed.
ARGVArray of command line arguments. The array is indexed from 0 to ARGC - 1.
BINMODESpecifies use of “binary” mode for all file I/O.
CONVFMTThe conversion format for numbers, “%.6g”, by default.
FIELDWIDTHSA 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.
FNRThe input record number in the current input file.
FPATA 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.
FSThe input field separator, a space by default.
IGNORECASEControls the case-sensitivity of all regular expression and string operations.
NFThe number of fields in the current input record.
NRThe total number of input records seen so far.
OFMTThe output format for numbers, “%.6g”, by default.
OFSThe output field separator, a space by default.
ORSThe output record separator, by default a newline.
RSThe input record separator, by default a newline.
RTThe record terminator.
SUBSEPThe 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)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki