awk:begin_and_end
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
awk:begin_and_end [2020/05/04 23:52] – [Basic BEGIN Statement] peter | awk:begin_and_end [2022/06/13 09:40] (current) – peter | ||
---|---|---|---|
Line 6: | Line 6: | ||
* **END** statements, on the other hand, can be used to continue processing after the program has finished. | * **END** statements, on the other hand, can be used to continue processing after the program has finished. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | Assuming a file, **test.txt**, | ||
+ | |||
+ | < | ||
+ | 10 | ||
+ | 11 | ||
+ | 12 | ||
+ | 13 | ||
+ | 14 | ||
+ | 48 | ||
+ | </ | ||
---- | ---- | ||
Line 13: | Line 26: | ||
Issue the following command: | Issue the following command: | ||
- | < | + | < |
- | awk 'BEGIN {print " | + | awk 'BEGIN {print " |
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
NUMBER | NUMBER | ||
</ | </ | ||
+ | |||
+ | ---- | ||
This would be more useful when run as: | This would be more useful when run as: | ||
- | < | + | < |
- | awk 'BEGIN {print " | + | awk 'BEGIN {print " |
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
- | NUMBER | + | NUMBER ITEM QUANTITY PRICE |
- | 1 pens 10 1.99 | + | 1 10 Peter Terence |
- | 2 pencils 20 3.99 | + | 2 11 Virginia Genevieve |
- | 3 staplers | + | 3 12 Felix Devon |
- | 4 rulers 12 2.50 | + | 4 13 David Bruce |
+ | 5 14 Bob James | ||
+ | 6 48 Adam Winter | ||
</ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
* The **BEGIN** has allowed the header to be added to the output. | * 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. | * 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^ | + | ===== Basic END Statement ===== |
- | |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, " | + | |
- | |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, " | + | |
- | |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 " | + | |
- | ---- | + | Assuming a file, **test2.txt**, |
- | ===== Basic END Statement ===== | + | < |
+ | pens 10 1.99 | ||
+ | pencils | ||
+ | staplers | ||
+ | rulers | ||
+ | </ | ||
Issue the following command: | Issue the following command: | ||
- | < | + | < |
- | awk ' | + | awk ' |
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
Total Value of Inventory: 174.65 | Total Value of Inventory: 174.65 | ||
</ | </ | ||
+ | |||
+ | ---- | ||
This would be more useful when run as: | This would be more useful when run as: | ||
- | < | + | < |
- | awk ' | + | awk ' |
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
Line 97: | Line 107: | ||
Total Value of Inventory: 174.65 | Total Value of Inventory: 174.65 | ||
</ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
* The **END** has allowed a total line to be added to the output. | * The **END** has allowed a total line to be added to the output. | ||
+ | * It is only run at the end. | ||
+ | |||
+ | </ | ||
---- | ---- | ||
awk/begin_and_end.1588636335.txt.gz · Last modified: 2020/07/15 09:30 (external edit)