awk:awk_fields
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
awk:awk_fields [2020/05/04 22:58] – created peter | awk: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 following contents: | + | |
< | < | ||
10 | 10 | ||
- | 11 | + | 11 |
12 | 12 | ||
13 | 13 | ||
Line 14: | Line 12: | ||
</ | </ | ||
- | Issue the following command: | + | ---- |
- | < | + | ===== Understanding Fields ===== |
- | awk ' | + | |
+ | < | ||
+ | awk ' | ||
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
10 Peter Terence Roux 45 | 10 Peter Terence Roux 45 | ||
- | 11 Virginia | + | 11 Virginia |
12 Felix Devon Roux 5 | 12 Felix Devon Roux 5 | ||
13 David Bruce Stevenson 48 | 13 David Bruce Stevenson 48 | ||
Line 31: | Line 31: | ||
</ | </ | ||
- | | + | <WRAP info> |
+ | **NOTE: | ||
* 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. | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Display Only Certain Fields ===== | ||
+ | |||
+ | To only print the name and surname, i.e. fields $2 and $4: | ||
+ | |||
+ | <code awk> | ||
+ | awk ' | ||
+ | </ | ||
+ | |||
+ | returns: | ||
+ | |||
+ | < | ||
+ | Peter Roux | ||
+ | Virginia Roux | ||
+ | Felix Roux | ||
+ | David Stevenson | ||
+ | Bob Smith | ||
+ | Adam Ridley | ||
+ | </ | ||
+ | |||
+ | The ability to address each field with a unique number allows only specific fields to be output. | ||
<WRAP info> | <WRAP info> | ||
- | **NOTE:** | + | **NOTE** |
+ | |||
+ | <code awk> | ||
+ | awk ' | ||
+ | </ | ||
+ | |||
+ | returns: | ||
+ | |||
+ | < | ||
+ | Roux Peter | ||
+ | Roux Virginia | ||
+ | Roux Felix | ||
+ | Stevenson David | ||
+ | Smith Bob | ||
+ | Ridley Adam | ||
+ | </ | ||
+ | |||
+ | Notice that the surname | ||
</ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Display Multi-line Output ===== | ||
+ | |||
+ | To output onto multiple lines: | ||
+ | |||
+ | <code awk> | ||
+ | awk ' | ||
+ | </ | ||
+ | |||
+ | returns: | ||
+ | |||
+ | < | ||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | </ | ||
+ | |||
+ | |||
+ | This could also be written as the following, which will produce the same results: | ||
+ | |||
+ | <code awk> | ||
+ | awk ' | ||
+ | </ | ||
+ | |||
+ | <WRAP important> | ||
+ | **WARNING: | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
awk/awk_fields.1588633112.txt.gz · Last modified: 2020/07/15 09:30 (external edit)