User Tools

Site Tools


awk:awk_patterns

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_patterns [2020/05/04 23:22] – [Multi-line Output with Patterns] peterawk:awk_patterns [2022/06/13 09:51] (current) – [Match using Multiple Patterns] peter
Line 5: 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 16: Line 16:
 ===== 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:
  
-<code bash>+<code awk>
 awk '/45/ {print $2,$4}' test.txt awk '/45/ {print $2,$4}' test.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
Line 29: Line 29:
 </code> </code>
  
-This only prints out the lines from the file which contained the number 45.+<WRAP info> 
 +**NOTE:**  This only prints out the lines from the file which contained the number 45. 
 +</WRAP> 
  
 ---- ----
Line 35: 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:
  
-<code bash>+<code awk>
 awk '/45/ {print $2,$3,$4 ; print $1, $5}' test.txt awk '/45/ {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
 </code> </code>
  
-Again, this only prints out the lines from the file which contained the number 45.+<WRAP info> 
 +**NOTE:**  This only prints out the lines from the file which contained the number 45.
  
-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. 
 + 
 +</WRAP>
  
 <WRAP info> <WRAP info>
Line 59: Line 65:
 For instance issue the following command instead: For instance issue the following command instead:
  
-<code bash>+<code awk>
 awk '/45/ {print $2,$3,$4} {print $1, $5}' test.txt awk '/45/ {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
 12 5 12 5
Line 89: 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:
  
-<code bash>+<code awk>
 awk '/45|48/ {print $2,$4}' test.txt awk '/45|48/ {print $2,$4}' test.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
Line 102: Line 108:
 </code> </code>
  
-The pipe symbol | is used to provide multiple patterns.+<WRAP info> 
 +**NOTE:**  The pipe symbol **|** is used to provide multiple patterns. 
 +</WRAP> 
  
 ---- ----
Line 108: 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:
  
-<code bash+<code awk
-awk '$5 ~ /45|48/ {print $2,$4}' /sharewiz/awk/test.txt+awk '$5 ~ /45|48/ {print $2,$4}' test.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
Line 122: Line 131:
 </code> </code>
  
-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:**  Adam Ridley is not displayed even though the source file has a 48 as part of his record.
  
-The tilda ~ ties the match to a specific field.+  * The tilda ~ ties the match to a specific field. 
 + 
 +</WRAP>
  
 ---- ----
Line 130: 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:
  
-<code bash>+<code awk>
 awk '$5 !~ /45|48/ {print $2,$4}' test.txt awk '$5 !~ /45|48/ {print $2,$4}' test.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
Line 144: Line 156:
 </code> </code>
  
-The **exclamation mark** and **tilda** (!~) together informs to not match to a specific field.+<WRAP info> 
 +**NOTE:**  The **exclamation mark** and **tilda** (!~) together informs to not match to a specific field. 
 +</WRAP> 
  
 ---- ----
Line 150: 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:
  
-<code bash+<code awk
-awk '/4[2-8]/ {print $2,$4}' /sharewiz/awk/test.txt+awk '/4[2-8]/ {print $2,$4}' test.txt
 </code> </code>
  
-which will display something like:+returns:
  
 <code> <code>
Line 165: Line 180:
 </code> </code>
  
-This finds all fields with values between 42 and 48.+<WRAP info> 
 +**NOTE:**  This finds all fields with values between 42 and 48. 
 +</WRAP> 
  
 ---- ----
  
  
awk/awk_patterns.1588634543.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki