Awk: Difference between revisions

reformat the pre section into mediawiki syntax
m Add image
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[File:Application-x-awk.svg|thumb]]
== Examples ==
; get first column from mw table data
awk '/\|-/{getline;print}' /tmp/terror.txt | sed s/\|// | sed s/'^ *'// | sort -u
; see who's trying to SSH into your server (brute force attack)
awk '/Invalid user/ {print $8}' /var/log/auth.log | sort | uniq -c
Tricky use of Output Record Separator (ORS) to use '''space''' instead of newline between records. Oddly enough, you don't need to escape the double quote within the double quote because the awk command is in single quotes.
; print a apt purge command for all the 'removed' 'configured' packages on your system
echo "apt purge $(dpkg -l | egrep '^rc' | awk 'ORS=" " {print $2}')"
== Docs ==
The man page for Awk is 27 pages long.  Hard to read in a terminal.  Here is [[File:Awk.pdf|a PDF copy of the Awk manual|thumb]] a PDF copy of the Awk manual
== One-liners ==
== One-liners ==
<pre>
<pre>
Line 309: Line 325:
http://www.catonmat.net/blog/wp-content/plugins/wp-downloadMonitor/user_uploads/awk.cheat.sheet.pdf
http://www.catonmat.net/blog/wp-content/plugins/wp-downloadMonitor/user_uploads/awk.cheat.sheet.pdf


<center>'''''AWK (Aho, Kernighan, and Weinberger) Summary'''''</center>
<center>'''''AWK ([[wp:Alfred_Aho|Aho]], [[wp:Peter_J._Weinberger|Weinberger]], & [[wp:Brian_Kernighan|Kernighan]]) Summary'''''</center>




Line 546: Line 562:


|}
|}
'''2007 Peteris Krumins peter@catonmat.net[http://www.catonmat.net/ http://www.catonmat.net] good coders code, great reuse'''
'''2007 Peteris Krumins peter@catonmat.net[http://www.catonmat.net/ http://www.catonmat.net] good coders code, great reuse'''


Variable is supported:'''Y'''
Variable is supported:'''Y'''
Line 616: Line 632:
| | '''-W lint[=value]--lint[=value]'''
| | '''-W lint[=value]--lint[=value]'''


| | Provide warnings about constructs that are dubious or non-portable to other AWK . With argument '''fatal''', lint warnings become fatal errors. With an optional argument of '''invalid''', only warnings about things that are actually invalid are issued. (This is not fully implemented yet.)
| | Provide warnings about constructs that are dubious or non-portable to other AWK impl�s. With argument '''fatal''', lint warnings become fatal errors. With an optional argument of '''invalid''', only warnings about things that are actually invalid are issued. (This is not fully implemented yet.)


|-
|-
Line 670: Line 686:


|}
|}
'''2007 Peteris Krumins peter@catonmat.net[http://www.catonmat.net/ http://www.catonmat.net] good coders code, great reuse'''
'''2007 Peteris Krumins peter@catonmat.net[http://www.catonmat.net/ http://www.catonmat.net] good coders code, great reuse'''


'''Statements and Functions:'''
'''Statements and Functions:'''
Line 691: Line 707:
|-
|-
| | '''getline'''
| | '''getline'''
| | Set $0 from next input record; set NF, NR, FNR. Returns 0 on EOF and on an error. Upon an error, ERRNO contains a string describing the problem.  
| | Set $0 from next input record; set NF, NR, FNR. Returns 0 on EOF and �1 on an error. Upon an error, ERRNO contains a string describing the problem.