awk:awk_fields
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
awk:awk_fields [2020/05/04 23:07] – 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 the following contents: | ||
Line 7: | Line 5: | ||
< | < | ||
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. | ||
- | <WRAP info> | + | |
- | **NOTE:** As AWK reads the input, the entire record is assigned to the variable $0. | + | |
</ | </ | ||
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: |
- | < | + | < |
awk ' | awk ' | ||
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
Line 63: | Line 65: | ||
<WRAP info> | <WRAP info> | ||
- | **NOTE** : Fields can be printed out in any order for example: | + | **NOTE** : Fields can be printed out in any order. For example: |
- | < | + | < |
awk ' | awk ' | ||
</ | </ | ||
- | result: | + | returns: |
< | < | ||
Line 87: | Line 89: | ||
===== Display Multi-line Output ===== | ===== Display Multi-line Output ===== | ||
- | To output onto multiple lines, issue the following command: | + | To output onto multiple lines: |
- | < | + | < |
awk ' | awk ' | ||
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
Peter Terence Roux | Peter Terence Roux | ||
10 45 | 10 45 | ||
- | Virginia | + | Virginia |
11 45 | 11 45 | ||
Felix Devon Roux | Felix Devon Roux | ||
Line 110: | Line 112: | ||
</ | </ | ||
- | The semicolon (;) indicates to write some data to a different line. | + | <WRAP info> |
+ | **NOTE: | ||
+ | </ | ||
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: | ||
- | < | + | < |
awk ' | awk ' | ||
</ | </ |
awk/awk_fields.1588633624.txt.gz · Last modified: 2020/07/15 09:30 (external edit)