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/04 23:53] – [Basic END Statement] 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 13: Line 26:
 Issue the following command: Issue the following command:
  
-<code bash+<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 bash+<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>
  
 ---- ----
  
-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, "%.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".|+
  
-----+Assuming a file, **test2.txt**, exists with the following contents:
  
-===== Basic END Statement =====+<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 bash+<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 bash+<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 97: 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.1588636394.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki