User Tools

Site Tools


awk:awk_math_operations

Differences

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

Link to this comparison view

Next revision
Previous revision
awk:awk_math_operations [2020/05/06 20:11] – created peterawk:awk_math_operations [2022/06/13 09:45] (current) – [Advanced Example Using a Variable] peter
Line 3: Line 3:
 ===== Basic Math Operations ===== ===== Basic Math Operations =====
  
-Assuming a file exists with following contents:+Assuming a file, **test2.txt** exists with following contents:
  
 <file> <file>
Line 17: Line 17:
  
 <code awk> <code awk>
-awk '{print $1,"QTY: "$2,"PRICE: "$3,"TOTAL: "$2*$3}' /sharewiz/awk/test2.txt+awk '{print $1,"QTY: "$2,"PRICE: "$3,"TOTAL: "$2*$3}' test2.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
Line 29: Line 29:
 </code> </code>
  
-  Note that the total field multiples $2 with $3.+<WRAP info> 
 +**NOTE:**  The total multiples fields $2 with $3. 
 +</WRAP>
  
 +----
  
 AWK supports the full range of arithmetic operators including: AWK supports the full range of arithmetic operators including:
Line 52: Line 55:
  
 ===== Using a Variable ===== ===== Using a Variable =====
- 
-Issue the following command: 
  
 <code awk> <code awk>
-awk '{x=x+$2} {print x}' /sharewiz/awk/test2.txt+awk '{x=x+$2} {print x}' test2.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
Line 68: Line 69:
 </code> </code>
  
-This assigns a variable named x.+<WRAP info> 
 +**NOTE:**  This assigns a variable named x.
  
 It has shown a running total and calculated an overall total of 47 items. It has shown a running total and calculated an overall total of 47 items.
 +
 +</WRAP>
  
 ---- ----
  
 ===== Advanced Example Using a Variable ===== ===== Advanced Example Using a Variable =====
- 
-Issue the following command: 
  
 <code awk> <code awk>
-awk '{x=x+($2*$3)}{print $1,"QTY: "$2,"PRICE: "$3,"TOTAL: "$2*$3,"BAL: "x}' /sharewiz/awk/test2.txt+awk '{x=x+($2*$3)}{print $1,"QTY: "$2,"PRICE: "$3,"TOTAL: "$2*$3,"BAL: "x}' test2.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
Line 91: Line 93:
 </code> </code>
  
-This procedure gives a listing of each record while assigning a total value and keeping a running balance. +<WRAP info> 
 +**NOTE:**  This lists each record while assigning a total value and keeping a running balance. 
 +</WRAP>
  
 ---- ----
  
awk/awk_math_operations.1588795894.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki