wiki:2016awk

# Differences

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

 wiki:2016awk [2016/09/12 14:09]tai wiki:2016awk [2016/09/13 09:35] (current)tai Both sides previous revision Previous revision 2016/09/13 09:35 tai 2016/09/12 14:09 tai 2016/09/12 12:08 tai 2016/09/12 12:07 tai 2016/09/12 10:55 tai 2016/09/12 10:55 tai 2016/09/09 12:46 tai 2016/09/09 12:40 tai 2016/09/09 12:18 tai 2016/09/08 09:32 tai 2016/09/07 17:08 tai 2016/09/07 14:24 tai 2016/09/02 15:58 tai 2016/08/31 12:09 tai 2016/08/31 12:07 tai 2016/08/31 12:06 tai 2016/08/31 12:05 tai 2016/08/31 12:00 tai 2016/08/31 11:59 tai 2016/08/31 11:56 tai 2016/08/31 11:47 tai 2016/08/31 11:22 tai 2016/08/31 09:46 tai 2016/08/31 09:37 tai created 2016/09/13 09:35 tai 2016/09/12 14:09 tai 2016/09/12 12:08 tai 2016/09/12 12:07 tai 2016/09/12 10:55 tai 2016/09/12 10:55 tai 2016/09/09 12:46 tai 2016/09/09 12:40 tai 2016/09/09 12:18 tai 2016/09/08 09:32 tai 2016/09/07 17:08 tai 2016/09/07 14:24 tai 2016/09/02 15:58 tai 2016/08/31 12:09 tai 2016/08/31 12:07 tai 2016/08/31 12:06 tai 2016/08/31 12:05 tai 2016/08/31 12:00 tai 2016/08/31 11:59 tai 2016/08/31 11:56 tai 2016/08/31 11:47 tai 2016/08/31 11:22 tai 2016/08/31 09:46 tai 2016/08/31 09:37 tai created 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.