{"id":4339,"date":"2016-08-21T21:16:07","date_gmt":"2016-08-21T20:16:07","guid":{"rendered":"https:\/\/stevepedwards.today\/DebianAdmin\/?page_id=4339"},"modified":"2025-06-08T15:27:35","modified_gmt":"2025-06-08T14:27:35","slug":"awk-lines-and-info","status":"publish","type":"page","link":"https:\/\/stevepedwards.today\/DebianAdmin\/awk-lines-and-info\/","title":{"rendered":"Awk\/Perl\/SED Notepad"},"content":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_4339\" class=\"pvc_stats all  \" data-element-id=\"4339\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>Create yourself a copy of the awk\/sed man page - it's easier to read:<\/p>\n<p><span style=\"color: #0000ff;\">man -t awk | ps2pdf - &gt; awkman.pdf<\/span><\/p>\n<p><span style=\"color: #0000ff;\">man -t sed\u00a0| ps2pdf - &gt; sedman.pdf<\/span><\/p>\n<p>From sed &amp; awk by OReilly - <b><i>older kernel\/system example problems<\/i><\/b>...examples don't copy in lines, but columns often, and major parts of examples are assumed knowledge\/steps, so missing...etc...so you'll have to work it out as you go...<\/p>\n<p>A better intro is Shott's:<\/p>\n<p><span style=\"color: #00ff00;\"><a style=\"color: #00ff00;\" href=\"https:\/\/linuxcommand.org\/lc3_adv_awk.php\">https:\/\/linuxcommand.org\/lc3_adv_awk.php<\/a><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><em>VIM - select all and delete is<strong> :%d \u00a0 \u00a0<\/strong> select all and copy is\u00a0<strong>:%y \u00a0 <\/strong>this goes into vim\u00a0mem so can be pasted to another vim doc with Shift P.<\/em><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><em>FS: field separator, default = 1 whitespace<\/em><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><em>OFS: output FS,\u00a0default = 1 whitespace<\/em><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><em>NF: no. of fields<\/em><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><em>NR: no. of records<\/em><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><em>RS: record separator, default = newline<\/em><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><em>ORS: output record separator, default = newline<\/em><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><em>FNR: filename record<\/em><\/span><\/p>\n<p><span style=\"color: #ff0000;\">-e program-text\u00a0<\/span><span style=\"color: #ff0000;\">--source program-text<\/span><br \/>\n<span style=\"color: #ff0000;\">Use program-text as AWK program source code. This option allows the easy inter<\/span><span style=\"color: #ff0000;\">mixing of library functions (used via the -f and --file options) with source code\u00a0<\/span><span style=\"color: #ff0000;\">entered on the command line.<\/span><\/p>\n<p>Pattern\/Action...<\/p>\n<hr \/>\n<p>By default, awk runs a loop. It can have parameters set before and\/or after the loop body with BEGIN and END:<\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/awkloop.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4644\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/awkloop.png\" alt=\"awkloop.png\" width=\"926\" height=\"510\" \/><\/a><\/p>\n<p><span style=\"color: #0000ff;\"><em>\"Of these three parts, the main input loop or \"what happens during processing\" is where most of the work<\/em><\/span><br \/>\n<span style=\"color: #0000ff;\"><em>gets done. Inside the main input loop, your instructions are written as a series of pattern\/action<\/em><\/span><br \/>\n<span style=\"color: #0000ff;\"><em>procedures. A pattern is a rule for testing the input line to determine whether or not the action should be<\/em><\/span><br \/>\n<span style=\"color: #0000ff;\"><em>applied to it. The actions, as we shall see, can be quite complex, consisting of statements, functions, and<\/em><\/span><br \/>\n<span style=\"color: #0000ff;\"><em>expressions.<\/em><\/span><br \/>\n<span style=\"color: #0000ff;\"><em>The main thing to remember is that each pattern\/action procedure sits in the main input loop, which<\/em><\/span><br \/>\n<span style=\"color: #0000ff;\"><em>takes care of reading the input line. The procedures that you write will be applied to each input line, one<\/em><\/span><br \/>\n<span style=\"color: #0000ff;\"><em>line at a time.\"<\/em><\/span><\/p>\n<p>So, you need to KNOW re the awk loop above to complete the first line scan...if you want number of chars in a line e.g. by setting the FS (Field Separator) to null, when there are no white spaces between chars:<\/p>\n<p><span style=\"color: #0000ff;\">cat hashes.txt<\/span><\/p>\n<p>The number of hashes won't be read correctly for the first line, only the second by default loop. This could be a prob in a program eh? A headscratcher debugging it!<\/p>\n<p><span style=\"color: #0000ff;\">awk 'FS = \"\"; {print NF}' hashes.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">0<\/span><br \/>\n<span style=\"color: #ff0000;\">60<\/span><\/p>\n<p>You get round this by using a BEGIN function before the main awk loop body starts to allow the first line to be read before the loop body:<\/p>\n<p><span style=\"color: #0000ff;\">awk 'BEGIN {FS=\"\"} {print NF }' hashes.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">60<\/span><br \/>\n<span style=\"color: #ff0000;\">60<\/span><\/p>\n<p>Awk seems powerful as it is compact, quite easy to read, and generally gets a lot done in a relatively short script, compared to other langs, e,g. C. For example, run Shott's loop here to see:<\/p>\n<p><span style=\"color: #0000ff;\">ls -l \/ | awk '<\/span><br \/>\n<span style=\"color: #0000ff;\">$1 ~ \/^-\/ {t[\"Regular Files\"]++}<\/span><br \/>\n<span style=\"color: #0000ff;\">$1 ~ \/^d\/ {t[\"Directories\"]++}<\/span><br \/>\n<span style=\"color: #0000ff;\">$1 ~ \/^l\/ {t[\"Symbolic Links\"]++}<\/span><br \/>\n<span style=\"color: #0000ff;\">END {for (i in t) print i \":\\t\" t[i]}<\/span><br \/>\n<span style=\"color: #0000ff;\">'<\/span><br \/>\n<span style=\"color: #ff0000;\">Symbolic Links: 2<\/span><br \/>\n<span style=\"color: #ff0000;\">Directories: 24<\/span><br \/>\n<span style=\"color: #ff0000;\">Regular Files: 1<\/span><\/p>\n<p><strong>Basic Behaviour<\/strong><\/p>\n<p>All\u00a0awk variables are initialized to zero so this script works immediately, as 0 is inc'd to 1 at start:<\/p>\n<p><span style=\"color: #ff0000;\">BEGIN {<\/span><br \/>\n<span style=\"color: #ff0000;\">do {<\/span><br \/>\n<span style=\"color: #ff0000;\">++x<\/span><\/p>\n<p><span style=\"color: #ff0000;\">print x<\/span><br \/>\n<span style=\"color: #ff0000;\">} while ( x &lt;= 4 )<\/span><br \/>\n<span style=\"color: #ff0000;\">}<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -f do.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">1<\/span><br \/>\n<span style=\"color: #ff0000;\">2<\/span><br \/>\n<span style=\"color: #ff0000;\">3<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">5<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -e 'BEGIN {do {++x; print x} while ( x &lt;= 4 )}'<\/span><br \/>\n<span style=\"color: #ff0000;\">1<\/span><br \/>\n<span style=\"color: #ff0000;\">2<\/span><br \/>\n<span style=\"color: #ff0000;\">3<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">5<\/span><\/p>\n<p>Count Blank Lines (bad idea to make 6 blanks and 6 text, as seen later!!)<\/p>\n<p><span style=\"color: #0000ff;\">cat -n text<\/span><br \/>\n<span style=\"color: #ff0000;\">1 <\/span><br \/>\n<span style=\"color: #ff0000;\">2 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">3 <\/span><br \/>\n<span style=\"color: #ff0000;\">4 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">5 <\/span><br \/>\n<span style=\"color: #ff0000;\">6 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">7 <\/span><br \/>\n<span style=\"color: #ff0000;\">8 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">9 <\/span><br \/>\n<span style=\"color: #ff0000;\">10 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">11 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">12<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk '{print}' text<\/span><\/p>\n<p><span style=\"color: #ff0000;\">not a blank line<\/span><\/p>\n<p><span style=\"color: #ff0000;\">not a blank line<\/span><\/p>\n<p><span style=\"color: #ff0000;\">not a blank line<\/span><\/p>\n<p><span style=\"color: #ff0000;\">not a blank line<\/span><\/p>\n<p><span style=\"color: #ff0000;\">not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">not a blank line<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk '{ print NR }' text<\/span><br \/>\n<span style=\"color: #ff0000;\">1<\/span><br \/>\n<span style=\"color: #ff0000;\">2<\/span><br \/>\n<span style=\"color: #ff0000;\">3<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">5<\/span><br \/>\n<span style=\"color: #ff0000;\">6<\/span><br \/>\n<span style=\"color: #ff0000;\">7<\/span><br \/>\n<span style=\"color: #ff0000;\">8<\/span><br \/>\n<span style=\"color: #ff0000;\">9<\/span><br \/>\n<span style=\"color: #ff0000;\">10<\/span><br \/>\n<span style=\"color: #ff0000;\">11<\/span><br \/>\n<span style=\"color: #ff0000;\">12<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk '{ print NR, $0 }' text<\/span><br \/>\n<span style=\"color: #ff0000;\">1 <\/span><br \/>\n<span style=\"color: #ff0000;\">2 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">3 <\/span><br \/>\n<span style=\"color: #ff0000;\">4 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">5 <\/span><br \/>\n<span style=\"color: #ff0000;\">6 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">7 <\/span><br \/>\n<span style=\"color: #ff0000;\">8 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">9 <\/span><br \/>\n<span style=\"color: #ff0000;\">10 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">11 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">12<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk '{ print NF, $0 }' text<\/span><br \/>\n<span style=\"color: #ff0000;\">0 <\/span><br \/>\n<span style=\"color: #ff0000;\">4 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">0 <\/span><br \/>\n<span style=\"color: #ff0000;\">4 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">0 <\/span><br \/>\n<span style=\"color: #ff0000;\">4 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">0 <\/span><br \/>\n<span style=\"color: #ff0000;\">4 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">0 <\/span><br \/>\n<span style=\"color: #ff0000;\">4 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">4 not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">0<\/span><\/p>\n<p><span style=\"color: #0000ff;\">vi awk.scr<\/span><\/p>\n<p><span style=\"color: #ff0000;\"># test for integer,<\/span><br \/>\n<span style=\"color: #ff0000;\">#\/[0-9]+\/{ print \"That is an integer\" }<\/span><br \/>\n<span style=\"color: #ff0000;\">#\/[A-Za-z]+\/ { print \"This is a string\" }<\/span><br \/>\n<span style=\"color: #ff0000;\"><strong>\/^$\/ { print \"This is a blank line.\" }<\/strong><\/span><br \/>\n<span style=\"color: #ff0000;\">#\/^$\/ {print x += 1}<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -f awk.scr text<\/span><br \/>\n<span style=\"color: #ff0000;\">This is a blank line.<\/span><br \/>\n<span style=\"color: #ff0000;\">This is a blank line.<\/span><br \/>\n<span style=\"color: #ff0000;\">This is a blank line.<\/span><br \/>\n<span style=\"color: #ff0000;\">This is a blank line.<\/span><br \/>\n<span style=\"color: #ff0000;\">This is a blank line.<\/span><br \/>\n<span style=\"color: #ff0000;\">This is a blank line.<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk '\/^$\/ { print NR, \"This is a blank line.\" }' text<\/span><br \/>\n<span style=\"color: #ff0000;\">1 This is a blank line.<\/span><br \/>\n<span style=\"color: #ff0000;\">3 This is a blank line.<\/span><br \/>\n<span style=\"color: #ff0000;\">5 This is a blank line.<\/span><br \/>\n<span style=\"color: #ff0000;\">7 This is a blank line.<\/span><br \/>\n<span style=\"color: #ff0000;\">9 This is a blank line.<\/span><br \/>\n<span style=\"color: #ff0000;\">12 This is a blank line.<\/span><\/p>\n<p>This matches printable chars between start and end of line and counts each matching line processed<\/p>\n<p><span style=\"color: #0000ff;\">awk '\/<strong>^$<\/strong>\/ {print x += 1}' text<\/span><br \/>\n<span style=\"color: #ff0000;\">1<\/span><br \/>\n<span style=\"color: #ff0000;\">2<\/span><br \/>\n<span style=\"color: #ff0000;\">3<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">5<\/span><br \/>\n<span style=\"color: #ff0000;\">6<\/span><\/p>\n<p>This matches each line start so counts all lines inc. blanks, same as NR.<\/p>\n<p><span style=\"color: #0000ff;\">awk '\/<strong>^\/<\/strong> {print x += 1}' text;<\/span><br \/>\n<span style=\"color: #ff0000;\">1<\/span><br \/>\n<span style=\"color: #ff0000;\">2<\/span><br \/>\n<span style=\"color: #ff0000;\">3<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">5<\/span><br \/>\n<span style=\"color: #ff0000;\">6<\/span><br \/>\n<span style=\"color: #ff0000;\">7<\/span><br \/>\n<span style=\"color: #ff0000;\">8<\/span><br \/>\n<span style=\"color: #ff0000;\">9<\/span><br \/>\n<span style=\"color: #ff0000;\">10<\/span><br \/>\n<span style=\"color: #ff0000;\">11<\/span><br \/>\n<span style=\"color: #ff0000;\">12<\/span><\/p>\n<p>This counts NF, so states \u00a0which have no fields, so show blank lines = 0 fields<\/p>\n<p><span style=\"color: #0000ff;\">awk ' {print NF} ' text;<\/span><br \/>\n<span style=\"color: #ff0000;\">0<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">0<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">0<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">0<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">0<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">0<\/span><\/p>\n<p>Or, by much opposite logic, non blank lines using Shotts POSIX (or \/[:alnum]\/ ;\u00a0\/[:ascii]\/ \/[:print]\/ \/[:space]\/ etc.)<\/p>\n<p><span style=\"color: #0000ff;\">awk '\/[:alpha]\/ { print NR, \"This is a string\" }' text<\/span><br \/>\n<span style=\"color: #ff0000;\">2 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">4 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">6 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">8 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">10 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">11 This is a string<\/span><\/p>\n<p>This can be confusing; you may think blank lines can have a newline at the start\/end (they do!), but \/^$\/ above, and [:ctrl] below doesn't show that, yet Libre shows they do - 12 in all, so you MUST put the ^$ in quotes to see them, as newlines or ctrl chars don't work fully to show all 12:<\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/newlines.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4411\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/newlines.png\" alt=\"newlines.png\" width=\"162\" height=\"196\" \/><\/a><\/p>\n<p><span style=\"color: #0000ff;\">awk '\/'^$'\/ {print \" record found\" }' text \u00a0 \u00a0<span style=\"color: #ffffff;\">same as<\/span>\u00a0 \u00a0 \u00a0awk '\/^\/ {print \" record found\" }' text<\/span><br \/>\n<span style=\"color: #ff0000;\">record found<\/span><br \/>\n<span style=\"color: #ff0000;\">record found<\/span><br \/>\n<span style=\"color: #ff0000;\">record found<\/span><br \/>\n<span style=\"color: #ff0000;\">record found<\/span><br \/>\n<span style=\"color: #ff0000;\">record found<\/span><br \/>\n<span style=\"color: #ff0000;\">record found<\/span><br \/>\n<span style=\"color: #ff0000;\">record found<\/span><br \/>\n<span style=\"color: #ff0000;\">record found<\/span><br \/>\n<span style=\"color: #ff0000;\">record found<\/span><br \/>\n<span style=\"color: #ff0000;\">record found<\/span><br \/>\n<span style=\"color: #ff0000;\">record found<\/span><br \/>\n<span style=\"color: #ff0000;\">record found<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk '\/\/' text<\/span><\/p>\n<p><span style=\"color: #ff0000;\">not a blank line<\/span><\/p>\n<p><span style=\"color: #ff0000;\">not a blank line<\/span><\/p>\n<p><span style=\"color: #ff0000;\">not a blank line<\/span><\/p>\n<p><span style=\"color: #ff0000;\">not a blank line<\/span><\/p>\n<p><span style=\"color: #ff0000;\">not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">not a blank line<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk '\/ \/' text<\/span><br \/>\n<span style=\"color: #ff0000;\">not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">not a blank line<\/span><br \/>\n<span style=\"color: #ff0000;\">not a blank line<\/span><\/p>\n<p><strong>blank lines only:<\/strong><\/p>\n<p><span style=\"color: #0000ff;\">awk '\/^$\/ {print NR \" record found\" }' text<\/span><br \/>\n<span style=\"color: #ff0000;\">1 record found<\/span><br \/>\n<span style=\"color: #ff0000;\">3 record found<\/span><br \/>\n<span style=\"color: #ff0000;\">5 record found<\/span><br \/>\n<span style=\"color: #ff0000;\">7 record found<\/span><br \/>\n<span style=\"color: #ff0000;\">9 record found<\/span><br \/>\n<span style=\"color: #ff0000;\">12 record found<\/span><\/p>\n<p>non blanks IDd by new line char :<\/p>\n<p><span style=\"color: #0000ff;\">awk '\/'\\n'\/ { print NR \" This is a string\" }' text<\/span><br \/>\n<span style=\"color: #ff0000;\">2 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">4 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">6 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">8 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">10 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">11 This is a string<\/span><\/p>\n<p>non blanks IDd by non printable Ctrl char\u00a0:<\/p>\n<p><span style=\"color: #0000ff;\">awk '\/[:ctrl]\/ { print NR \" This is a string\" }' text<\/span><br \/>\n<span style=\"color: #ff0000;\">2 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">4 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">6 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">8 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">10 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">11 This is a string<\/span><\/p>\n<p>via a regex:<\/p>\n<p><span style=\"color: #0000ff;\">awk '\/[0-9a-zA-Z]\/ { print NR \" This is a string\" }' text<\/span><br \/>\n<span style=\"color: #ff0000;\">2 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">4 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">6 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">8 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">10 This is a string<\/span><br \/>\n<span style=\"color: #ff0000;\">11 This is a string<\/span><\/p>\n<p>blank line\u00a0removal (not necessarily the same as white space removal and you need to be careful about what else may be removed - like } in code!!):<\/p>\n<p><span style=\"color: #0000ff;\">awk 'gsub(\" \",\"\")' &lt; text<\/span><br \/>\n<span style=\"color: #ff0000;\">notablankline<\/span><br \/>\n<span style=\"color: #ff0000;\">notablankline<\/span><br \/>\n<span style=\"color: #ff0000;\">notablankline<\/span><br \/>\n<span style=\"color: #ff0000;\">notablankline<\/span><br \/>\n<span style=\"color: #ff0000;\">notablankline<\/span><br \/>\n<span style=\"color: #ff0000;\">notablankline<\/span><\/p>\n<p>Part of a CSS style file with blank lines and {} :<\/p>\n<p><span style=\"color: #ff0000;\"><span style=\"color: #0000ff;\">cat style.css\u00a0<\/span><\/span><\/p>\n<p><span style=\"color: #ff0000;\">text-indent: -9999px;<\/span><\/p>\n<p><span style=\"color: #ff0000;\">background: url(images\/menu-indicator-right.png) no-repeat;<\/span><\/p>\n<p><strong><span style=\"color: #ff0000;\">\/*border: solid 1px red;*\/<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #ff0000;\">}<\/span><\/strong><\/p>\n<p>Piping it thru gosub\u00a0as above removes blank lines but also the closing }\u00a0 from each section !!<\/p>\n<p><span style=\"color: #0000ff;\">cat style.css | awk 'gsub(\" \",\"\")'<\/span><\/p>\n<p><span style=\"color: #ff0000;\">.menu1ulul.sf-sub-indicator{<\/span><br \/>\n<span style=\"color: #ff0000;\">position:absolute;<\/span><br \/>\n<span style=\"color: #ff0000;\">width:7px;<\/span><br \/>\n<span style=\"color: #ff0000;\">height:15px;<\/span><br \/>\n<span style=\"color: #ff0000;\">top:8px;<\/span><br \/>\n<span style=\"color: #ff0000;\">right:15px;<\/span><br \/>\n<span style=\"color: #ff0000;\">text-indent:-9999px;<\/span><br \/>\n<strong><span style=\"color: #ff0000;\">background:url(images\/menu-indicator-right.png)no-repeat;<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">\/*border:solid1pxred;*\/<\/span><\/strong><\/p>\n<p>Can use :\u00a0<span style=\"color: #0000ff;\">cat style.css | sed '\/^[[:space:]]*$\/d' <span style=\"color: #ffffff;\">OR<\/span> \u00a0cat style.css | sed '\/^.$\/d'<\/span><\/p>\n<p><span style=\"color: #ff0000;\">.menu1 ul ul .sf-sub-indicator {<\/span><br \/>\n<span style=\"color: #ff0000;\">position: absolute;<\/span><br \/>\n<span style=\"color: #ff0000;\">width: 7px;<\/span><br \/>\n<span style=\"color: #ff0000;\">height: 15px;<\/span><br \/>\n<span style=\"color: #ff0000;\">top: 8px;<\/span><br \/>\n<span style=\"color: #ff0000;\">right: 15px;<\/span><br \/>\n<span style=\"color: #ff0000;\">text-indent: -9999px;<\/span><br \/>\n<span style=\"color: #ff0000;\">background: url(images\/menu-indicator-right.png) no-repeat;<\/span><br \/>\n<span style=\"color: #ff0000;\">\/*border: solid 1px red;*\/<\/span><br \/>\n<span style=\"color: #ff0000;\">}<\/span><\/p>\n<p>sed removes whitespace between words, but NOT blank lines with this:<\/p>\n<p><span style=\"color: #0000ff;\">sed 's\/ \/\/g' &lt; text<\/span><\/p>\n<p><span style=\"color: #ff0000;\">notablankline<\/span><\/p>\n<p><span style=\"color: #ff0000;\">notablankline<\/span><\/p>\n<p><span style=\"color: #ff0000;\">notablankline<\/span><\/p>\n<p><span style=\"color: #ff0000;\">notablankline<\/span><\/p>\n<p><span style=\"color: #ff0000;\">notablankline<\/span><br \/>\n<span style=\"color: #ff0000;\">notablankline<\/span><\/p>\n<p><strong>Negation - a real world problem. Dir full of .c files with their binaries that are not +x so cannot run due to a past chmod error. ID all those with a .c extension, then negate the term to show only none .c files:<\/strong><\/p>\n<p><span style=\"color: #0000ff;\">ls<\/span><\/p>\n<p><span style=\"color: #ff0000;\">floating sevendebug vars<\/span><br \/>\n<span style=\"color: #ff0000;\">floating.c sevendebug.c vars.c<\/span><br \/>\n<span style=\"color: #ff0000;\">full sidefx vowels<\/span><br \/>\n<span style=\"color: #ff0000;\">full1 sidefx.c vowels.c<\/span><br \/>\n<span style=\"color: #ff0000;\">full1.c sieve vsphere<\/span><br \/>\n<span style=\"color: #ff0000;\">full.c sieve1.py vsphere.c<\/span><br \/>\n<span style=\"color: #ff0000;\">graph sieve.c zero<\/span><br \/>\n<span style=\"color: #ff0000;\">graph.c Sieve.c zero.c<\/span><br \/>\n<span style=\"color: #ff0000;\">guess SievefromWeb<\/span><\/p>\n<p>shows only the .c files:<\/p>\n<p><span style=\"color: #0000ff;\">ls | awk '\/.c$\/'<\/span><\/p>\n<p><span style=\"color: #ff0000;\">triang.c<\/span><br \/>\n<span style=\"color: #ff0000;\">tri.c<\/span><br \/>\n<span style=\"color: #ff0000;\">trisub.c<\/span><br \/>\n<span style=\"color: #ff0000;\">twiceterm.c<\/span><br \/>\n<span style=\"color: #ff0000;\">two.c<\/span><br \/>\n<span style=\"color: #ff0000;\">vars.c<\/span><br \/>\n<span style=\"color: #ff0000;\">vowels.c<\/span><br \/>\n<span style=\"color: #ff0000;\">vsphere.c<\/span><br \/>\n<span style=\"color: #ff0000;\">zero.c<\/span><\/p>\n<p>now negate it to show only the exe files that need chmod +x<\/p>\n<p><span style=\"color: #0000ff;\">ls | awk '!\/.c$\/'<\/span><\/p>\n<p><span style=\"color: #ff0000;\">triang2<\/span><br \/>\n<span style=\"color: #ff0000;\">trisub<\/span><br \/>\n<span style=\"color: #ff0000;\">twiceterm<\/span><br \/>\n<span style=\"color: #ff0000;\">two<\/span><br \/>\n<span style=\"color: #ff0000;\">UnitsConverter<\/span><br \/>\n<span style=\"color: #ff0000;\">vars<\/span><br \/>\n<span style=\"color: #ff0000;\">vowels<\/span><br \/>\n<span style=\"color: #ff0000;\">vsphere<\/span><br \/>\n<span style=\"color: #ff0000;\">zero<\/span><\/p>\n<p>Now they can all be +x:<\/p>\n<p><span style=\"color: #0000ff;\">chmod +x `ls | awk '!\/.c$\/' `<\/span><\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/cexes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4768\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/cexes.png\" alt=\"cexes.png\" width=\"658\" height=\"199\" \/><\/a><\/p>\n<hr \/>\n<p><span style=\"color: #0000ff;\">dstat -tc | awk '{if(NR&gt;3) print $0 fflush()}' | tee dstatlive.txt <span style=\"color: #ffffff;\">(used with\u00a0<\/span><\/span><span style=\"color: #ffffff;\">watch -n 1 gnuplot livedstat.gnu)<\/span><\/p>\n<p><span style=\"color: #0000ff;\">sadf -p -P 0,1,2,3 | grep user | grep cpu0 | awk '{print $3,$4,$8}'<\/span><\/p>\n<p><span style=\"color: #0000ff;\">cat data1.dat | awk '{print $3}' | sed ':a;N;$!ba;s\/\\n\/; \/g' <span style=\"color: #ffffff;\">\u00a0(creates 1 long line - comma delimits each line by removing \\n, except last line )<\/span><\/span><\/p>\n<p><span style=\"color: #0000ff;\">sadf -p -P 0,1,2,3 | grep user | grep cpu0 | awk '{print $3,$4,$8}' | sed 's\/:01\/\/g' &gt; data1.dat; gnuplot plot1.gnu; xdg-open userCPU.png <span style=\"color: #ffffff;\">(see gnu live data Posts)<\/span><\/span><\/p>\n<p><span style=\"color: #0000ff;\">sadf -p -P 0,1,2,3 | grep idle | grep cpu0 | awk '{print $3,$4,$8}' | sed 's\/:01\/\/g' &gt; data1.dat; gnuplot plot1.gnu; mv -v userCPU.png idleCPU.png ; xdg-open idleCPU.png\u00a0<span style=\"color: #ffffff;\">(see gnu live data Posts)<\/span><\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk 'gsub(\" \",\"\")' &lt; file.txt <span style=\"color: #ffffff;\">(removes whitespace)<\/span><\/span><\/p>\n<p><span style=\"color: #0000ff;\">cat 1_5.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">1<\/span><br \/>\n<span style=\"color: #ff0000;\">2<\/span><br \/>\n<span style=\"color: #ff0000;\">3<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">5<\/span><\/p>\n<p><strong>AWK Subs<\/strong><\/p>\n<p>Sub a function for text:<\/p>\n<p><span style=\"color: #0000ff;\">awk 'gsub(\"blank\",sqrt(2))' text<\/span><br \/>\n<span style=\"color: #ff0000;\">not a 1.41421 line<\/span><br \/>\n<span style=\"color: #ff0000;\">not a 1.41421 line<\/span><br \/>\n<span style=\"color: #ff0000;\">not a 1.41421 line<\/span><br \/>\n<span style=\"color: #ff0000;\">not a 1.41421 line<\/span><br \/>\n<span style=\"color: #ff0000;\">not a 1.41421 line<\/span><br \/>\n<span style=\"color: #ff0000;\">not a 1.41421 line<\/span><\/p>\n<p>Replace all numbers with a char:<\/p>\n<p><span style=\"color: #0000ff;\">awk 'gsub(\"[0-9]\",\"=\")' 1_5.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">=<\/span><br \/>\n<span style=\"color: #ff0000;\">=<\/span><br \/>\n<span style=\"color: #ff0000;\">=<\/span><br \/>\n<span style=\"color: #ff0000;\">=<\/span><br \/>\n<span style=\"color: #ff0000;\">=<\/span><\/p>\n<p><span style=\"color: #0000ff;\">cat ABABAB.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">ABABAB<\/span><\/p>\n<p><span style=\"color: #ff0000;\">awk -e '{if (gsub(\/A\/,\"B\")) {print}}' &lt; ABABAB.txt<\/span><br \/>\n<span style=\"color: #0000ff;\">BBBBBB<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{if (gsub(\/B\/,\" \")) {print}}' &lt; ABABAB.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">A A A<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{if (gsub(\/B\/,\"\")) {print}}' &lt; ABABAB.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">AAA<\/span><\/p>\n<p>Subs one char for the whole record\/string ...? Shows looping behaviour of awk. Useful? Maybe:<\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{if (gsub(\/B\/,tolower($0) )) {print}}' &lt; ABABAB.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">AabababAabababAababab<\/span><\/p>\n<p>OR<\/p>\n<p><span style=\"color: #0000ff;\">echo \"ABABAB\" | awk -e '{if (gsub(\/B\/,tolower($0) )) {print}}'<\/span><br \/>\n<span style=\"color: #ff0000;\">AabababAabababAababab<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{print tolower($0)}' &lt; ABABAB.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">ababab<\/span><\/p>\n<p><strong>Again, note the loop effects and first line issue without a BEGIN:<\/strong><\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{print $0\"\\n\",tolower($0)}' &lt; ABABAB.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">ABABAB<\/span><br \/>\n<span style=\"color: #ff0000;\">\u00a0 ababab<\/span><\/p>\n<p><strong><span style=\"color: #0000ff;\">awk -e '{print $0\"\\n\", tolower($0)}' &lt; ABABAB.txt | awk -e '{print $0\"\\n\", toupper($0)}'<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">ABABAB<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">\u00a0 \u00a0ABABAB<\/span><\/strong><br \/>\n<span style=\"color: #ff0000;\"><strong>\u00a0 \u00a0ababab<\/strong><\/span><br \/>\n<strong><span style=\"color: #ff0000;\">\u00a0 \u00a0 \u00a0ABABAB<\/span><\/strong><\/p>\n<p>Nothing from the first awk gets printed in the 2nd version..like the loop does not complete for 1st awk ?<\/p>\n<p><span style=\"color: #0000ff;\">cat ABABAB.txt | awk -e '{print tolower($0)}'<\/span><br \/>\nababab<\/p>\n<p><span style=\"color: #0000ff;\">cat ABABAB.txt | awk \u00a0'{print tolower($0)}' | awk \u00a0'{print toupper($0)}'<\/span><br \/>\n<span style=\"color: #ff0000;\">ABABAB<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{if (gsub(\/B\/,\"\\v\")) {print}}' &lt; ABABAB.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">A<\/span><br \/>\n<span style=\"color: #ff0000;\">\u00a0 A<\/span><br \/>\n<span style=\"color: #ff0000;\">\u00a0 \u00a0 A<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{if (gsub(\/[A-Z]\/, \"1\")) {print}}' &lt; ABABAB.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">111111<\/span><\/p>\n<p><span style=\"color: #00ccff;\"><em>\"Sed offers capabilities that seem a natural extension of interactive text editing. For instance, it offers a<\/em><\/span><br \/>\n<span style=\"color: #00ccff;\"><em>search-and-replace facility that can be applied globally to a single file or a group of files. While you<\/em><\/span><br \/>\n<span style=\"color: #00ccff;\"><em>would not typically use sed to change a term that appears once in a particular file, you will find it very<\/em><\/span><br \/>\n<span style=\"color: #00ccff;\"><em>useful to make a series of changes across a number of files. Think about making 20 different edits in<\/em><\/span><br \/>\n<span style=\"color: #ffffff;\"><em><span style=\"color: #00ccff;\">over 100 files in a matter of minutes, and you get an idea of how powerful sed can be.\" <\/span><strong><span style=\"color: #00ccff;\">E.G. Subs \"In Place\" for a mysql DB to change you web name for home server name globally - BACK IT UP first!!<\/span>\u00a0<\/strong><\/em><\/span><\/p>\n<p>sed -i 's\/https:\/http:\/g' NEO.sql<\/p>\n<p>Or tr and perl are better\u00a0to use when learning concepts, as sed and awk don't work in the same simple search and replace format for \"\\n\" type chars in Mint.<\/p>\n<p>This seems to be what paste -s uses anyway, so you could also use 1 white space \" \" instead of a tab \\t, also:<\/p>\n<p><span style=\"color: #0000ff;\">arp -a | awk '{print $2}' | sed 's\/^.\/\/g' | sed 's\/.$\/\/g' | tr \"\\n\" \"\\t\"<\/span><br \/>\n<span style=\"color: #ff0000;\">192.168.1.2 192.168.1.7 192.168.1.1 192.168.1.3 192.168.1.8 192.168.1.4 192.168.1.5<\/span><\/p>\n<p><span style=\"color: #0000ff;\">arp -a | awk '{print $2}' | sed 's\/^.\/\/g' | sed 's\/.$\/\/g' | perl -pe 's\/\\n\/\\t\/g'<\/span><br \/>\n<span style=\"color: #ff0000;\">192.168.1.2 192.168.1.7 192.168.1.1 192.168.1.3 192.168.1.8 192.168.1.4 192.168.1.5<\/span><\/p>\n<p><strong>sed and awk fail for this simple format requiring more cryptic parameters:<\/strong><\/p>\n<p><span style=\"color: #0000ff;\">arp -a | awk '{print $2}' | sed 's\/^.\/\/g' | sed 's\/.$\/\/g' | <strong>sed 's\/\\n\/\\t\/g'<\/strong><\/span><br \/>\n<span style=\"color: #ff0000;\">192.168.1.2<\/span><br \/>\n<span style=\"color: #ff0000;\">192.168.1.7...<\/span><\/p>\n<p><span style=\"color: #0000ff;\">arp -a | awk '{print $2}' | sed 's\/^.\/\/g' | sed 's\/.$\/\/g' | <strong>sed '{:q;N;s\/\\n\/ \/g;t q}'<\/strong><\/span><br \/>\n<span style=\"color: #ff0000;\">192.168.1.2 192.168.1.7 192.168.1.1 192.168.1.3 192.168.1.8 192.168.1.4 192.168.1.5<\/span><\/p>\n<p><span style=\"color: #0000ff;\">arp -a | awk '{print $2}' | sed 's\/^.\/\/g' | sed 's\/.$\/\/g' | awk 'gsub(\"\\n\",\"\\t\")'<\/span><\/p>\n<p><span style=\"color: #ff0000;\">no OP<\/span><\/p>\n<p><span style=\"color: #0000ff;\">arp -a | awk '{print $2}' | sed 's\/^.\/\/g' | sed 's\/.$\/\/g' | awk '{printf \"%s \",$0} END {print \"\"}'<\/span><br \/>\n<span style=\"color: #ff0000;\">192.168.1.2 192.168.1.7 192.168.1.1 192.168.1.3 192.168.1.8 192.168.1.4 192.168.1.5<\/span><\/p>\n<p>OR<\/p>\n<p><span style=\"color: #0000ff;\">arp -a | awk '{print $2}' | sed 's\/^.\/\/g' | sed 's\/.$\/\/g' | awk 'BEGIN {RS=\"\"}{gsub(\/\\n\/,\" \",$0); print $0}'<\/span><br \/>\n<span style=\"color: #ff0000;\">192.168.1.2 192.168.1.7 192.168.1.1 192.168.1.3 192.168.1.8 192.168.1.4 192.168.1.5<\/span><\/p>\n<p>Perl evaluation\/substitution of integers:<\/p>\n<p><span style=\"color: #0000ff;\">perl -pe 's\/(\\d+)$\/\"=\"x$1\/e' 1_5.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">=<\/span><br \/>\n<span style=\"color: #ff0000;\">==<\/span><br \/>\n<span style=\"color: #ff0000;\">===<\/span><br \/>\n<span style=\"color: #ff0000;\">====<\/span><br \/>\n<span style=\"color: #ff0000;\">=====<\/span><\/p>\n<p>Oh look! It was right in front of me all the time above, adding print!! Dick...<\/p>\n<p><span style=\"color: #0000ff;\">perl -pe 's\/(\\d+)$\/\"=\"x$1\/e; print \"$1 \"' 1_5.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">1 =<\/span><br \/>\n<span style=\"color: #ff0000;\">2 ==<\/span><br \/>\n<span style=\"color: #ff0000;\">3 ===<\/span><br \/>\n<span style=\"color: #ff0000;\">4 ====<\/span><br \/>\n<span style=\"color: #ff0000;\">5 =====<\/span><\/p>\n<p>Does it work for diff values on diff lines..? Yep!<\/p>\n<p><span style=\"color: #0000ff;\">cat 6_1.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">6<\/span><br \/>\n<span style=\"color: #ff0000;\">5<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">3<\/span><br \/>\n<span style=\"color: #ff0000;\">2<\/span><br \/>\n<span style=\"color: #ff0000;\">1<\/span><\/p>\n<p><span style=\"color: #0000ff;\">perl -pe 's\/(\\d+)$\/\"=\"x$1\/e; print \"$1 \"' 6_1.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">6 ======<\/span><br \/>\n<span style=\"color: #ff0000;\">5 =====<\/span><br \/>\n<span style=\"color: #ff0000;\">4 ====<\/span><br \/>\n<span style=\"color: #ff0000;\">3 ===<\/span><br \/>\n<span style=\"color: #ff0000;\">2 ==<\/span><br \/>\n<span style=\"color: #ff0000;\">1 =<\/span><\/p>\n<p><span style=\"color: #0000ff;\">perl -pe 's\/(\\d+)$\/\"=\"x$1\/e; print \"$1 \"' random.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">90 ======<\/span><br \/>\n<span style=\"color: #ff0000;\">87 ===<\/span><br \/>\n<span style=\"color: #ff0000;\">74 ==================<\/span><br \/>\n<span style=\"color: #ff0000;\">40 ============<\/span><br \/>\n<span style=\"color: #ff0000;\">90 ======<\/span><br \/>\n<span style=\"color: #ff0000;\">70 ==============<\/span><br \/>\n<span style=\"color: #ff0000;\">46 ==================<\/span><br \/>\n<span style=\"color: #ff0000;\">10 ==========<\/span><br \/>\n<span style=\"color: #ff0000;\">35 =======<\/span><br \/>\n<span style=\"color: #ff0000;\">08 ========<\/span><br \/>\n<span style=\"color: #ff0000;\">1 =<\/span><br \/>\n<span style=\"color: #ff0000;\">0<\/span><\/p>\n<p>Remember - load avg is divided by cores to get value per core (avg of 4 of a localhost core = 4\/4=100% utilization per core - PC blows up and you die...)<\/p>\n<p><span style=\"color: #0000ff;\">uptime<\/span><br \/>\n23:56:19 up 9:20, 5 users, load <span style=\"color: #ff0000;\"><strong>average: 0.88,<\/strong><\/span> 0.95, 0.81<\/p>\n<p><span style=\"color: #0000ff;\">perl -e 'while(1) {`uptime` <strong>=~ \/average: ([\\d.]+)\/<\/strong>; printf(\"% 5s %s\\n\", $1, \"#\" x ($1 * 10)); sleep 3 }'<\/span><\/p>\n<p><span style=\"color: #ff0000;\">0.87 ##<\/span><br \/>\n<span style=\"color: #ff0000;\">0.87 ##<\/span><br \/>\n<span style=\"color: #ff0000;\">0.88 ##<\/span><br \/>\n<span style=\"color: #ff0000;\">0.97 <\/span><br \/>\n<span style=\"color: #ff0000;\">0.97 <\/span><br \/>\n<span style=\"color: #ff0000;\">1.21 <\/span><br \/>\n<span style=\"color: #ff0000;\">1.21 <\/span><br \/>\n<span style=\"color: #ff0000;\">1.11 ##<\/span><br \/>\n<span style=\"color: #ff0000;\">1.10 ##<\/span><br \/>\n<span style=\"color: #ff0000;\">1.10 ##<\/span><br \/>\n<span style=\"color: #ff0000;\">1.10 ##<\/span><br \/>\n<span style=\"color: #ff0000;\">1.10 ##<\/span><br \/>\n<span style=\"color: #ff0000;\">1.01 #<\/span><br \/>\n<span style=\"color: #ff0000;\">0.93 <\/span><br \/>\n<span style=\"color: #ff0000;\">0.93 <\/span><br \/>\n<span style=\"color: #ff0000;\">0.85 ##<\/span><br \/>\n<span style=\"color: #ff0000;\">0.85 ##<\/span><\/p>\n<p>For these types of single line histos, increase the multiplier for clearer proportions, accounting for decimal:<\/p>\n<p><span style=\"color: #0000ff;\">perl -e 'while(1) {`uptime` =~ \/average: ([\\d.]++)\/; printf(\"% 5s %s\\n\", $1, \"#\" x ($1 * 100)); sleep 3 }'<\/span><br \/>\n<span style=\"color: #ff0000;\">0.91 #<\/span><br \/>\n<span style=\"color: #ff0000;\">0.92 ##<\/span><br \/>\n<span style=\"color: #ff0000;\">0.92 ##<\/span><br \/>\n<span style=\"color: #ff0000;\">0.84 <\/span><br \/>\n<span style=\"color: #ff0000;\">0.84 <\/span><br \/>\n<span style=\"color: #ff0000;\">0.78 <\/span><br \/>\n<span style=\"color: #ff0000;\">0.71 ##<\/span><\/p>\n<p>Perl matches:<\/p>\n<p><em>\".\" =~ \/.\/ # Match<\/em><\/p>\n<p><em>\" \" =~ \/\\h\/ # Match, space is horizontal whitespace.<\/em><\/p>\n<p><em><strong>\\d Match a decimal digit character.<\/strong><\/em><br \/>\n<em>\\D Match a non-decimal-digit character.<\/em><br \/>\n<em>\\w Match a \"word\" character.<\/em><br \/>\n<em>\\W Match a non-\"word\" character.<\/em><br \/>\n<em><strong>\\s Match a whitespace character.<\/strong><\/em><br \/>\n<em>\\S Match a non-whitespace character.<\/em><br \/>\n<em>\\h Match a horizontal whitespace character.<\/em><br \/>\n<em>\\H Match a character that isn't horizontal whitespace.<\/em><br \/>\n<em>\\v Match a vertical whitespace character.<\/em><br \/>\n<em>\\V Match a character that isn't vertical whitespace.<\/em><br \/>\n<em>\\N Match a character that isn't a newline.<\/em><br \/>\n<em>\\pP, \\p{Prop} Match a character that has the given Unicode property.<\/em><br \/>\n<em>\\PP, \\P{Prop} Match a character that doesn't have the Unicode property<\/em><\/p>\n<p>Trying to work out this cryptic perl lang...from the above, which takes a number from a matching column, from ONE line only from each loop, and multiplies the digit value by 10, then prints that number of # chars...So...<\/p>\n<p>For a generic data file with<strong> 1 line only<\/strong>, without the while\/sleep to match digits, amend the \u00a0\"<strong>=~\u00a0\/avg: ([\\d.])\/\" ; <\/strong>to read the correct column digits. The\u00a0<strong>$1, \"#\" x ($1 * 10) \u00a0<\/strong>- reads\/prints column 1 (digit), then multiplies by 10, the \"#\" char, by the actual digit value, ignoring the decimal fractions:<\/p>\n<p><span style=\"color: #0000ff;\">cat load.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">load average: <strong>1.15<\/strong>, 0.92, 0.57<\/span><\/p>\n<p><span style=\"color: #0000ff;\">perl -e '{`cat load.txt` =~ \/<strong>([\\d.]+)\/<\/strong>; printf(\"% 5s %s\\n\", $1, \"#\" x ($1 * 10)) }'<\/span><br \/>\n<span style=\"color: #ff0000;\"><strong>1.15<\/strong> ##<\/span><\/p>\n<p>e.g:<\/p>\n<p><span style=\"color: #0000ff;\">perl -e '{`cat load.txt` =~ \/$x ([\\d.]+)\/; printf(\"% 5s %s\\n\", $1, \"#\" x ($1 * 10)) }'<\/span><br \/>\n<span style=\"color: #ff0000;\">1.15 ##<\/span><\/p>\n<p><span style=\"color: #0000ff;\">perl -pe '{}' 1_5.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">1<\/span><br \/>\n<span style=\"color: #ff0000;\">2<\/span><br \/>\n<span style=\"color: #ff0000;\">3<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">5<\/span><\/p>\n<p>Can't get it to do more than one line of the 5...unlike sed\/awk, perl does not process multilines it seems...without while(&lt;&gt;)<\/p>\n<p><span style=\"color: #0000ff;\">awk '{print}' 1_5.txt | perl -e '{\/^\/ =~ \/([\\d.]+)\/; printf(\"% 5s %s\\n\", $1, \"#\" x ($1 * 10)) }'<\/span><br \/>\n<span style=\"color: #ff0000;\">1 #<\/span><\/p>\n<p>Aah...awk histo for cmd history - nice....<\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++}END{for(i in h){print h[i],i|\"sort -rn|head -20\"}}' |awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\nsudo 194<br \/>\nls 151 ##<br \/>\ncat 97<br \/>\nfortune 57<br \/>\nperl 47<br \/>\nawk 41 #<br \/>\nfind 38<br \/>\ndu 35 ##<br \/>\nvi 25 ##<br \/>\nrsync 23 ##<br \/>\ncd 23 ##<br \/>\nman 19<br \/>\ncrontab 17<br \/>\nrm 16 ##<br \/>\nprintf 16 ##<br \/>\nmplayer 14 ##<br \/>\ndf 13 ##<br \/>\nfor 11 #<br \/>\nalias 10 #<br \/>\nx11vnc 9<\/p>\n<p><span style=\"color: #0000ff;\">awk '{print}' 1_5.txt | perl -e 'while(&lt;&gt;){ \/^\/=~\/(\\d)\/; printf(\"% 5s %s\\n\", $1, \"#\" x ($1 * 10))}'<\/span><br \/>\n<span style=\"color: #ff0000;\">1 #<\/span><br \/>\n<span style=\"color: #ff0000;\">1 #<\/span><br \/>\n<span style=\"color: #ff0000;\">1 #<\/span><br \/>\n<span style=\"color: #ff0000;\">1 #<\/span><br \/>\n<span style=\"color: #ff0000;\">1 #<\/span><\/p>\n<hr \/>\n<p>grades.txt<\/p>\n<p>john 85 92 78 94 88<br \/>\nandrea 89 90 75 90 86<br \/>\njasper 84 88 80 92 84<\/p>\n<p>awk.scr<\/p>\n<p><span style=\"color: #ff0000;\"># average five grades<\/span><br \/>\n<span style=\"color: #ff0000;\">{ total = $2 + $3 + $4 + $5 + $6<\/span><br \/>\n<span style=\"color: #ff0000;\">avg = total \/ 5<\/span><br \/>\n<span style=\"color: #ff0000;\">print $1, avg }<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -f awkscr grades.txt<\/span><br \/>\njohn 87.4<br \/>\nandrea 86<br \/>\njasper 85.6<\/p>\n<p>change:<\/p>\n<p># average five grades<br \/>\n<span style=\"color: #ff0000;\">{print NR \".\", $1, avg }<\/span><br \/>\n<span style=\"color: #ff0000;\">END {<\/span><br \/>\n<span style=\"color: #ff0000;\">print \"\"<\/span><br \/>\n<span style=\"color: #ff0000;\">print NR, \"records processed.\" }<\/span><\/p>\n<p>gives:<\/p>\n<p><span style=\"color: #0000ff;\">awk -f awkscr grades.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">1. john <\/span><br \/>\n<span style=\"color: #ff0000;\">2. andrea <\/span><br \/>\n<span style=\"color: #ff0000;\">3. jasper<\/span><\/p>\n<p><span style=\"color: #ff0000;\">3 records processed.<\/span><\/p>\n<p>make OFS a tab:<\/p>\n<p>BEGIN { OFS = \"\\t\" }<br \/>\n{print NR \".\", $1, avg }<br \/>\nEND {<br \/>\nprint \"\"<br \/>\nprint NR, \"records processed.\" }<\/p>\n<p><span style=\"color: #0000ff;\">awk -f awkscr grades.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">1. \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0john<\/span><br \/>\n<span style=\"color: #ff0000;\">2.\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0andrea<\/span><br \/>\n<span style=\"color: #ff0000;\">3. \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0jasper<\/span><\/p>\n<p><span style=\"color: #ff0000;\">3 records processed.<\/span><\/p>\n<p>combine all grades file options:<\/p>\n<p>BEGIN { OFS = \"\\t\" }<br \/>\n{total = $2 + $3 + $4 + $5 + $6<br \/>\navg = total \/ 5<br \/>\nprint NR \".\", $1, avg }<br \/>\nEND {<br \/>\nprint \"\"<br \/>\nprint NR, \"records processed.\" }<\/p>\n<p><span style=\"color: #0000ff;\">awk -f awkscr grades.txt<\/span><\/p>\n<p><span style=\"color: #ff0000;\">1. \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0john 87.4<\/span><br \/>\n<span style=\"color: #ff0000;\">2.\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0andrea 86<\/span><br \/>\n<span style=\"color: #ff0000;\">3. \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0jasper 85.6<\/span><\/p>\n<p><span style=\"color: #ff0000;\">3 records processed.<\/span><\/p>\n<p>For file presidents.txt of text:<\/p>\n<p>John Kennedy<br \/>\nLyndon B. Johnson<br \/>\nRichard Milhouse Nixon<br \/>\nGerald R. Ford<br \/>\nJimmy Carter<br \/>\nRonald Reagan<br \/>\nGeorge Bush<br \/>\nBill Clinton<\/p>\n<p>If above awkscr is run:<\/p>\n<p><span style=\"color: #0000ff;\">awk -f awkscr presidents.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">1. John 0<\/span><br \/>\n<span style=\"color: #ff0000;\">2. Lyndon 0<\/span><br \/>\n<span style=\"color: #ff0000;\">3. Richard 0<\/span><br \/>\n<span style=\"color: #ff0000;\">4. Gerald 0<\/span><br \/>\n<span style=\"color: #ff0000;\">5. Jimmy 0<\/span><br \/>\n<span style=\"color: #ff0000;\">6. Ronald 0<\/span><br \/>\n<span style=\"color: #ff0000;\">7. George 0<\/span><br \/>\n<span style=\"color: #ff0000;\">8. Bill 0<\/span><\/p>\n<p><span style=\"color: #ff0000;\">8 records processed.<\/span><\/p>\n<p>Amending for removal of avgs and finding OFS\u00a0number of fields:<\/p>\n<p><span style=\"color: #ff0000;\">BEGIN { <strong>OFS = \"\\t\"<\/strong> }<\/span><br \/>\n<span style=\"color: #ff0000;\">END {<\/span><br \/>\n<span style=\"color: #ff0000;\">print NF, \"fields present.\"<\/span><br \/>\n<span style=\"color: #ff0000;\">print NR, \"records processed.\" }<\/span><\/p>\n<p>awk -f awkscr presidents.txt<br \/>\n<span style=\"color: #ff0000;\">2 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0fields present.<\/span><br \/>\n<span style=\"color: #ff0000;\">8 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0records processed.<\/span><\/p>\n<p>But are there only 2 real fields? No, as for the spaces between Lyndon B. Johnson, Ford and Milhouse - OFS has set the field, not the text content:<\/p>\n<p>awk ' {print NF, $0}' presidents.txt<\/p>\n<p><span style=\"color: #ff0000;\">2 John Kennedy<br \/>\n3 Lyndon B. Johnson<br \/>\n3 Richard Milhouse Nixon<br \/>\n3 Gerald R. Ford<br \/>\n2 Jimmy Carter<br \/>\n2 Ronald Reagan<br \/>\n2 George Bush<br \/>\n2 Bill Clinton<\/span><\/p>\n<p>You can find the text's no of fields for each record (which tells of a middle name or letter), with the last name (from the last field) with a total of records processed with just:<\/p>\n<p>{print $NF<br \/>\nprint NF, \"fields present.\"<br \/>\nprint NR, \"records processed.\" }<\/p>\n<p>awk -f awkscr presidents.txt<br \/>\n<span style=\"color: #ff0000;\">Kennedy<\/span><br \/>\n<span style=\"color: #ff0000;\">2 fields present.<\/span><br \/>\n<span style=\"color: #ff0000;\">1 records processed.<\/span><br \/>\n<strong><span style=\"color: #ff0000;\">Johnson<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">3 fields present.<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">2 records processed.<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">Nixon<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">3 fields present.<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">3 records processed.<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">Ford<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">3 fields present.<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">4 records processed.<\/span><\/strong><br \/>\n<span style=\"color: #ff0000;\">Carter<\/span><br \/>\n<span style=\"color: #ff0000;\">2 fields present.<\/span><br \/>\n<span style=\"color: #ff0000;\">5 records processed.<\/span><br \/>\n<span style=\"color: #ff0000;\">Reagan<\/span><br \/>\n<span style=\"color: #ff0000;\">2 fields present.<\/span><br \/>\n<span style=\"color: #ff0000;\">6 records processed.<\/span><br \/>\n<span style=\"color: #ff0000;\">Bush<\/span><br \/>\n<span style=\"color: #ff0000;\">2 fields present.<\/span><br \/>\n<span style=\"color: #ff0000;\">7 records processed.<\/span><br \/>\n<span style=\"color: #ff0000;\">Clinton<\/span><br \/>\n<span style=\"color: #ff0000;\">2 fields present.<\/span><br \/>\n<span style=\"color: #ff0000;\">8 records processed.<\/span><\/p>\n<p>Or for explicit clarity:<\/p>\n<p><span style=\"color: #0000ff;\">awk ' {print $0\" \" NF \" fields present\" ; print NR, \"records processed.\" }' presidents.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">John Kennedy 2 fields present<\/span><br \/>\n<span style=\"color: #ff0000;\">1 records processed.<\/span><br \/>\n<span style=\"color: #ff0000;\">Lyndon B. Johnson 3 fields present<\/span><br \/>\n<span style=\"color: #ff0000;\">2 records processed.<\/span><br \/>\n<span style=\"color: #ff0000;\">Richard Milhouse Nixon 3 fields present<\/span><br \/>\n<span style=\"color: #ff0000;\">3 records processed.<\/span><br \/>\n<span style=\"color: #ff0000;\">Gerald R. Ford 3 fields present<\/span><br \/>\n<span style=\"color: #ff0000;\">4 records processed.<\/span><br \/>\n<span style=\"color: #ff0000;\">Jimmy Carter 2 fields present<\/span><br \/>\n<span style=\"color: #ff0000;\">5 records processed.<\/span><br \/>\n<span style=\"color: #ff0000;\">Ronald Reagan 2 fields present<\/span><br \/>\n<span style=\"color: #ff0000;\">6 records processed.<\/span><br \/>\n<span style=\"color: #ff0000;\">George Bush 2 fields present<\/span><br \/>\n<span style=\"color: #ff0000;\">7 records processed.<\/span><br \/>\n<span style=\"color: #ff0000;\">Bill Clinton 2 fields present<\/span><br \/>\n<span style=\"color: #ff0000;\">8 records processed.<\/span><\/p>\n<p>Instead of having all the information on one line, the person's\u00a0name is on one line, followed by the company's name on the next line and so on. Here's a sample record:<br \/>\nJohn Robinson<br \/>\nKoren Inc.<br \/>\n978 Commonwealth Ave.<br \/>\nBoston<br \/>\nMA 01760<br \/>\n696-0987<br \/>\nThis file has 6 records, or you can look at it as a single user record that has six fields where a blank line separates each field.\u00a0To process this data, we can specify a multi line record by defining the field separator to be a newline,\u00a0represented as \"\\n\", and set the record separator to the empty string (RS = \"\") which stands for a blank line. print gives the 1st and last records ($NF=$6 here).<br \/>\nBEGIN { FS = \"\\n\"; RS = \"\" }<br \/>\nWe can print the first and last fields using the following script:<br \/>\n# block.awk - print first and last fields<br \/>\n# $1 = name; $NF = phone number<\/p>\n<p><span style=\"color: #ff0000;\">BEGIN { FS = \"\\n\"; RS = \"\" }<\/span><br \/>\n<span style=\"color: #ff0000;\">{ print $1, $NF }<\/span><\/p>\n<p><span style=\"color: #0000ff;\">vi multiline_record.txt<\/span><\/p>\n<p>John Robinson<br \/>\nKoren Inc.<br \/>\n978 Commonwealth Ave.<br \/>\nBoston<br \/>\nMA 01760<br \/>\n696-0987<\/p>\n<p><span style=\"color: #0000ff;\">vi block.awk<\/span><\/p>\n<p># block.awk - print first and last fields<br \/>\n# $1 = name; $NF = phone number<\/p>\n<p>BEGIN { FS = \"\\n\"; RS = \"\" }<br \/>\n{ print $1, $NF }<\/p>\n<p><span style=\"color: #0000ff;\">awk -f block.awk multiline_record.txt \u00a0 \u00a0<\/span><\/p>\n<p><span style=\"color: #ffffff;\">BEGIN { FS = \"\\n\"; RS = \"\" }<br \/>\n{ print $1, $NF }<\/span><\/p>\n<p><span style=\"color: #ff0000;\">John Robinson 696-0987<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{ FS = \"\\n\"; ORS = \" \"; print }' multiline_record.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">John Robinson Koren Inc. 978 Commonwealth Ave. Boston MA 01760 696-0987 stevee@AMDA8 ~\/Awk $<\/span><\/p>\n<hr \/>\n<p>A key ability of awk is to set field separators of your choice, which then determine how many fields the file is read as. If a file (names.txt) contains:<\/p>\n<p><span style=\"color: #ff0000;\">John Rink,Boob Inc,2 Tities street,Boston,MA 666-555-1111<\/span><br \/>\n<span style=\"color: #ff0000;\">John Robinson,Koren Inc.,978 4th Ave.,Boston,MA 01760,696-0987<\/span><br \/>\n<span style=\"color: #ff0000;\">Phyllis Chapman,GVE Corp.,34 Sea Drive,Amesbury,MA 01881,879-0900<\/span><br \/>\n<span style=\"color: #ff0000;\">Phyllis Man,GVE Corp.,34 Sea Drive,Amesbury,MA 707-724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">Phil Thap,GVE Corp.,34 Sea Drive,Amesbury,MA (707) 724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">(Phyllis Chap,GVE Corp.,34 Sea Drive,Amesbury,MA (707)724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">Phyllis Apeman,GVE Corp.,34 Sea Drive,Amesbury,MA 1-707-724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">Phyllis Chipmunk,GVE Corp.,34 Sea Drive,Amesbury,MA1 707-724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">Phyllis Shaman,GVE Corp.,34 Sea Drive,Amesbury,MA 1(707)724-0000<\/span><\/p>\n<p>You could choose to separate on say, commas, TABS (\\t), single letters, any number range [0-9] etc.<\/p>\n<p>This decides which column number would need stating to print out by amending the script column $x, say for script\u00a0phonelist.awk, with prior commented out examples, but now separates by a single space \" \" so \"creates\" up to 7 columns, depending on record (line) content that print $6 will show from the \"names\" data file:<\/p>\n<p><span style=\"color: #0000ff;\">vi phonelist.awk<\/span><\/p>\n<p># phonelist.awk -- print name and phone number.<br \/>\n# input file -- name, company, street, city, state and zip,phone<br \/>\n#BEGIN { FS = \",\" }<br \/>\n# comma-delimited fields<br \/>\n#{ print $1 \", \" $6 }<br \/>\n#$5 !~ \/MA\/ { print $1 \", \" $6 }<br \/>\n#$6 !~ \/1?(-| )?\\(?[0-9]+\\)?( |-)?[0-9]+-[0-9]+\/<\/p>\n<p><span style=\"color: #ff0000;\">BEGIN { FS = \" \" } # delimited fields<\/span><br \/>\n<span style=\"color: #ff0000;\">{ print $1,\": \" $NF }<\/span><br \/>\n<span style=\"color: #ff0000;\">END {<\/span><br \/>\n<span style=\"color: #ff0000;\">print \"\"<\/span><br \/>\n<span style=\"color: #ff0000;\">print NR, \"records processed. \\n\" }<\/span><\/p>\n<p>using phonelist.awk, becomes:<\/p>\n<p><span style=\"color: #0000ff;\">awk -f phonelist.awk names.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">John : 666-555-1111<\/span><br \/>\n<span style=\"color: #ff0000;\">John : 01760,696-0987<\/span><br \/>\n<span style=\"color: #ff0000;\">Phyllis : 879-0900<\/span><br \/>\n<span style=\"color: #ff0000;\">Phyllis : 707-724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">Phil : 724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">(Phyllis : (707)724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">Phyllis : 1-707-724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">Phyllis : 1707-724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">Phyllis : 1(707)724-0000<\/span><\/p>\n<p><span style=\"color: #ff0000;\">9 records processed.<\/span><\/p>\n<p>PRINT FIRST\/LAST RECORD (line) ONLY:<\/p>\n<p>efficient - doesn't read whole file:<\/p>\n<p><span style=\"color: #0000ff;\">awk 'NR==1 {print; exit}' presidents.txt<\/span><\/p>\n<p>John Kennedy<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if(NR&lt;=1) print}' presidents.txt<\/span><br \/>\nJohn Kennedy<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if(NR&lt;2) print}' presidents.txt<\/span><br \/>\nJohn Kennedy<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if(NR&gt;7) print}' presidents.txt<\/span><br \/>\nBill Clinton<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if(NR&gt;=8) print}' presidents.txt<\/span><br \/>\nBill Clinton<\/p>\n<p><span style=\"color: #0000ff;\">awk 'END{print}' presidents.txt<\/span><br \/>\nBill Clinton<\/p>\n<p><span style=\"color: #0000ff;\">awk -f phonelist.awk names.txt<\/span><br \/>\nJohn, 666-555-1111<br \/>\nJohn, 01760,696-0987<br \/>\nPhyllis, 01881,879-0900<br \/>\nPhyllis, 707-724-0000<br \/>\nPhil, (707)<br \/>\n(Phyllis, (707)724-0000<br \/>\nPhyllis, 1-707-724-0000<br \/>\nPhyllis, 707-724-0000<br \/>\nPhyllis, 1(707)724-0000<\/p>\n<p>9 records processed.<\/p>\n<p>It you chose a TAB (\\t) then only 2 columns can be created for output as the phone numbers are tab delimited in this file:<\/p>\n<p><span style=\"color: #ff0000;\">BEGIN { FS = <strong>\"\\t\"<\/strong> } # comma-delimited fields<\/span><br \/>\n<span style=\"color: #ff0000;\">{ print <strong>$2<\/strong> }<\/span><br \/>\n<span style=\"color: #ff0000;\">END {<\/span><br \/>\n<span style=\"color: #ff0000;\">print \"\"<\/span><br \/>\n<span style=\"color: #ff0000;\">print NR, \"records processed.\" }<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -f phonelist.awk names<\/span><br \/>\n666-555-1111<br \/>\n01760,696-0987<br \/>\n01881,879-0900<br \/>\n707-724-0000<br \/>\n(707) 724-0000<br \/>\n(707)724-0000<br \/>\n1-707-724-0000<br \/>\n707-724-0000<br \/>\n1(707)724-0000<\/p>\n<p>9 records processed.<\/p>\n<p>For the example of names.txt, which has FS = \",\" a comma, find how many fields each record has (count the commas and add 1 eg 5 commas = 6 columns ) :<\/p>\n<p><span style=\"color: #0000ff;\">cat names.txt<\/span><br \/>\nJohn Rink,Boob Inc,2 Tities street,Boston,MA,666-555-1111<br \/>\nJohn Robinson,Koren Inc.,978 4th Ave.,Boston,MA,01760,696-0987<br \/>\nPhyllis Chapman,GVE Corp.,34 Sea Drive,Amesbury,MA,01881,879-0900<br \/>\nPhyllis Man,GVE Corp.,34 Sea Drive,Amesbury,MA,707-724-0000<br \/>\nPhil Thap,GVE Corp.,34 Sea Drive,Amesbury,MA,(707) 724-0000<br \/>\n(Phyllis Chap,GVE Corp.,34 Sea Drive,Amesbury,MA,(707)724-0000<br \/>\nPhyllis Apeman,GVE Corp.,34 Sea Drive,Amesbury,MA,1-707-724-0000<br \/>\nPhyllis Chipmunk,GVE Corp.,34 Sea Drive,Amesbury,MA,1707-724-0000<br \/>\nPhyllis Shaman,GVE Corp.,34 Sea Drive,Amesbury,MA,1(707)724-0000<\/p>\n<p>Combining presidents example from above and showing no. of\u00a0space separated fields (space is default):<\/p>\n<p><span style=\"color: #0000ff;\">awk ' {print $0\"\\n\" NF \" fields present\" ; print NR, \"records processed.\" }' names.txt<\/span><br \/>\nJohn Rink,Boob Inc,2 Tities street,Boston,MA,666-555-1111<br \/>\n<strong>5 fields present<\/strong><br \/>\n1 records processed.<br \/>\nJohn Robinson,Koren Inc.,978 4th Ave.,Boston,MA,01760,696-0987<br \/>\n<strong>5 fields present<\/strong><br \/>\n2 records processed.<br \/>\nPhyllis Chapman,GVE Corp.,34 Sea Drive,Amesbury,MA,01881,879-0900<br \/>\n5 fields present<br \/>\n3 records processed.<br \/>\nPhyllis Man,GVE Corp.,34 Sea Drive,Amesbury,MA,707-724-0000<br \/>\n5 fields present<br \/>\n4 records processed.<br \/>\nPhil Thap,GVE Corp.,34 Sea Drive,Amesbury,MA,(707) 724-0000<br \/>\n<strong>6 fields present<\/strong><br \/>\n5 records processed.<br \/>\n(Phyllis Chap,GVE Corp.,34 Sea Drive,Amesbury,MA,(707)724-0000<br \/>\n5 fields present<br \/>\n6 records processed.<br \/>\nPhyllis Apeman,GVE Corp.,34 Sea Drive,Amesbury,MA,1-707-724-0000<br \/>\n5 fields present<br \/>\n7 records processed.<br \/>\nPhyllis Chipmunk,GVE Corp.,34 Sea Drive,Amesbury,MA,1707-724-0000<br \/>\n5 fields present<br \/>\n8 records processed.<br \/>\nPhyllis Shaman,GVE Corp.,34 Sea Drive,Amesbury,MA,1(707)724-0000<br \/>\n5 fields present<br \/>\n9 records processed.<\/p>\n<p>Made clearer if I\u00a0replace commas with comma and space for first record and try again:<\/p>\n<p>John Rink, Boob Inc, 2 Tities street, Boston, MA, 666-555-1111<\/p>\n<p><span style=\"color: #0000ff;\">awk ' {print $0\"\\n\" NF \" fields present\" ; print NR, \"records processed.\" }' names.txt | head -3<\/span><br \/>\n<span style=\"color: #ff0000;\">John Rink, Boob Inc, 2 Tities street, Boston, MA, 666-555-1111<\/span><br \/>\n<strong><span style=\"color: #ff0000;\">10 fields present<\/span><\/strong><br \/>\n<span style=\"color: #ff0000;\">1 records processed.<\/span><\/p>\n<p>Lesson there is keep clear records with a \", \" or \": \" or similar for ease of NR. Names will be auto split to first and last name fields by a space anyway too.\u00a0For CSV files, need to set FS or OFS =\",\"<\/p>\n<p><span style=\"color: #0000ff;\">awk ' {<strong>FS=\",\" ;<\/strong> print $0\"\\n\" NF \" fields present\" ; print NR, \"records processed.\" }' names.txt<\/span><br \/>\nJohn Robinson,Koren Inc.,978 4th Ave.,Boston,MA,01760,696-0987<br \/>\n<strong>7 fields present<\/strong><\/p>\n<p>Add lines from file1 to file 2 example.<\/p>\n<p>- remember - need to feed to &gt; newfile.txt for a new copy, and remove the n from cat to remove the numbers:<\/p>\n<p><span style=\"color: #0000ff;\">awk '<strong>NR%2 == 1<\/strong> {getline f2 &lt; \"presidents.txt\"; print f2} 1' names.txt | cat -n<\/span><\/p>\n<p><span style=\"color: #ff0000;\">1 John Kennedy<\/span><br \/>\n<span style=\"color: #ff0000;\">2 John Rink, Boob Inc, 2 Tities street, Boston, MA, 666-555-1111<\/span><br \/>\n<span style=\"color: #ff0000;\">3 John Robinson, Koren Inc., 978, 4th Ave., Boston, MA, 01760,696-0987<\/span><br \/>\n<span style=\"color: #ff0000;\">4 Phyllis Chapman, GVE Corp., 34 Sea Drive, Amesbury, MA, 01881 879-0900<\/span><br \/>\n<span style=\"color: #ff0000;\">5 Phyllis Man, GVE Corp., 34 Sea Drive, Amesbury, MA, 707-724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">6 Phil Thap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707) 724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">7 (Phyllis Chap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707)724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">8 Phyllis Apeman, GVE Corp., 34 Sea Drive, Amesbury, MA, 1-707-724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">9 Phyllis Chipmunk, GVE Corp., 34 Sea Drive, Amesbury, MA, 1707-724-0000<\/span><br \/>\n<span style=\"color: #ff0000;\">10 Lyndon B. Johnson<\/span><br \/>\n<span style=\"color: #ff0000;\">11 Phyllis Shaman, GVE Corp., 34 Sea Drive, Amesbury, MA, 1(707)724-0000<\/span><\/p>\n<p>You have to supply the minimum NR = 2, so line record 1 gets read. This is proved by setting it to 1, as it does not complete line 1 read, so feeds nothing to file 2:<\/p>\n<p><span style=\"color: #0000ff;\">awk '<strong>NR%1\u00a0== 1<\/strong> {getline f2 &lt; \"presidents.txt\"; print f2} 1' names.txt | cat -n<\/span><br \/>\n<span style=\"color: #ff0000;\">1 John Rink, Boob Inc, 2 Tities street, Boston, MA, 666-555-1111<\/span><br \/>\n<span style=\"color: #ff0000;\">2 John Robinson, Koren Inc., 978, 4th Ave., Boston, MA, 01760,696-0987....<\/span><\/p>\n<p><strong>~\u00a0Matches - <\/strong>search for a string in a specific field and if it matches, print xxx..<\/p>\n<p><span style=\"color: #0000ff;\">awk ' { print $0\"\\n\" NF \" fields present\" }' names.txt<\/span><br \/>\nJohn Rink, Boob Inc, 2 Tities street, Boston, MA, 666-555-1111<br \/>\n10 fields present<br \/>\nJohn Robinson, Koren Inc., 978, 4th Ave., Boston, MA, 01760,696-0987<br \/>\n10 fields present<br \/>\nPhyllis Chapman, GVE Corp., 34 Sea Drive, Amesbury, MA, 01881 879-0900<br \/>\n11 fields present<br \/>\nPhyllis Man, GVE Corp., 34 Sea Drive, Amesbury, MA, 707-724-0000<br \/>\n10 fields present<br \/>\nPhil Thap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707) 724-0000<br \/>\n11 fields present<br \/>\n(Phyllis Chap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707)724-0000<br \/>\n10 fields present<br \/>\nPhyllis Apeman, GVE Corp., 34 Sea Drive, Amesbury, MA, 1-707-724-0000<br \/>\n10 fields present<br \/>\nPhyllis Chipmunk, GVE Corp., 34 Sea Drive, Amesbury, MA, 1707-724-0000<br \/>\n10 fields present<br \/>\nPhyllis Shaman, GVE Corp., 34 Sea Drive, Amesbury, MA, 1(707)724-0000<br \/>\n10 fields present<\/p>\n<p><span style=\"color: #0000ff;\">awk '$4 ~\/Inc\/ {print $1,$2,$4 \" \" $10}' names.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">John Rink, Inc, 666-555-1111<\/span><br \/>\n<span style=\"color: #ff0000;\">John Robinson, Inc., 01760,696-0987<\/span><\/p>\n<p><span style=\"color: #0000ff;\">grep Inc names.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">John Rink, Boob Inc, 2 Tities street, Boston, MA, 666-555-1111<\/span><br \/>\n<span style=\"color: #ff0000;\">John Robinson, Koren Inc., 978, 4th Ave., Boston, MA, 01760,696-0987<\/span><\/p>\n<hr \/>\n<p>The cheque book deduction example - from above, one liners start getting too long, so you need to program with scripts, and comment addition is compulsory as for any script. It does a lot with just 2 functions:<\/p>\n<p><span style=\"color: #0000ff;\">cat cheques.awk<\/span><br \/>\n# checkbook.awk<br \/>\nBEGIN { FS = \"\\t\" }<br \/>\n#1 Expect the first record to have the starting balance.<br \/>\nNR == 1 { print \"Beginning Balance: \\t\" $1<br \/>\nbalance = $1<br \/>\nnext<br \/>\n# get next record and start over<br \/>\n}<br \/>\n#2 Apply to each check record, subtracting amount from balance<br \/>\n{<br \/>\nprint $1, $2, $3<br \/>\nprint balance -= $3<br \/>\n}<\/p>\n<p><span style=\"color: #0000ff;\">cat spent.txt<\/span><br \/>\n1000<br \/>\n125 Market 125.45<br \/>\n126 Hardware Store 34.95<br \/>\n127 Video Store 7.45<br \/>\n128 Book Store 14.32<br \/>\n129 Gasoline 16.10<\/p>\n<p>This deducts the spends from a 1000 balance:<\/p>\n<p><span style=\"color: #0000ff;\">awk -f cheques.awk spent.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">Beginning Balance: 1000<\/span><br \/>\n<span style=\"color: #ff0000;\">125 Market 125.45<\/span><br \/>\n<span style=\"color: #ff0000;\">874.55<\/span><br \/>\n<span style=\"color: #ff0000;\">126 Hardware Store 34.95<\/span><br \/>\n<span style=\"color: #ff0000;\">839.6<\/span><br \/>\n<span style=\"color: #ff0000;\">127 Video Store 7.45<\/span><br \/>\n<span style=\"color: #ff0000;\">832.15<\/span><br \/>\n<span style=\"color: #ff0000;\">128 Book Store 14.32<\/span><br \/>\n<span style=\"color: #ff0000;\">817.83<\/span><br \/>\n<span style=\"color: #ff0000;\">129 Gasoline 16.10<\/span><br \/>\n<span style=\"color: #ff0000;\">801.73<\/span><\/p>\n<p>This can be tidied by making the current balance clearer in line with the start balance:<\/p>\n<p><span style=\"color: #ff0000;\"># checkbook.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">BEGIN { FS = \"\\t\" }<\/span><br \/>\n<span style=\"color: #ff0000;\">#1 Expect the first record to have the starting balance.<\/span><br \/>\n<span style=\"color: #ff0000;\">NR == 1 { print \"Beginning Balance: \\t\" $1<\/span><br \/>\n<span style=\"color: #ff0000;\">balance = $1<\/span><br \/>\n<span style=\"color: #ff0000;\">next<\/span><br \/>\n<span style=\"color: #ff0000;\"># get next record and start over<\/span><br \/>\n<span style=\"color: #ff0000;\">}<\/span><br \/>\n<span style=\"color: #ff0000;\">#2 Apply to each check record, subtracting amount from balance<\/span><br \/>\n<span style=\"color: #ff0000;\">{<\/span><br \/>\n<span style=\"color: #ff0000;\">print $1, $2, $3<\/span><br \/>\n<span style=\"color: #ff0000;\"><strong>print \"Current Balance: \\t\", balance -= $3<\/strong><\/span><br \/>\n<span style=\"color: #ff0000;\">}<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -f cheques.awk spent.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">Beginning Balance: 1000<\/span><br \/>\n<span style=\"color: #ff0000;\">125 Market 125.45<\/span><br \/>\n<span style=\"color: #ff0000;\">Current Balance: 874.55<\/span><br \/>\n<span style=\"color: #ff0000;\">126 Hardware Store 34.95<\/span><br \/>\n<span style=\"color: #ff0000;\">Current Balance: 839.6<\/span><br \/>\n<span style=\"color: #ff0000;\">127 Video Store 7.45<\/span><br \/>\n<span style=\"color: #ff0000;\">Current Balance: 832.15<\/span><br \/>\n<span style=\"color: #ff0000;\">128 Book Store 14.32<\/span><br \/>\n<span style=\"color: #ff0000;\">Current Balance: 817.83<\/span><br \/>\n<span style=\"color: #ff0000;\">129 Gasoline 16.10<\/span><br \/>\n<span style=\"color: #ff0000;\">Current Balance: 801.73<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk '{if (NR&gt;0) print}' names.txt<\/span><br \/>\nJohn Rink, Boob Inc, 2 Tities street, Boston, MA, 666-555-1111<br \/>\nJohn Robinson, Koren Inc., 978, 4th Ave., Boston, MA, 01760,696-0987<br \/>\nPhyllis Chapman, GVE Corp., 34 Sea Drive, Amesbury, MA, 01881 879-0900<br \/>\nPhyllis Man, GVE Corp., 34 Sea Drive, Amesbury, MA, 707-724-0000<br \/>\nPhil Thap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707) 724-0000<br \/>\n(Phyllis Chap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707)724-0000<br \/>\nPhyllis Apeman, GVE Corp., 34 Sea Drive, Amesbury, MA, 1-707-724-0000<br \/>\nPhyllis Chipmunk, GVE Corp., 34 Sea Drive, Amesbury, MA, 1707-724-0000<br \/>\nPhyllis Shaman, GVE Corp., 34 Sea Drive, Amesbury, MA, 1(707)724-0000<\/p>\n<p>You can print last record only now you know how many there are (9):<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if (NR&gt;8) print}' names.txt<\/span><br \/>\nPhyllis Shaman, GVE Corp., 34 Sea Drive, Amesbury, MA, 1(707)724-0000<\/p>\n<p><strong>Operator Descriptions<\/strong><br \/>\n||\u00a0Logical OR<br \/>\n&amp;&amp;\u00a0Logical AND<br \/>\n!\u00a0Logical NOT<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if (NF == 10 &amp;&amp; NR &gt; 0) print}' names.txt<\/span><br \/>\nJohn Rink, Boob Inc, 2 Tities street, Boston, MA, 666-555-1111<br \/>\nJohn Robinson, Koren Inc., 978, 4th Ave., Boston, MA, 01760,696-0987<br \/>\nPhyllis Man, GVE Corp., 34 Sea Drive, Amesbury, MA, 707-724-0000<br \/>\n(Phyllis Chap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707)724-0000<br \/>\nPhyllis Apeman, GVE Corp., 34 Sea Drive, Amesbury, MA, 1-707-724-0000<br \/>\nPhyllis Chipmunk, GVE Corp., 34 Sea Drive, Amesbury, MA, 1707-724-0000<br \/>\nPhyllis Shaman, GVE Corp., 34 Sea Drive, Amesbury, MA, 1(707)724-0000<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if (NF == 11 &amp;&amp; NR &gt; 0) print}' names.txt<\/span><br \/>\nPhyllis Chapman, GVE Corp., 34 Sea Drive, Amesbury, MA, 01881 879-0900<br \/>\nPhil Thap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707) 724-0000<\/p>\n<p><strong>Precedence and ~ Matches<\/strong><\/p>\n<p>The names.txt file records have 10 or 11 fields:<\/p>\n<p><span style=\"color: #0000ff;\">awk '{print NF}' names.txt<\/span><br \/>\n10<br \/>\n10<br \/>\n11<br \/>\n10<br \/>\n11<br \/>\n10<br \/>\n10<br \/>\n10<br \/>\n10<\/p>\n<p>The first &amp;&amp; option will find records with 11 or more fields - which there are, so TRUE - so prints anyway:<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if (NR &gt; 0 &amp;&amp; NF &gt;= 11) print}' names.txt<\/span><br \/>\nPhyllis Chapman, GVE Corp., 34 Sea Drive, Amesbury, MA, 01881 879-0900<br \/>\nPhil Thap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707) 724-0000<\/p>\n<p>The next option is as above, because OR is false - no records start with an X:<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if ((NR &gt; 0 &amp;&amp; NF &gt;= 11) || $1 ~ \/X\/) print}' names.txt<\/span><br \/>\nPhyllis Chapman, GVE Corp., 34 Sea Drive, Amesbury, MA, 01881 879-0900<br \/>\nPhil Thap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707) 724-0000<\/p>\n<p>BUT...if both AND and OR conditions are TRUE,\u00a0then those files that fit either condition are printed - the file starting with a \"<span style=\"color: #ff0000;\">(<\/span>\" also, which doesn't begin with a capital and has only 10 fields. The first 2 lines have 11 fields so are also TRUE.<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if ((NR &gt; 0 &amp;&amp; NF &gt;= 11) || $1 ~ \/\\(\/) print}' names.txt<\/span><br \/>\nPhyllis Chapman, GVE Corp., 34 Sea Drive, Amesbury, MA, 01881 879-0900<br \/>\nPhil Thap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707) 724-0000<br \/>\n<span style=\"color: #ff0000;\">(<\/span>Phyllis Chap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707)724-0000<\/p>\n<p>For the OR operator to be precedent, either one or both of the &amp;&amp; conditions must be false, and the OR true. The first letter of a record starts P (TRUE) &amp;&amp; the 2nd letter starts with Q (FALSE) so first condition is FALSE. The 2nd condition (OR) is TRUE, because it's the only file that starts with the \"(\"<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if ($1 ~ \/P\/ &amp;&amp; $2 ~ \/Q\/ || $1 ~ \/\\(\/) print }' names.txt<\/span><br \/>\n(Phyllis Chap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707)724-0000<\/p>\n<p>If neither side are TRUE, nothing is printed:<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if ($1 ~ \/Z\/ &amp;&amp; $2 ~ \/Z\/ || $1 ~ \/Z\/) print }' names.txt<\/span><\/p>\n<p>The logic is negated by the \"!\" so all records are printed:<\/p>\n<p><span style=\"color: #0000ff;\">awk '{if ($1 !~ \/Z\/ &amp;&amp; $2 !~ \/Z\/ || $1 !~ \/Z\/) print }' names.txt<\/span><br \/>\nJohn Rink, Boob Inc, 2 Tities street, Boston, MA, 666-555-1111<br \/>\nJohn Robinson, Koren Inc., 978, 4th Ave., Boston, MA, 01760,696-0987<br \/>\nPhyllis Chapman, GVE Corp., 34 Sea Drive, Amesbury, MA, 01881 879-0900<br \/>\nPhyllis Man, GVE Corp., 34 Sea Drive, Amesbury, MA, 707-724-0000<br \/>\nPhil Thap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707) 724-0000<br \/>\n(Phyllis Chap, GVE Corp., 34 Sea Drive, Amesbury, MA, (707)724-0000<br \/>\nPhyllis Apeman, GVE Corp., 34 Sea Drive, Amesbury, MA, 1-707-724-0000<br \/>\nPhyllis Chipmunk, GVE Corp., 34 Sea Drive, Amesbury, MA, 1707-724-0000<br \/>\nPhyllis Shaman, GVE Corp., 34 Sea Drive, Amesbury, MA, 1(707)724-0000<\/p>\n<p><span style=\"color: #0000ff;\">ls -l<\/span><br \/>\n-rw-r--r-- 1 stevee stevee 512 Aug 24 06:11 awk.scr<br \/>\n-rw-r--r-- 1 stevee stevee 157 Aug 23 19:28 block.awk<br \/>\n-rw-r--r-- 1 stevee stevee 240 Aug 21 20:29 blocklist.awk<br \/>\n-rw-r--r-- 1 stevee stevee 293 Aug 23 19:28 cheques.awk<br \/>\n-rw-r--r-- 1 stevee stevee 64 Aug 22 20:52 grades.txt<br \/>\n-rw-r--r-- 1 stevee stevee 72 Aug 23 17:28 multiline_record.txt<br \/>\n-rw-r--r-- 1 stevee stevee 612 Aug 24 08:17 names.txt<br \/>\n-rw-r--r-- 1 stevee stevee 368 Aug 22 23:36 phonelist.awk<br \/>\n-rw-r--r-- 1 stevee stevee 121 Aug 23 16:40 presidents.txt<br \/>\n-rw-r--r-- 1 stevee stevee 109 Aug 23 19:26 spent.txt<br \/>\n-rw-r--r-- 1 stevee stevee 144 Aug 24 07:24 text<\/p>\n<p><strong>Creating running total of file bytes from ls cmd using script \"fls.awk\", making it exe and starting with;<\/strong><\/p>\n<p><span style=\"color: #0000ff;\">ls -l $* | awk '{print $5, \"\\t\", $9}'<\/span><\/p>\n<p>512 awk.scr<br \/>\n157 block.awk<br \/>\n240 blocklist.awk<br \/>\n293 cheques.awk<br \/>\n64 grades.txt<br \/>\n72 multiline_record.txt<br \/>\n612 names.txt<br \/>\n368 phonelist.awk<br \/>\n121 presidents.txt<br \/>\n109 spent.txt<br \/>\n144 text<\/p>\n<p><span style=\"color: #0000ff;\">.\/fls.awk<\/span><br \/>\n512 awk.scr<br \/>\n157 block.awk<br \/>\n240 blocklist.awk<br \/>\n293 cheques.awk<br \/>\n38 fls.awk<br \/>\n64 grades.txt<br \/>\n72 multiline_record.txt<br \/>\n612 names.txt<br \/>\n368 phonelist.awk<br \/>\n121 presidents.txt<br \/>\n109 spent.txt<br \/>\n144 text<\/p>\n<p><span style=\"color: #0000ff;\">.\/fls.awk *txt<\/span><br \/>\n64 grades.txt<br \/>\n72 multiline_record.txt<br \/>\n612 names.txt<br \/>\n121 presidents.txt<br \/>\n109 spent.txt<\/p>\n<p>Adding to script fls.awk to become:<\/p>\n<p><span style=\"color: #ff0000;\">ls -l $* | awk '{print $5, \"\\t\", $9}'<\/span><br \/>\n<span style=\"color: #ff0000;\">{<\/span><br \/>\n<span style=\"color: #ff0000;\">sum += $5<\/span><br \/>\n<span style=\"color: #ff0000;\">++filenum<\/span><br \/>\n<span style=\"color: #ff0000;\">print $5, \"\\t\", $9<\/span><br \/>\n<span style=\"color: #ff0000;\">}<\/span><br \/>\n<span style=\"color: #ff0000;\">END { print \"Total: \", sum, \"bytes (\" filenum \" files)\" }<\/span><\/p>\n<p>This is an example of the bad proof reading - you work it out for yourself - I got it to work as:<\/p>\n<p>where fls.awk is:<br \/>\n<span style=\"color: #ff0000;\">BEGIN { print \"BYTES\", \"\\t\", \"FILE\" }<\/span><br \/>\n<span style=\"color: #ff0000;\">{print $5, \"\\t\", $9<\/span><br \/>\n<span style=\"color: #ff0000;\">sum += $5<\/span><br \/>\n<span style=\"color: #ff0000;\">+filenum}<\/span><br \/>\n<span style=\"color: #ff0000;\">END { print \"Total: \", sum, \"bytes (\" filenum NR \" files)\" }<\/span><\/p>\n<p><span style=\"color: #0000ff;\">ls -l | awk -f fls.awk<\/span><br \/>\nBYTES FILE<\/p>\n<p><span style=\"color: #ff0000;\">512 awk.scr<\/span><br \/>\n<span style=\"color: #ff0000;\">157 block.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">240 blocklist.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">293 cheques.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">154 fls.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">64 grades.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">72 multiline_record.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">612 names.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">368 phonelist.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">121 presidents.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">109 spent.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">144 text<\/span><br \/>\n<span style=\"color: #ff0000;\">Total: 2846 bytes (13 files)<\/span><\/p>\n<p>Checked with calc and it correct. Needs formatting better - Total is wrong place etc.:<\/p>\n<p><span style=\"color: #ff0000;\">BEGIN {print \"BYTES\", \"\\t\", \"FILE\" }<\/span><br \/>\n<span style=\"color: #ff0000;\">{print $5, \"\\t\", $9<\/span><br \/>\n<span style=\"color: #ff0000;\">sum += $5<\/span><br \/>\n<span style=\"color: #ff0000;\">+filenum}<\/span><br \/>\n<span style=\"color: #ff0000;\">END {print sum,\":Bytes (\"filenum NR,\" files)\" }<\/span><\/p>\n<p>now put all this in another exe..? The awk scr has to always be in the same dir as the sh script..hacky..! Needs to be written as a full one-liner in a sh script sometime...but works for now.<\/p>\n<p><span style=\"color: #0000ff;\">vi filesum.sh<\/span><\/p>\n<p><span style=\"color: #ff0000;\">ls -l | awk -f fls.awk<\/span><\/p>\n<p><span style=\"color: #0000ff;\">chmod u+x\u00a0filesum.sh<\/span><\/p>\n<p><span style=\"color: #0000ff;\">.\/filesum.sh *<\/span><br \/>\n<span style=\"color: #ff0000;\">BYTES FILE<\/span><\/p>\n<p><span style=\"color: #ff0000;\">512 awk.scr<\/span><br \/>\n<span style=\"color: #ff0000;\">157 block.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">240 blocklist.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">293 cheques.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">23 filesum.sh<\/span><br \/>\n<span style=\"color: #ff0000;\">140 fls.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">64 grades.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">72 multiline_record.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">612 names.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">368 phonelist.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">121 presidents.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">109 spent.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">144 text<\/span><br \/>\n<span style=\"color: #ff0000;\">2855 :Bytes (14 files)<\/span><\/p>\n<p>It even correlates! with<\/p>\n<p><span style=\"color: #0000ff;\">du -bc *<\/span><br \/>\n<span style=\"color: #ff0000;\">512 awk.scr<\/span><br \/>\n<span style=\"color: #ff0000;\">157 block.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">240 blocklist.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">293 cheques.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">23 filesum.sh<\/span><br \/>\n<span style=\"color: #ff0000;\">140 fls.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">64 grades.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">72 multiline_record.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">612 names.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">368 phonelist.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">121 presidents.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">109 spent.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">144 text<\/span><br \/>\n<span style=\"color: #ff0000;\">2855 total<\/span><\/p>\n<p>Formatting print - kernel very diff from PDF output - experiment..<\/p>\n<p><span style=\"color: #0000ff;\">printf '|\\n'<\/span><br \/>\n<span style=\"color: #ff0000;\">|<\/span><\/p>\n<p><span style=\"color: #0000ff;\">printf '(\"|\")\\n'<\/span><br \/>\n<span style=\"color: #ff0000;\">(\"|\")<\/span><\/p>\n<p><strong>awk sh as a grep search of $1:<\/strong><\/p>\n<p><span style=\"color: #0000ff;\">vi acronyms.txt<\/span><\/p>\n<p><strong>BASIC<\/strong> Beginner's All-Purpose Symbolic Instruction Code<br \/>\n<strong>CICS<\/strong> Customer Information Control System<br \/>\n<strong>COBOL<\/strong> Common Business Oriented Language<br \/>\n<strong>DBMS<\/strong> Data Base Management System<br \/>\n<strong>GIGO<\/strong> Garbage In, Garbage Out<br \/>\n<strong>GIRL<\/strong> Generalized Information Retrieval Language<br \/>\n<strong>AWK<\/strong> Aho Weinberger Kernighan<br \/>\n<strong>PERL<\/strong> Practical Extraction and Reporting Language<br \/>\n<strong>FORTRAN\u00a0<\/strong>Formula Translation<\/p>\n<p><span style=\"color: #0000ff;\">vi search.sh<\/span><\/p>\n<p>#! \/bin\/sh<br \/>\n# assign shell's $1 to awk search variable<br \/>\n<strong>awk '$1 ~ search' search=$1 acronyms.txt<\/strong><\/p>\n<p><span style=\"color: #0000ff;\">.\/search.sh GI\u00a0<\/span><br \/>\n<span style=\"color: #ff0000;\"><strong>GI<\/strong>GO Garbage In, Garbage Out<\/span><br \/>\n<span style=\"color: #ff0000;\"><strong>GI<\/strong>RL Generalized Information Retrieval Language<\/span><\/p>\n<p><span style=\"color: #0000ff;\">.\/search.sh a<\/span><\/p>\n<p><span style=\"color: #0000ff;\">.\/search.sh A<\/span><br \/>\n<span style=\"color: #ff0000;\">BASIC Beginner's All-Purpose Symbolic Instruction Code <\/span><br \/>\n<span style=\"color: #ff0000;\">AWK Aho Weinberger Kernighan<\/span><br \/>\n<span style=\"color: #ff0000;\">FORTRAN Formula Translation<\/span><\/p>\n<p><span style=\"color: #0000ff;\">.\/search.sh AW<\/span><br \/>\n<span style=\"color: #ff0000;\">AWK Aho Weinberger Kernighan<\/span><\/p>\n<p><strong>for loop to read\u00a0word of each field at a time:<\/strong><\/p>\n<p><span style=\"color: #0000ff;\">vi awkfor.awk<\/span><\/p>\n<p><span style=\"color: #ff0000;\">{for ( i = 1; i &lt;= NF; i++ )<\/span><br \/>\n<span style=\"color: #ff0000;\">print $i}<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{for ( i = 1; i &lt;= NF; i++ ) print $i }' acronyms.txt<\/span><\/p>\n<p><span style=\"color: #ff0000;\">BASIC<\/span><br \/>\n<span style=\"color: #ff0000;\">Beginner's<\/span><br \/>\n<span style=\"color: #ff0000;\">All-Purpose<\/span><br \/>\n<span style=\"color: #ff0000;\">Symbolic<\/span><br \/>\n<span style=\"color: #ff0000;\">Instruction<\/span><br \/>\n<span style=\"color: #ff0000;\">Code<\/span><br \/>\n<span style=\"color: #ff0000;\">CICS<\/span><br \/>\n<span style=\"color: #ff0000;\">Customer<\/span><br \/>\n<span style=\"color: #ff0000;\">Information<\/span><br \/>\n<span style=\"color: #ff0000;\">Control<\/span><br \/>\n<span style=\"color: #ff0000;\">System<\/span><br \/>\n<span style=\"color: #ff0000;\">COBOL<\/span><br \/>\n<span style=\"color: #ff0000;\">Common<\/span><br \/>\n<span style=\"color: #ff0000;\">Business<\/span><br \/>\n<span style=\"color: #ff0000;\">Oriented<\/span><br \/>\n<span style=\"color: #ff0000;\">Language<\/span><br \/>\n<span style=\"color: #ff0000;\">DBMS<\/span><br \/>\n<span style=\"color: #ff0000;\">Data<\/span><br \/>\n<span style=\"color: #ff0000;\">Base<\/span><br \/>\n<span style=\"color: #ff0000;\">Management<\/span><br \/>\n<span style=\"color: #ff0000;\">System<\/span><br \/>\n<span style=\"color: #ff0000;\">GIGO<\/span><br \/>\n<span style=\"color: #ff0000;\">Garbage<\/span><br \/>\n<span style=\"color: #ff0000;\">In,<\/span><br \/>\n<span style=\"color: #ff0000;\">Garbage<\/span><br \/>\n<span style=\"color: #ff0000;\">Out<\/span><br \/>\n<span style=\"color: #ff0000;\">GIRL<\/span><br \/>\n<span style=\"color: #ff0000;\">Generalized<\/span><br \/>\n<span style=\"color: #ff0000;\">Information<\/span><br \/>\n<span style=\"color: #ff0000;\">Retrieval<\/span><br \/>\n<span style=\"color: #ff0000;\">Language<\/span><\/p>\n<p>This can be used as a basis for statistics word count example:<\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{for ( i = 1; i &lt;= NF; i++ ) print $i }' acronyms.txt | sort | uniq -c<\/span><br \/>\n<span style=\"color: #ff0000;\">1 All-Purpose<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Base<\/span><br \/>\n<span style=\"color: #ff0000;\">1 BASIC<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Beginner's<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Business<\/span><br \/>\n<span style=\"color: #ff0000;\">1 CICS<\/span><br \/>\n<span style=\"color: #ff0000;\">1 COBOL<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Code<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Common<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Control<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Customer<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Data<\/span><br \/>\n<span style=\"color: #ff0000;\">1 DBMS<\/span><br \/>\n<strong><span style=\"color: #ff0000;\">2 Garbage<\/span><\/strong><br \/>\n<span style=\"color: #ff0000;\">1 Generalized<\/span><br \/>\n<span style=\"color: #ff0000;\">1 GIGO<\/span><br \/>\n<span style=\"color: #ff0000;\">1 GIRL<\/span><br \/>\n<span style=\"color: #ff0000;\">1 In,<\/span><br \/>\n<strong><span style=\"color: #ff0000;\">2 Information<\/span><\/strong><br \/>\n<span style=\"color: #ff0000;\">1 Instruction<\/span><br \/>\n<strong><span style=\"color: #ff0000;\">2 Language<\/span><\/strong><br \/>\n<span style=\"color: #ff0000;\">1 Management<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Oriented<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Out<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Retrieval<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Symbolic<\/span><br \/>\n<strong><span style=\"color: #ff0000;\">2 System<\/span><\/strong><\/p>\n<p>Now count word distribution of some random text:<\/p>\n<p><span style=\"color: #0000ff;\">fortune | awk -e '{for ( i = 1; i &lt;= NF; i++ ) print $i }' | sort | uniq -c<\/span><\/p>\n<p><span style=\"color: #ff0000;\">1 A:<\/span><br \/>\n<span style=\"color: #ff0000;\">2 an<\/span><br \/>\n<span style=\"color: #ff0000;\">1 and<\/span><br \/>\n<span style=\"color: #ff0000;\">1 between<\/span><br \/>\n<span style=\"color: #ff0000;\">1 difference<\/span><br \/>\n<span style=\"color: #ff0000;\">1 drunk.<\/span><br \/>\n<span style=\"color: #ff0000;\">2 Irish<\/span><br \/>\n<span style=\"color: #ff0000;\">1 less<\/span><br \/>\n<span style=\"color: #ff0000;\">1 One<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Q:<\/span><br \/>\n<span style=\"color: #ff0000;\">1 the<\/span><br \/>\n<span style=\"color: #ff0000;\">1 wake?<\/span><br \/>\n<span style=\"color: #ff0000;\">1 wedding<\/span><br \/>\n<span style=\"color: #ff0000;\">1 What's<\/span><\/p>\n<p>Or letter\/char distribution in the sys dictionaries:<\/p>\n<p><span style=\"color: #0000ff;\">cat \/usr\/share\/dict\/british-english | awk -e '{<strong>FS=\"\"\u00a0<\/strong>; for ( i = 1; i <strong>&lt;= NF<\/strong>; i++ ) print $i }' | sort | uniq -c<\/span><br \/>\n<span style=\"color: #ff0000;\">26262<strong> '<\/strong><\/span><br \/>\n<span style=\"color: #ff0000;\">63180 a<\/span><br \/>\n<span style=\"color: #ff0000;\">1288 A<\/span><br \/>\n<span style=\"color: #ff0000;\">10 \u00c3\u00a1<\/span><br \/>\n<span style=\"color: #ff0000;\">6 \u00c3:<\/span><br \/>\n<span style=\"color: #ff0000;\">3 \u00c3\u00a5<\/span><br \/>\n<span style=\"color: #ff0000;\">1 \u00c3\u2026<\/span><br \/>\n<span style=\"color: #ff0000;\">4 \u00c3\u00a4<\/span><br \/>\n<span style=\"color: #ff0000;\">14293 b<\/span><br \/>\n<span style=\"color: #ff0000;\">1247 B<\/span><br \/>\n<span style=\"color: #ff0000;\">30435 c<\/span><br \/>\n<span style=\"color: #ff0000;\">1419 C<\/span><br \/>\n<span style=\"color: #ff0000;\">5 \u00c3\u00a7<\/span><br \/>\n<span style=\"color: #ff0000;\">27771 d<\/span><br \/>\n<span style=\"color: #ff0000;\">734 D<\/span><br \/>\n<span style=\"color: #ff0000;\"><strong>88096 e<\/strong><\/span><\/p>\n<p><span style=\"color: #ff0000;\"><strong>596 E....<\/strong><\/span><\/p>\n<p><span style=\"color: #ff0000;\">89882 s<\/span><\/p>\n<p>That stat is misleading for letter s because this dic lists plurals\/ownership :\u00a01 youngsters,1 youngster's. The word count total is\u00a099156 so many extra s.<\/p>\n<p>You would need the total letter sample in the dictionary for stats.<\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{for ( i = 1; i &lt;= NF; i++ ) print $i }' &lt; alphabet.txt | sort | uniq -c<\/span><br \/>\n<span style=\"color: #ff0000;\">1 a<\/span><br \/>\n<span style=\"color: #ff0000;\">1 b<\/span><br \/>\n<span style=\"color: #ff0000;\">1 c<\/span><br \/>\n<span style=\"color: #ff0000;\">1 d<\/span><br \/>\n<span style=\"color: #ff0000;\">1 e<\/span><br \/>\n<span style=\"color: #ff0000;\">1 f<\/span><br \/>\n<span style=\"color: #ff0000;\">1 g<\/span><br \/>\n<span style=\"color: #ff0000;\">1 h<\/span><br \/>\n<span style=\"color: #ff0000;\">1 i<\/span><br \/>\n<span style=\"color: #ff0000;\">1 j<\/span><br \/>\n<span style=\"color: #ff0000;\">1 k<\/span><br \/>\n<span style=\"color: #ff0000;\">1 l<\/span><br \/>\n<span style=\"color: #ff0000;\">1 m<\/span><br \/>\n<span style=\"color: #ff0000;\">1 n<\/span><br \/>\n<span style=\"color: #ff0000;\">1 o<\/span><br \/>\n<span style=\"color: #ff0000;\">1 p<\/span><br \/>\n<span style=\"color: #ff0000;\">1 q<\/span><br \/>\n<span style=\"color: #ff0000;\">1 r<\/span><br \/>\n<span style=\"color: #ff0000;\">1 s<\/span><br \/>\n<span style=\"color: #ff0000;\">1 t<\/span><br \/>\n<span style=\"color: #ff0000;\">1 u<\/span><br \/>\n<span style=\"color: #ff0000;\">1 v<\/span><br \/>\n<span style=\"color: #ff0000;\">1 w<\/span><br \/>\n<span style=\"color: #ff0000;\">1 x<\/span><br \/>\n<span style=\"color: #ff0000;\">1 y<\/span><br \/>\n<span style=\"color: #ff0000;\">1 z<\/span><\/p>\n<p>Do reverse loop - count down for same result:<\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{for ( i = NF; i &gt;= NF; i-- ) print $i }' &lt; alphabet.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">a<\/span><br \/>\n<span style=\"color: #ff0000;\">b<\/span><br \/>\n<span style=\"color: #ff0000;\">c<\/span><br \/>\n<span style=\"color: #ff0000;\">d<\/span><br \/>\n<span style=\"color: #ff0000;\">e<\/span><br \/>\n<span style=\"color: #ff0000;\">f<\/span><br \/>\n<span style=\"color: #ff0000;\">g<\/span><br \/>\n<span style=\"color: #ff0000;\">h<\/span><br \/>\n<span style=\"color: #ff0000;\">i<\/span><br \/>\n<span style=\"color: #ff0000;\">j<\/span><br \/>\n<span style=\"color: #ff0000;\">k<\/span><br \/>\n<span style=\"color: #ff0000;\">l<\/span><br \/>\n<span style=\"color: #ff0000;\">m<\/span><br \/>\n<span style=\"color: #ff0000;\">n<\/span><br \/>\n<span style=\"color: #ff0000;\">o<\/span><br \/>\n<span style=\"color: #ff0000;\">p<\/span><br \/>\n<span style=\"color: #ff0000;\">q<\/span><br \/>\n<span style=\"color: #ff0000;\">r<\/span><br \/>\n<span style=\"color: #ff0000;\">s<\/span><br \/>\n<span style=\"color: #ff0000;\">t<\/span><br \/>\n<span style=\"color: #ff0000;\">u<\/span><br \/>\n<span style=\"color: #ff0000;\">v<\/span><br \/>\n<span style=\"color: #ff0000;\">w<\/span><br \/>\n<span style=\"color: #ff0000;\">x<\/span><br \/>\n<span style=\"color: #ff0000;\">y<\/span><br \/>\n<span style=\"color: #ff0000;\">z<\/span><\/p>\n<p>Count no. of loops\/lines processed:<\/p>\n<p><span style=\"color: #0000ff;\">awk -e '{for ( i = 1; i &lt;= NF; i++ ) print $i, x += 1 }' &lt; alphabet.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">a 1<\/span><br \/>\n<span style=\"color: #ff0000;\">b 2<\/span><br \/>\n<span style=\"color: #ff0000;\">c 3<\/span><br \/>\n<span style=\"color: #ff0000;\">d 4<\/span><br \/>\n<span style=\"color: #ff0000;\">e 5<\/span><br \/>\n<span style=\"color: #ff0000;\">f 6<\/span><br \/>\n<span style=\"color: #ff0000;\">g 7<\/span><br \/>\n<span style=\"color: #ff0000;\">h 8<\/span><br \/>\n<span style=\"color: #ff0000;\">i 9<\/span><br \/>\n<span style=\"color: #ff0000;\">j 10<\/span><br \/>\n<span style=\"color: #ff0000;\">k 11<\/span><br \/>\n<span style=\"color: #ff0000;\">l 12<\/span><br \/>\n<span style=\"color: #ff0000;\">m 13<\/span><br \/>\n<span style=\"color: #ff0000;\">n 14<\/span><br \/>\n<span style=\"color: #ff0000;\">o 15<\/span><br \/>\n<span style=\"color: #ff0000;\">p 16<\/span><br \/>\n<span style=\"color: #ff0000;\">q 17<\/span><br \/>\n<span style=\"color: #ff0000;\">r 18<\/span><br \/>\n<span style=\"color: #ff0000;\">s 19<\/span><br \/>\n<span style=\"color: #ff0000;\">t 20<\/span><br \/>\n<span style=\"color: #ff0000;\">u 21<\/span><br \/>\n<span style=\"color: #ff0000;\">v 22<\/span><br \/>\n<span style=\"color: #ff0000;\">w 23<\/span><br \/>\n<span style=\"color: #ff0000;\">x 24<\/span><br \/>\n<span style=\"color: #ff0000;\">y 25<\/span><br \/>\n<span style=\"color: #ff0000;\">z 26<\/span><\/p>\n<p><span style=\"color: #0000ff;\">cat TheRavenV1_3.txt<\/span><br \/>\nOnce upon a midnight dreary, while I pondered, weak and weary,<br \/>\nOver many a quaint and curious volume of forgotten lore\u201d<br \/>\nWhile I nodded, nearly napping, suddenly there came a tapping,<br \/>\nAs of some one gently rapping, rapping at my chamber door.<br \/>\n,Tis some visitor, I muttered, tapping at my chamber door\u201d<br \/>\nOnly this and nothing more.<\/p>\n<p>Ah, distinctly I remember it was in the bleak December;<br \/>\nAnd each separate dying ember wrought its ghost upon the floor.<br \/>\nEagerly I wished the morrow;\u201dvainly I had sought to borrow<br \/>\nFrom my books surcease of sorrow\u201dsorrow for the lost Lenore\u201d<br \/>\nFor the rare and radiant maiden whom the angels name Lenore\u201d<br \/>\nNameless here for evermore.<\/p>\n<p>And the silken, sad, uncertain rustling of each purple curtain<br \/>\nThrilled me\u201dfilled me with fantastic terrors never felt before;<br \/>\nSo that now, to still the beating of my heart, I stood repeating<br \/>\n,Tis some visitor entreating entrance at my chamber door\u201d<br \/>\nSome late visitor entreating entrance at my chamber door;\u201d<br \/>\nThis it is and nothing more.<\/p>\n<p><span style=\"color: #0000ff;\">cat TheRavenV1_3.txt | awk -e '{FS=\"\" ; for ( i = 1; i &lt;= NF; i++ ) print $i }' | sort | uniq -c<\/span><br \/>\n<span style=\"color: #ff0000;\">221 <\/span><br \/>\n<span style=\"color: #ff0000;\">11 ,<\/span><br \/>\n<span style=\"color: #ff0000;\">4 ;<\/span><br \/>\n<span style=\"color: #ff0000;\">5 .<\/span><br \/>\n<span style=\"color: #ff0000;\">2 ,<\/span><br \/>\n<span style=\"color: #ff0000;\">3 <\/span><br \/>\n<span style=\"color: #ff0000;\">3 <\/span><br \/>\n<span style=\"color: #ff0000;\">9 \u201d<\/span><br \/>\n<span style=\"color: #ff0000;\">55 a<\/span><br \/>\n<span style=\"color: #ff0000;\">4 A<\/span><br \/>\n<span style=\"color: #ff0000;\">1 and<\/span><br \/>\n<span style=\"color: #ff0000;\">12 b<\/span><br \/>\n<span style=\"color: #ff0000;\">16 c<\/span><br \/>\n<span style=\"color: #ff0000;\">26 d<\/span><br \/>\n<span style=\"color: #ff0000;\">1 D<\/span><br \/>\n<span style=\"color: #ff0000;\">1 dreary,<\/span><br \/>\n<span style=\"color: #ff0000;\">91 e<\/span><br \/>\n<span style=\"color: #ff0000;\">1 E<\/span><br \/>\n<span style=\"color: #ff0000;\">13 f<\/span><br \/>\n<span style=\"color: #ff0000;\">2 F<\/span><br \/>\n<span style=\"color: #ff0000;\">20 g<\/span><br \/>\n<span style=\"color: #ff0000;\">32 h<\/span><br \/>\n<span style=\"color: #ff0000;\">45 i<\/span><br \/>\n<span style=\"color: #ff0000;\">7 I<\/span><br \/>\n<span style=\"color: #ff0000;\">3 k<\/span><br \/>\n<span style=\"color: #ff0000;\">26 l<\/span><br \/>\n<span style=\"color: #ff0000;\">2 L<\/span><br \/>\n<span style=\"color: #ff0000;\">33 m<\/span><br \/>\n<span style=\"color: #ff0000;\">1 midnight<\/span><br \/>\n<span style=\"color: #ff0000;\">54 n<\/span><br \/>\n<span style=\"color: #ff0000;\">1 N<\/span><br \/>\n<span style=\"color: #ff0000;\">64 o<\/span><br \/>\n<span style=\"color: #ff0000;\">2 O<\/span><br \/>\n<span style=\"color: #ff0000;\">1 Once<\/span><br \/>\n<span style=\"color: #ff0000;\">15 p<\/span><br \/>\n<span style=\"color: #ff0000;\">1 pondered,<\/span><br \/>\n<span style=\"color: #ff0000;\">1 q<\/span><br \/>\n<span style=\"color: #ff0000;\">67 r<\/span><br \/>\n<span style=\"color: #ff0000;\">37 s<\/span><br \/>\n<span style=\"color: #ff0000;\">2 S<\/span><br \/>\n<span style=\"color: #ff0000;\">62 t<\/span><br \/>\n<span style=\"color: #ff0000;\">4 T<\/span><br \/>\n<span style=\"color: #ff0000;\">14 u<\/span><br \/>\n<span style=\"color: #ff0000;\">1 upon<\/span><br \/>\n<span style=\"color: #ff0000;\">8 v<\/span><br \/>\n<span style=\"color: #ff0000;\">10 w<\/span><br \/>\n<span style=\"color: #ff0000;\">1 W<\/span><br \/>\n<span style=\"color: #ff0000;\">1 weak<\/span><br \/>\n<span style=\"color: #ff0000;\">1 weary,<\/span><br \/>\n<span style=\"color: #ff0000;\">1 while<\/span><br \/>\n<span style=\"color: #ff0000;\">15 y<\/span><\/p>\n<p><strong>Examining the history histograph function - how it works - can be applied elsewhere?:<\/strong><\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++}END{for(i in h){print h[i],i|\"sort -rn|head -20\"}}' |awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"o%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">awk 419 <\/span><br \/>\n<span style=\"color: #ff0000;\">perl 216 # <\/span><br \/>\n<span style=\"color: #ff0000;\">cat 135 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">vi 39 <\/span><br \/>\n<span style=\"color: #ff0000;\">printf 34 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">echo 26 # <\/span><br \/>\n<span style=\"color: #ff0000;\">for 17 <\/span><br \/>\n<span style=\"color: #ff0000;\">budellmint 15 <\/span><br \/>\n<span style=\"color: #ff0000;\">.\/search.sh 14 <\/span><br \/>\n<span style=\"color: #ff0000;\">uptime 12 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">ls 7 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">df 5 # <\/span><br \/>\n<span style=\"color: #ff0000;\">history 4 # <\/span><br \/>\n<span style=\"color: #ff0000;\">hexdump 4 # <\/span><br \/>\n<span style=\"color: #ff0000;\">od 3 # <\/span><br \/>\n<span style=\"color: #ff0000;\">clear 3 # <\/span><br \/>\n<span style=\"color: #ff0000;\">90 3 # <\/span><br \/>\n<span style=\"color: #ff0000;\">87 3 # <\/span><br \/>\n<span style=\"color: #ff0000;\">74 3 # <\/span><br \/>\n<span style=\"color: #ff0000;\">70 3 #<\/span><\/p>\n<p>Just see what happens piping RavenV3 in:<\/p>\n<p><span style=\"color: #0000ff;\">cat TheRavenV1_3.txt | awk '{h[$2]++}END{for(i in h){print h[i],i|\"sort -rn|head -20\"}}' |awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\nthe 2<br \/>\nsome 2<br \/>\nI 2<br \/>\n2<br \/>\nupon 1<br \/>\nthis 1<br \/>\nthat 1<br \/>\nof 1<br \/>\nmy 1<br \/>\nme\u201dfilled 1<br \/>\nmany 1<br \/>\nlate 1<br \/>\nit 1<br \/>\nhere 1<br \/>\neach 1<br \/>\ndistinctly 1<\/p>\n<p>FIRST - 20 lines of 169 words in RavenV3<\/p>\n<p>wc -l &lt; TheRavenV1_3.txt<\/p>\n<p>20<br \/>\nwc -w &lt; TheRavenV1_3.txt<br \/>\n169<\/p>\n<p>The function is actually two awk commands that can be treated separately - good!<\/p>\n<p>history | awk '{h[$2]++}END{for(i in h){print h[i],i|\"sort -rn|head -20\"}}'<br \/>\n419 awk<br \/>\n216 perl<br \/>\n122 cat<br \/>\n39 vi...<\/p>\n<p>The first above is easyish - loops to find how many occurrences of each command used, sorts high to low numerically, then heads it to only 20. (The full history record varies on setup and linux dist.<\/p>\n<p>history | wc -l<br \/>\n1000<\/p>\n<p>The history awk\u00a0function\u00a0reads $2 for the command names and puts them in $1 for output:<\/p>\n<p><span style=\"color: #0000ff;\">history | tail -3<\/span><br \/>\n<span style=\"color: #ff0000;\">2208 <strong>wc -w<\/strong> &lt; TheRavenV1_3.txt <\/span><br \/>\n<span style=\"color: #ff0000;\">2209 <strong>history<\/strong> | head -3<\/span><br \/>\n<span style=\"color: #ff0000;\">2210 <strong>history<\/strong> | tail -3<\/span><\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++}END{for(i in h){print h[i],i|\"sort -rn|head -20\"}}' |awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}' | head -2<\/span><br \/>\n<strong>awk<\/strong> 419<br \/>\n<strong>perl<\/strong> 216 #<\/p>\n<p>I <em>think<\/em> it puts the contents of $2 of history list into an incrementing array named h[$2], so contains the command name, later ID'd by $2,h[$2], with the increment number ID'd by h[i] and i in the for loop:\u00a0This can be shown by printing the contents of each section:<\/p>\n<p>Array command list contents:<\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++; print $2}' | uniq -c | head<\/span><br \/>\n<span style=\"color: #ff0000;\">5 cat<\/span><br \/>\n<span style=\"color: #ff0000;\">3 perl<\/span><br \/>\n<span style=\"color: #ff0000;\">1 uptime<\/span><br \/>\n<span style=\"color: #ff0000;\">1 vi<\/span><br \/>\n<span style=\"color: #ff0000;\">22 cat<\/span><br \/>\n<span style=\"color: #ff0000;\">1 sudo<\/span><br \/>\n<span style=\"color: #ff0000;\">2 perl<\/span><br \/>\n<span style=\"color: #ff0000;\">1 uptime<\/span><br \/>\n<span style=\"color: #ff0000;\">10 cat<\/span><br \/>\n<span style=\"color: #ff0000;\">10 uptime<\/span><\/p>\n<p>Array Counter:<br \/>\nstevee@AMDA8 ~\/Awk $ <span style=\"color: #0000ff;\">history | awk '{h[$2]++; print h[$2]}' | head -3<\/span><br \/>\n1<br \/>\n2<br \/>\n3<\/p>\n<p>For loop counter contents:<\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++}END{for(i in h){print h[i],i }}'<\/span><br \/>\n<span style=\"color: #ff0000;\">1 rm<\/span><br \/>\n<span style=\"color: #ff0000;\">419 awk<\/span><br \/>\n<span style=\"color: #ff0000;\">1 sudo<\/span><br \/>\n<span style=\"color: #ff0000;\">2 cd<\/span><br \/>\n<span style=\"color: #ff0000;\">3 clear<\/span><br \/>\n<span style=\"color: #ff0000;\">39 vi...<\/span><\/p>\n<p>This also shows I have used only 45 different commands in the whole 1000 size list:<\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++}END{for(i in h){print h[i],i | \"sort -rn\"}}' | wc -l<\/span><br \/>\n45<\/p>\n<p><strong>Summary Part1<\/strong> - output of history command column $2 are stored in a numbered\u00a0incrementing array, counted for occurrence of each command and labelled using a FOR loop, then numerically sorted by quantity and headed.<\/p>\n<p><strong>Part 2<\/strong><\/p>\n<p><span style=\"color: #0000ff;\">awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><\/p>\n<p>The next awk command can't be shown directly as it needs the prior awk\u00a0output, but knowing the format of the first part that feeds it comprises only 2 columns, a different 2 column input can be fed to learn it's behaviour. Using cat to give a number list and a name file from prior above examples, to give text, input can be fed in:<\/p>\n<p>cat -n acronyms.txt | awk '{print $1,$2}' | <span style=\"color: #0000ff;\">awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">BASIC 1 <\/span><br \/>\n<span style=\"color: #ff0000;\">CICS 2 <\/span><br \/>\n<span style=\"color: #ff0000;\">COBOL 3 <\/span><\/p>\n<p>Now it is obvious that the multiplier is 60*$1. If reduced to 1, it shows:<\/p>\n<p><span style=\"color: #0000ff;\">cat -n acronyms.txt | awk '{print $1,$2}' | awk '!max{max=$1;}{r=\"\";i=s=1*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">BASIC 1 # <\/span><br \/>\n<span style=\"color: #ff0000;\">CICS 2 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">COBOL 3 <\/span><br \/>\n<span style=\"color: #ff0000;\">DBMS 4 # <\/span><br \/>\n<span style=\"color: #ff0000;\">GIGO 5 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">GIRL 6 <\/span><br \/>\n<span style=\"color: #ff0000;\">AWK 7 # <\/span><br \/>\n<span style=\"color: #ff0000;\">PERL 8 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">FORTRAN 9 \u00a0<\/span><\/p>\n<p><strong>Summary Part 2\u00a0<\/strong><\/p>\n<p>An experienced programmer would have seen immediately that it is part 2 that is the generic useful part for histogram usage as this contains the multiplier to suit various number ranges, and reads a NOT MAX and MAX value and uses a dividend (somehow!) to scale the output according the maximum and minimum of the range (?? not sure bout that or how).<\/p>\n<p>Point is, it can be used in isolation for many basic number lists - experimenting now...<\/p>\n<p>It works correctly\u00a0on the random.txt file, scaling automatically AND padding single digits properly in a terminal, with the 60 multiplier! Don't know how it works yet...(why 60?) This scales all below by 2\/3...very clever...<\/p>\n<p><span style=\"color: #0000ff;\">cat random.txt | awk '{print $1,$2}' | awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">90 <\/span><br \/>\n<span style=\"color: #ff0000;\">87 # <\/span><br \/>\n<span style=\"color: #ff0000;\">74 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">40 <\/span><br \/>\n<span style=\"color: #ff0000;\">90 <\/span><br \/>\n<span style=\"color: #ff0000;\">70 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">46 # <\/span><br \/>\n<span style=\"color: #ff0000;\">10 # <\/span><br \/>\n<span style=\"color: #ff0000;\">35 <\/span><br \/>\n<span style=\"color: #ff0000;\">\u00a0 8 <\/span><br \/>\n<span style=\"color: #ff0000;\">\u00a0 1 # <\/span><br \/>\n<span style=\"color: #ff0000;\">\u00a0 0<\/span><\/p>\n<p>Factorial.awk:<\/p>\n<p>-<\/p>\n<p><span style=\"color: #ff0000;\">awk '<\/span><\/p>\n<p><span style=\"color: #ff0000;\"># factorial: return factorial of user-supplied number<\/span><br \/>\n<span style=\"color: #ff0000;\">BEGIN {<\/span><br \/>\n<span style=\"color: #ff0000;\"># prompt user; use printf, not print, to avoid the\u00a0<\/span><span style=\"color: #ff0000;\">newline<\/span><br \/>\n<span style=\"color: #ff0000;\">printf(\"Enter number: \")<\/span><br \/>\n<span style=\"color: #ff0000;\">}<\/span><br \/>\n<span style=\"color: #ff0000;\"># check that user enters a number<\/span><br \/>\n<span style=\"color: #ff0000;\">$1 ~ \/^[0-9]+$\/ {<\/span><br \/>\n<span style=\"color: #ff0000;\"># assign value of $1 to number &amp; fact<\/span><br \/>\n<span style=\"color: #ff0000;\">number = $1<\/span><br \/>\n<span style=\"color: #ff0000;\">if (number == 0)<\/span><br \/>\n<span style=\"color: #ff0000;\">fact = 1<\/span><br \/>\n<span style=\"color: #ff0000;\">else<\/span><br \/>\n<span style=\"color: #ff0000;\">fact = number<\/span><br \/>\n<span style=\"color: #ff0000;\"># loop to multiply fact*x until x = 1<\/span><br \/>\n<span style=\"color: #ff0000;\">for (x = number - 1; x &gt; 1; x--)<\/span><br \/>\n<span style=\"color: #ff0000;\">fact *= x<\/span><br \/>\n<span style=\"color: #ff0000;\">printf(\"The factorial of %d is %g\\n\", number, fact)<\/span><br \/>\n<span style=\"color: #ff0000;\"># exit -- saves user from typing CRTL-D.<\/span><br \/>\n<span style=\"color: #ff0000;\">exit<\/span><br \/>\n<span style=\"color: #ff0000;\">}<\/span><br \/>\n<span style=\"color: #ff0000;\"># if not a number, prompt again.<\/span><br \/>\n<span style=\"color: #ff0000;\">{ printf(\"\\nInvalid entry. Enter a number: \")<\/span><br \/>\n<span style=\"color: #ff0000;\">}' -<\/span><\/p>\n<p><span style=\"color: #0000ff;\">chmod +x factorial.awk<\/span><\/p>\n<p><span style=\"color: #0000ff;\">.\/factorial.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">Enter number: 4<\/span><br \/>\n<span style=\"color: #ff0000;\">The factorial of 4 is 24<\/span><\/p>\n<p><span style=\"color: #0000ff;\">.\/factorial.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">Enter number: f<\/span><\/p>\n<p><span style=\"color: #ff0000;\">Invalid entry. Enter a number:\u00a00<br \/>\nThe factorial of 0 is 1<\/span><\/p>\n<p><span style=\"color: #0000ff;\">vi grades.txt<\/span><\/p>\n<p>mona 70 77 85 83 70 89<br \/>\njohn 85 92 78 94 88 91<br \/>\nandrea 89 90 85 94 90 95<br \/>\njasper 84 88 80 92 84 82<br \/>\ndunce 64 80 60 60 61 62<br \/>\nellis 90 98 89 96 96 92<\/p>\n<p><span style=\"color: #0000ff;\">vi grades.awk<\/span><\/p>\n<p># grades.awk -- average student grades and determine<br \/>\n# letter grade as well as class averages.<br \/>\n# $1 = student name; $2 - $NF = test scores.<br \/>\n# set output field separator to tab.<br \/>\nBEGIN { OFS = \"\\t\" }<br \/>\n# action applied to all input lines<br \/>\n{<br \/>\n# add up grades<br \/>\ntotal = 0<br \/>\nfor (i = 2; i &lt;= NF; ++i)<br \/>\ntotal += $i<br \/>\n# calculate average<br \/>\navg = total \/ (NF - 1)<br \/>\n# assign student's average to element of array<br \/>\nstudent_avg[NR] = avg<br \/>\n# determine letter grade<br \/>\nif (avg &gt;= 90) grade = \"A\"<br \/>\nelse if (avg &gt;= 80) grade = \"B\"<br \/>\nelse if (avg &gt;= 70) grade = \"C\"<br \/>\nelse if (avg &gt;= 60) grade = \"D\"<br \/>\nelse grade = \"F\"<br \/>\n# increment counter for letter grade array<br \/>\n++class_grade[grade]<br \/>\n# print student name, average and letter gradeprint $1, avg, grade<br \/>\n}<br \/>\n# print out class statistics<br \/>\nEND {<br \/>\n# calculate class average<br \/>\nfor (x = 1; x &lt;= NR; x++)<br \/>\nclass_avg_total += student_avg[x]<br \/>\nclass_average = class_avg_total \/ NR<br \/>\n# determine how many above\/below average<br \/>\nfor (x = 1; x &lt;= NR; x++)<br \/>\nif (student_avg[x] &gt;= class_average)<br \/>\n++above_average<br \/>\nelse<br \/>\n++below_average<br \/>\n# print results<br \/>\nprint \"\"<br \/>\nprint \"Class Average: \", class_average<br \/>\nprint \"At or Above Average: \", above_average<br \/>\nprint \"Below Average: \", below_average<br \/>\n# print number of students per letter grade<br \/>\nfor (letter_grade in class_grade)<br \/>\nprint letter_grade \":\", class_grade<br \/>\n[letter_grade] | \"sort\"<br \/>\n}<\/p>\n<p><span style=\"color: #0000ff;\">awk -f grades.awk grades.test<\/span><\/p>\n<p><span style=\"color: #ff0000;\">Class Average: 83.4167<\/span><br \/>\n<span style=\"color: #ff0000;\">At or Above Average: 4<\/span><br \/>\n<span style=\"color: #ff0000;\">Below Average: 2<\/span><br \/>\n<span style=\"color: #ff0000;\">A: 2<\/span><br \/>\n<span style=\"color: #ff0000;\">B: 2<\/span><br \/>\n<span style=\"color: #ff0000;\">C: 2<\/span><br \/>\n<span style=\"color: #ff0000;\">D: 2<\/span><\/p>\n<p>Doesn't add up and prog had to be \"fixed\" <em>wrongly<\/em> by me..Needs 2 @ avg and 4 above avge for 8 students total - Proof read the oh dear!ing thing OReilly!! This OP part is missing..<\/p>\n<p>mona 79\u00a0C<br \/>\njohn 88B<br \/>\nandrea 90.5A<\/p>\n<p>jasper 85B<br \/>\ndunce\u00a064.5D<br \/>\nellis\u00a093.5A<\/p>\n<p>The keyword in is also an operator that can be used in a conditional expression to test that a subscript is\u00a0a member of an array. The expression:<br \/>\n<em>item <strong>in<\/strong> array<\/em><\/p>\n<p>returns 1 if array[item] exists and 0 if it does not.<\/p>\n<p><span style=\"color: #0000ff;\">vi lookup.awk<\/span><\/p>\n<p>------<\/p>\n<p><span style=\"color: #ff0000;\">awk '<\/span><span style=\"color: #ff0000;\"># lookup -- reads local glossary file and prompts user<\/span><br \/>\n<span style=\"color: #ff0000;\">for query<\/span><br \/>\n<span style=\"color: #ff0000;\">#0<\/span><br \/>\n<span style=\"color: #ff0000;\">BEGIN { FS = \"\\t\"; OFS = \"\\t\"<\/span><br \/>\n<span style=\"color: #ff0000;\"># prompt user<\/span><br \/>\n<span style=\"color: #ff0000;\">printf(\"Enter a glossary term: \")<\/span><br \/>\n<span style=\"color: #ff0000;\">}<\/span><br \/>\n<span style=\"color: #ff0000;\">#1 read local file named glossary<\/span><br \/>\n<span style=\"color: #ff0000;\">FILENAME == \"glossary.txt\" {<\/span><br \/>\n<span style=\"color: #ff0000;\"># load each glossary entry into an array<\/span><\/p>\n<p><span style=\"color: #ff0000;\">entry[$1] = $2<\/span><br \/>\n<span style=\"color: #ff0000;\">next<\/span><br \/>\n<span style=\"color: #ff0000;\">}<\/span><br \/>\n<span style=\"color: #ff0000;\">#2 scan for command to exit program<\/span><br \/>\n<span style=\"color: #ff0000;\">$0 ~ \/^(quit|[qQ]|exit|[Xx])$\/ { exit }<\/span><br \/>\n<span style=\"color: #ff0000;\">#3 process any non-empty line<\/span><br \/>\n<span style=\"color: #ff0000;\">$0 != \"\" {<\/span><br \/>\n<span style=\"color: #ff0000;\">if ( $0 in entry ) {<\/span><br \/>\n<span style=\"color: #ff0000;\"># it is there, print definition<\/span><br \/>\n<span style=\"color: #ff0000;\">print entry[$0]<\/span><br \/>\n<span style=\"color: #ff0000;\">} else<\/span><br \/>\n<span style=\"color: #ff0000;\">print $0 \" not found\"<\/span><br \/>\n<span style=\"color: #ff0000;\">}<\/span><br \/>\n<span style=\"color: #ff0000;\">#4 prompt user again for another term<\/span><br \/>\n<span style=\"color: #ff0000;\">{<\/span><br \/>\n<span style=\"color: #ff0000;\">printf(\"Enter another glossary term (q to quit): \")<\/span><br \/>\n<span style=\"color: #ff0000;\">}' glossary.txt -<\/span><\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/lookup.awk_.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4656\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/lookup.awk_.png\" alt=\"lookup.awk.png\" width=\"586\" height=\"488\" \/><\/a><\/p>\n<p>---------<\/p>\n<p><span style=\"color: #0000ff;\">cp -v acronyms.txt glossary.txt<\/span><\/p>\n<p><span style=\"color: #0000ff;\">chmod +x lookup.awk<\/span><\/p>\n<p><span style=\"color: #0000ff;\">.\/lookup.awk<\/span><br \/>\n<span style=\"color: #ff0000;\">Enter a glossary term: AWK<\/span><br \/>\n<span style=\"color: #ff0000;\">Aho Weinberger Kernighan<\/span><br \/>\n<span style=\"color: #ff0000;\">Enter another glossary term (q to quit): COBOL<\/span><br \/>\n<span style=\"color: #ff0000;\">Common Business Oriented Language<\/span><br \/>\n<span style=\"color: #ff0000;\">Enter another glossary term (q to quit): q<\/span><\/p>\n<p><span style=\"color: #0000ff;\">cat glossary.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">ACID Atomicity, Consistency, Isolation, Durability<\/span><br \/>\n<span style=\"color: #ff0000;\">AWK Aho Weinberger Kernighan<\/span><br \/>\n<span style=\"color: #ff0000;\">BASIC Beginner's All-Purpose Symbolic Instruction Code <\/span><br \/>\n<span style=\"color: #ff0000;\">CICS Customer Information Control System<\/span><br \/>\n<span style=\"color: #ff0000;\">COBOL Common Business Oriented Language<\/span><br \/>\n<span style=\"color: #ff0000;\">DBMS Data Base Management System<\/span><br \/>\n<span style=\"color: #ff0000;\">GIGO Garbage In, Garbage Out<\/span><br \/>\n<span style=\"color: #ff0000;\">GIRL Generalized Information Retrieval Language<\/span><br \/>\n<span style=\"color: #ff0000;\">PERL Practical Extraction and Reporting Language<\/span><br \/>\n<span style=\"color: #ff0000;\">FORTRAN Formula Translation<\/span><\/p>\n<p><strong>Program Piping<\/strong><\/p>\n<p>Coz awk acts per line, it can alter output of a program piped thru it e.g. a prime prog prints primes per line, so awk can count the number of primes output per request:<\/p>\n<p><span style=\"color: #0000ff;\">.\/bash.sh<\/span><br \/>\n<span style=\"color: #ff0000;\">How many prime numbers ?: 4<\/span><br \/>\n<span style=\"color: #ff0000;\">1<\/span><br \/>\n<span style=\"color: #ff0000;\">2<\/span><br \/>\n<span style=\"color: #ff0000;\">3<\/span><br \/>\n<span style=\"color: #ff0000;\">5<\/span><br \/>\n<span style=\"color: #ff0000;\">7<\/span><\/p>\n<p><span style=\"color: #0000ff;\">.\/bash.sh | awk '{print NR, $0}'<\/span><br \/>\n<span style=\"color: #ff0000;\">How many prime numbers ?: 4<\/span><br \/>\n<span style=\"color: #ff0000;\">1 1<\/span><br \/>\n<span style=\"color: #ff0000;\">2 2<\/span><br \/>\n<span style=\"color: #ff0000;\">3 3<\/span><br \/>\n<span style=\"color: #ff0000;\">4 5<\/span><br \/>\n<span style=\"color: #ff0000;\">5 7<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_4339\" class=\"pvc_stats all  \" data-element-id=\"4339\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>Create yourself a copy of the awk\/sed man page - it's easier to read: man -t awk | ps2pdf - &gt; awkman.pdf man -t sed\u00a0| ps2pdf - &gt; sedman.pdf From sed &amp; awk by OReilly - older kernel\/system example problems...examples don't copy in lines, but columns often, and major parts of examples are assumed knowledge\/steps, <a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/awk-lines-and-info\/\" class=\"more-link\">...<span class=\"screen-reader-text\">\u00a0 Awk\/Perl\/SED Notepad<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4339","page","type-page","status-publish","hentry"],"a3_pvc":{"activated":true,"total_views":4,"today_views":0},"_links":{"self":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/pages\/4339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/comments?post=4339"}],"version-history":[{"count":9,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/pages\/4339\/revisions"}],"predecessor-version":[{"id":10337,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/pages\/4339\/revisions\/10337"}],"wp:attachment":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/media?parent=4339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}