User Tools

Site Tools


awk:awk_fields

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:awk_fields [2020/05/04 23:05] peterawk:awk_fields [2022/06/13 09:37] (current) peter
Line 1: Line 1:
 ====== AWK - AWK Fields ====== ====== AWK - AWK Fields ======
- 
-===== Understanding Fields. ===== 
  
 Assuming a file exists with the following contents: Assuming a file exists with the following contents:
Line 7: Line 5:
 <file> <file>
 10   Peter     Terence   Roux        45 10   Peter     Terence   Roux        45
-11   Virginia  Genevive  Roux        45+11   Virginia  Genevieve  Roux        45
 12   Felix     Devon     Roux         5 12   Felix     Devon     Roux         5
 13   David     Bruce     Stevenson   48 13   David     Bruce     Stevenson   48
Line 14: Line 12:
 </file> </file>
  
-Issue the following command:+---- 
 + 
 +===== Understanding Fields =====
  
-<code bash>+<code awk>
 awk '{print $1,$2,$3,$4,$5}' test.txt awk '{print $1,$2,$3,$4,$5}' test.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
 10 Peter Terence Roux 45 10 Peter Terence Roux 45
-11 Virginia Genevive Roux 45+11 Virginia Genevieve Roux 45
 12 Felix Devon Roux 5 12 Felix Devon Roux 5
 13 David Bruce Stevenson 48 13 David Bruce Stevenson 48
Line 31: Line 31:
 </code> </code>
  
-  * AWK has read each field in the file into the variables $1, $2, $3, etc.+<WRAP info> 
 +**NOTE:**  AWK has read each field in the file into the variables $1, $2, $3, etc. 
   * Each field is split by a field separator, which by default is a space or comma.   * Each field is split by a field separator, which by default is a space or comma.
     * As can be seen, it does not matter how many spaces separate each field in the input file.     * As can be seen, it does not matter how many spaces separate each field in the input file.
  
-<WRAP info> +  * As AWK reads the input, the entire record is assigned to the variable $0. 
-**NOTE:* As AWK reads the input, the entire record is assigned to the variable $0.+
 </WRAP> </WRAP>
  
Line 43: Line 45:
 ===== Display Only Certain Fields ===== ===== Display Only Certain Fields =====
  
-To only print the name and surname, i.e. fields $2 and $4, issue the following command:+To only print the name and surname, i.e. fields $2 and $4:
  
-<code bash>+<code awk>
 awk '{print $2,$4}' test.txt awk '{print $2,$4}' test.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
Line 62: Line 64:
 The ability to address each field with a unique number allows only specific fields to be output. The ability to address each field with a unique number allows only specific fields to be output.
  
-**NOTE** : Fields can be printed out in any order, for example:+<WRAP info> 
 +**NOTE** : Fields can be printed out in any order.  For example:
  
-<code bash>+<code awk>
 awk '{print $4,$2}' test.txt awk '{print $4,$2}' test.txt
 </code> </code>
  
-result:+returns:
  
 <code> <code>
Line 80: Line 83:
  
 Notice that the surname is now printed before first name. Notice that the surname is now printed before first name.
 +</WRAP>
  
 ---- ----
Line 85: Line 89:
 ===== Display Multi-line Output ===== ===== Display Multi-line Output =====
  
-To output onto multiple lines, issue the following command:+To output onto multiple lines:
  
-<code bash>+<code awk>
 awk '{print $2,$3,$4; print $1, $5}' test.txt awk '{print $2,$3,$4; print $1, $5}' test.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
 Peter Terence Roux Peter Terence Roux
 10 45 10 45
-Virginia Genevive Roux+Virginia Genevieve Roux
 11 45 11 45
 Felix Devon Roux Felix Devon Roux
Line 108: Line 112:
 </code> </code>
  
-The semicolon (;) indicates to write some data to a different line.+<WRAP info> 
 +**NOTE:**  The semicolon (;) indicates to write some data to a different line. 
 +</WRAP> 
  
 This could also be written as the following, which will produce the same results: This could also be written as the following, which will produce the same results:
  
-<code bash>+<code awk>
 awk '{print $2,$3,$4} {print $1, $5}' test.txt awk '{print $2,$3,$4} {print $1, $5}' test.txt
 </code> </code>
awk/awk_fields.1588633530.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki