awk:awk_patterns
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
awk:awk_patterns [2020/05/04 23:16] – created peter | awk:awk_patterns [2022/06/13 09:51] (current) – [Match using Multiple Patterns] peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== AWK - AWK Patterns ====== | ====== AWK - AWK Patterns ====== | ||
+ | |||
+ | Assuming a file exists with the following contents: | ||
+ | |||
+ | < | ||
+ | 10 | ||
+ | 11 | ||
+ | 12 | ||
+ | 13 | ||
+ | 14 | ||
+ | 48 | ||
+ | </ | ||
+ | |||
+ | ---- | ||
===== Matching Patterns ===== | ===== Matching Patterns ===== | ||
- | To only print out lines which match a specific search criteria, issue the following command: | + | To only print out lines which **match** a specific search criteria: |
- | < | + | < |
awk '/45/ {print $2, | awk '/45/ {print $2, | ||
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
Line 16: | Line 29: | ||
</ | </ | ||
- | This only prints out the lines from the file which contained the number 45. | + | <WRAP info> |
+ | **NOTE: | ||
+ | </ | ||
---- | ---- | ||
Line 22: | Line 38: | ||
===== Multi-line Output with Patterns ===== | ===== Multi-line Output with Patterns ===== | ||
- | To output onto multiple lines, issue the following command: | + | To output onto multiple lines: |
- | < | + | < |
awk '/45/ {print $2,$3,$4 ; print $1, $5}' test.txt | awk '/45/ {print $2,$3,$4 ; print $1, $5}' test.txt | ||
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
Peter Terence Roux | Peter Terence Roux | ||
10 45 | 10 45 | ||
- | Virginia | + | Virginia |
11 45 | 11 45 | ||
</ | </ | ||
- | Again, this only prints out the lines from the file which contained the number 45. | + | <WRAP info> |
+ | **NOTE: | ||
- | But also prints out a 2nd line because of the use of the semicolon. | + | * But also prints out a 2nd line because of the use of the semicolon. |
- | A semicolon does not have to be used to allow using multiple commands. | + | </ |
- | < | + | <WRAP info> |
+ | **NOTE: | ||
+ | |||
+ | For instance issue the following command instead: | ||
+ | |||
+ | < | ||
awk '/45/ {print $2,$3,$4} {print $1, $5}' test.txt | awk '/45/ {print $2,$3,$4} {print $1, $5}' test.txt | ||
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
Peter Terence Roux | Peter Terence Roux | ||
10 45 | 10 45 | ||
- | Virginia | + | Virginia |
11 45 | 11 45 | ||
12 5 | 12 5 | ||
Line 62: | Line 84: | ||
This time an altogether different result is produced, than when a semicolon was used. | This time an altogether different result is produced, than when a semicolon was used. | ||
- | The reason for this is that the pattern that is used, /45/ is only applied to the command immediately following it. In this example, this was only against fields $2, $3 and $4. | + | The reason for this is that the pattern that is used, **/45/** is only applied to the command immediately following it. In this example, this was only against fields $2, $3 and $4. |
The 2nd print statement is not affected by the pattern and would apply to all fields in the input file. | The 2nd print statement is not affected by the pattern and would apply to all fields in the input file. | ||
+ | </ | ||
---- | ---- | ||
Line 72: | Line 95: | ||
To search for more than one pattern match at a time, issue the following command: | To search for more than one pattern match at a time, issue the following command: | ||
- | < | + | < |
awk '/ | awk '/ | ||
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
Line 85: | Line 108: | ||
</ | </ | ||
- | The pipe symbol | is used to provide multiple patterns. | + | <WRAP info> |
+ | **NOTE: | ||
+ | </ | ||
---- | ---- | ||
Line 91: | Line 117: | ||
===== Match against specific field ===== | ===== Match against specific field ===== | ||
- | To restrict the match to only a specific field, issue the following command: | + | To restrict the match to only a specific field: |
- | < | + | < |
- | awk '$5 ~ /45|48/ {print $2, | + | awk '$5 ~ /45|48/ {print $2, |
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
Line 105: | Line 131: | ||
</ | </ | ||
- | Note that this time Adam Ridley is not displayed even though the source file has a 48 as part of his record. | + | <WRAP info> |
+ | **NOTE: | ||
- | The tilda ~ ties the match to a specific field. | + | * The tilda ~ ties the match to a specific field. |
+ | |||
+ | </ | ||
---- | ---- | ||
Line 113: | Line 142: | ||
===== Match not against a specific field ===== | ===== Match not against a specific field ===== | ||
- | To have the match be the opposite of matching a specific field, issue the following command: | + | To have the match be the opposite of matching a specific field: |
- | < | + | < |
awk '$5 !~ /45|48/ {print $2, | awk '$5 !~ /45|48/ {print $2, | ||
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
Line 127: | Line 156: | ||
</ | </ | ||
- | The **exclamation mark** and **tilda** (!~) together informs to not match to a specific field. | + | <WRAP info> |
+ | **NOTE: | ||
+ | </ | ||
---- | ---- | ||
Line 133: | Line 165: | ||
===== Match in a Range ===== | ===== Match in a Range ===== | ||
- | To have the match be the opposite of matching a specific field, issue the following command: | + | To have the match be the opposite of matching a specific field: |
- | < | + | < |
- | awk '/ | + | awk '/ |
</ | </ | ||
- | which will display something like: | + | returns: |
< | < | ||
Line 148: | Line 180: | ||
</ | </ | ||
- | This finds all fields with values between 42 and 48. | + | <WRAP info> |
+ | **NOTE: | ||
+ | </ | ||
---- | ---- | ||
awk/awk_patterns.1588634174.txt.gz · Last modified: 2020/07/15 09:30 (external edit)