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 22:59] 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:+----
  
-<code bash>+===== Understanding Fields ===== 
 + 
 +<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.
 +
 +  * As AWK reads the input, the entire record is assigned to the variable $0.
 +
 +</WRAP>
 +
 +----
 +
 +===== Display Only Certain Fields =====
 +
 +To only print the name and surname, i.e. fields $2 and $4:
 +
 +<code awk>
 +awk '{print $2,$4}' test.txt
 +</code>
 +
 +returns:
 +
 +<code>
 +Peter Roux
 +Virginia Roux
 +Felix Roux
 +David Stevenson
 +Bob Smith
 +Adam Ridley
 +</code>
 +
 +The ability to address each field with a unique number allows only specific fields to be output.
  
 <WRAP info> <WRAP info>
-**NOTE:**  As AWK reads the input, the entire record is assigned to the variable $0.+**NOTE** : Fields can be printed out in any order.  For example: 
 + 
 +<code awk> 
 +awk '{print $4,$2}' test.txt 
 +</code> 
 + 
 +returns: 
 + 
 +<code> 
 +Roux Peter 
 +Roux Virginia 
 +Roux Felix 
 +Stevenson David 
 +Smith Bob 
 +Ridley Adam 
 +</code> 
 + 
 +Notice that the surname is now printed before first name.
 </WRAP> </WRAP>
 +
 +----
 +
 +===== Display Multi-line Output =====
 +
 +To output onto multiple lines:
 +
 +<code awk>
 +awk '{print $2,$3,$4; print $1, $5}' test.txt
 +</code>
 +
 +returns:
 +
 +<code>
 +Peter Terence Roux
 +10 45
 +Virginia Genevieve Roux
 +11 45
 +Felix Devon Roux
 +12 5
 +David Bruce Stevenson
 +13 48
 +Bob James Smith
 +14 16
 +Adam Winter Ridley
 +48 23
 +</code>
 +
 +<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:
 +
 +<code awk>
 +awk '{print $2,$3,$4} {print $1, $5}' test.txt
 +</code>
 +
 +<WRAP important>
 +**WARNING:**  Although both uses of code produce the same result here, be warned that they can produce different results when used with more advanced AWK options.
 +</WRAP>
 +
 +
 +----
 +
  
awk/awk_fields.1588633197.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki