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/06 20:02] – 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 14: | Line 27: | ||
<code awk> | <code awk> | ||
- | 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: | ||
<code awk> | <code awk> | ||
- | 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. | ||
+ | |||
+ | </ | ||
---- | ---- | ||
Line 46: | Line 68: | ||
===== Basic END Statement ===== | ===== Basic END Statement ===== | ||
+ | |||
+ | Assuming a file, **test2.txt**, | ||
+ | |||
+ | < | ||
+ | pens 10 1.99 | ||
+ | pencils | ||
+ | staplers | ||
+ | rulers | ||
+ | </ | ||
Issue the following command: | Issue the following command: | ||
<code awk> | <code awk> | ||
- | 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: | ||
<code awk> | <code awk> | ||
- | awk ' | + | awk ' |
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
Line 74: | 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. | * It is only run at the end. | ||
+ | |||
+ | </ | ||
---- | ---- | ||
awk/begin_and_end.1588795359.txt.gz · Last modified: 2020/07/15 09:30 (external edit)