User Tools

Site Tools


wiki:2016awk

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
wiki:2016awk [2016/09/12 14:09]
tai
wiki:2016awk [2016/09/13 09:35] (current)
tai
Line 16: Line 16:
   * Drawback - cryptic syntax.   * Drawback - cryptic syntax.
  
-We don't have much time to sufficiently cover even the basics of awk.  The purpose of this is to introduce the power of AWK and provide a starting point for you to learn AWK on your own or to AWK code written by others.+We don't have much time to sufficiently cover even the basics of awk.  The purpose of this is to introduce the power of AWK and provide a starting point for you to learn AWK on your own or to modify and AWK code written by others.
  
 For a nice tutorial, see http://​www.grymoire.com/​Unix/​Awk.html. For a nice tutorial, see http://​www.grymoire.com/​Unix/​Awk.html.
Line 26: Line 26:
 Pattern can be basically anything, specific lines, specific columns, specific characters strings, etc. Pattern can be basically anything, specific lines, specific columns, specific characters strings, etc.
 \\ \\ \\ \\
-Pattern can also be the beginning of a file (BEGIN) or the end of a file (END), for example:+Pattern can also be the beginning of a file (BEGIN) or the end of a file (END). Here is an example ​of a simple awk script:
   ​   ​
   #!/bin/awk -f   #!/bin/awk -f
   BEGIN { print "​Beginning"​ }   BEGIN { print "​Beginning"​ }
         { print         }         { print         }
-  END   { print "​End" ​ } input_file >​output_file+  END   { print "​End" ​ }
   ​   ​
 This little program would print the word Beginning on a line, followed by each line of the file, and then a new line with the word End and output the results to a new file. This little program would print the word Beginning on a line, followed by each line of the file, and then a new line with the word End and output the results to a new file.
Line 60: Line 60:
 | next | script starts again at next line; subsequent commands are only run on files other than first | | next | script starts again at next line; subsequent commands are only run on files other than first |
 | %%n[$#]%% | array (e.g. NR==FNR{n[$0];​next} -> read all NR starting with FNR for each line in FNR) | | %%n[$#]%% | array (e.g. NR==FNR{n[$0];​next} -> read all NR starting with FNR for each line in FNR) |
-| gsub | global sub (e.g. {gsub("​\\.","​\t",​$1)}) |+| gsub | global sub (see example below) |
 | print | return output (by default adds new line; use printf to not add new line; use toupper() to change case; use print substr to return a defined number of characters from a string) | | print | return output (by default adds new line; use printf to not add new line; use toupper() to change case; use print substr to return a defined number of characters from a string) |
  
Line 85: Line 85:
  
 To print a single line number, for example line 7: To print a single line number, for example line 7:
-  awk 'NR == 7' input_file+  awk 'NR == 7 { print }' input_file
   ​   ​
 Even though we only want line 7, awk will still read through the entire file, which can be slow.  Instead we can exit after returning line 7: Even though we only want line 7, awk will still read through the entire file, which can be slow.  Instead we can exit after returning line 7:
Line 125: Line 125:
  
 ====2. sed==== ====2. sed====
-  * sed (stream editor) is a Unix tool+  * sed (stream editor) is a Unix tool.
   * Useful for making substitutions in a document.   * Useful for making substitutions in a document.
-  * Allows pattern matching with regular expressions¬†+  * Allows pattern matching with regular expressions.¬†
-  * Reads in a file line by line+  * Reads in a file line by line.
   * Simpler but far more limited ability compared to AWK, but allows for more complex substitutions than tr.   * Simpler but far more limited ability compared to AWK, but allows for more complex substitutions than tr.
  
wiki/2016awk.txt · Last modified: 2016/09/13 09:35 by tai