User Tools

Site Tools


awk:begin_and_end

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
awk:begin_and_end [2020/05/06 20:02] peterawk: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**, exists with the following contents:
 +
 +<file>
 +10   Peter     Terence   Roux        45
 +11   Virginia  Genevieve  Roux       45
 +12   Felix     Devon     Roux         5
 +13   David     Bruce     Stevenson   48
 +14   Bob       James     Smith       16
 +48   Adam      Winter    Ridley      23
 +</file>
  
 ---- ----
Line 14: Line 27:
  
 <code awk> <code awk>
-awk 'BEGIN {print "NUMBER ITEM QUANTITY PRICE"}' /sharewiz/awk/test2.txt+awk 'BEGIN {print "NUMBER ITEM QUANTITY PRICE"}' test.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
 NUMBER   ITEM   QUANTITY   PRICE NUMBER   ITEM   QUANTITY   PRICE
 </code> </code>
 +
 +----
  
 This would be more useful when run as: This would be more useful when run as:
  
 <code awk> <code awk>
-awk 'BEGIN {print "NUMBER ITEM QUANTITY PRICE"}{print NR,$1,$2,$3}' /sharewiz/awk/test2.txt+awk 'BEGIN {print "NUMBER ITEM QUANTITY PRICE"}{print NR,$1,$2,$3}' test.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
-NUMBER   ITEM    QUANTITY   PRICE +NUMBER ITEM QUANTITY PRICE 
-pens 10 1.99 +1 10 Peter Terence 
-pencils 20 3.99 +11 Virginia Genevieve 
-3 staplers 8.99 +12 Felix Devon 
-4 rulers 12 2.50+4 13 David Bruce 
 +14 Bob James 
 +6 48 Adam Winter
 </code> </code>
 +
 +<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.
 +
 +</WRAP>
  
 ---- ----
Line 46: Line 68:
  
 ===== Basic END Statement ===== ===== Basic END Statement =====
 +
 +Assuming a file, **test2.txt**, exists with the following contents:
 +
 +<file>
 +pens      10    1.99
 +pencils   20    3.99
 +staplers      8.99
 +rulers    12    2.50
 +</file>
  
 Issue the following command: Issue the following command:
  
 <code awk> <code awk>
-awk '{x=x+($2*$3)} END {print "Total Value of Inventory: "x}' /sharewiz/awk/test2.txt+awk '{x=x+($2*$3)} END {print "Total Value of Inventory: "x}' test2.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
 Total Value of Inventory: 174.65 Total Value of Inventory: 174.65
 </code> </code>
 +
 +----
  
 This would be more useful when run as: This would be more useful when run as:
  
 <code awk> <code awk>
-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+awk '{x=x+($2*$3)} {print $1,"QTY: "$2,"PRICE:"$3,"TOTAL: "$2*$3} END {print "Total Value of Inventory: " x}' test2.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
Line 74: Line 107:
 Total Value of Inventory: 174.65 Total Value of Inventory: 174.65
 </code> </code>
 +
 +<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.
 +
 +</WRAP>
  
 ---- ----
  
  
awk/begin_and_end.1588795359.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki