'''[http://awk.info/%|%Awk]''' is a [text processing] language named
named after the initials of its authors, Aho, Weinberger, Kernighan.
** Attributes **
website: http://awk.info/
website: http://cm.bell-labs.com/cm/cs/awkbook/index.html
** Documentation **
[http://cm.bell-labs.com/cm/cs/awkbook/index.html%|%The Awk Programming Language], Aho, Kernighan, Weinberger, 1988:
[http://www.gnu.org/software/gawk/manual/gawk.html%|%The GNU Awk User's Guide]: the guide for one of the most widely-used Awk implementations
[http://www.vectorsite.net/tsawk.html%|%An Awk Primer], Greg Goebel, updated 2014-04-01: a comprehensive intro to awk, suitable to beginners.
[http://www.catonmat.net/blog/awk-one-liners-explained-part-one/%|%Famous Awk One-Liners Explained, Part I: File Spacing, Numbering and Calculations], Peteris Krumins, 2008-09-27: a handy FAQ for common tasks.
** Community **
the [usenet] [newsgroup] news:comp.lang.awk:
** Description **
'''Awk''' is a standard program on most (all?) [Unix]-like [operating
system%|%operating systems].
Awk's ability to scan through a file and manipulate the contents pre-dates [Perl]'s functionalitys to do this, and frankly awk's abilities,
while cruder in many ways, are also simpler (simpler even than Tcl!).
Tcl is a good candidate for any tasks that might be programmed in Awk, but is a
more general language, and in terms of sheer speed, is outperformed by Awk at
many text processing and data extraction tasks. [Tcl Core Team%|%Core team]
member [Alexandre Ferrieux] is interested in erasing this performance
difference, so maybe that will change.
[LV]: Awk is one of my favorite languages in which to write...
** Using Awk from Tcl **
[LV]: A common question is:
How can I invoke awk scripts from Tcl?
Here is nhow '''not''' to do it:
======none
$ tclsh
% set a [exec awk {'{print $1}'} /etc/motd]
awk: cmd. line:1: '{print $1}'
awk: cmd. line:1: ^ invalid char ''' in expression
======
[RS] answers: This is not an Awk problem, but improper [shell] quoting quoting:
single quotes in a shell have the effect as braces in Tcl - group in one word, don't substitute on contents. Solution here: You have outer braces already, so just drop the single quotes (the inner brace pair is awk syntax, not seen by Tcl):
======none
% set a [exec awk {{print $1}} /etc/motd]
======
** Look Ma, No Awk! **
[RS] 2007-02-07: I love this few-liner that allows tests in a subset of Awk notation (in fact, the common subset of Awk and `[expr]`, plus a shortcut for `[regexp]`):
======
proc awktest {filter 0} {
if {[regexp {^/(.+)/$} $filter -> re]} {return [regexp $re $0]}
set i 0
foreach field $0 {set [incr i] $field}
expr $filter
}
======
e.g.:
======
awktest {$1==$2} {foo bar grill} ;#-> 0.
======
`$0` is just the input list :^) The following shortcut is also cute:
======
interp alias {} ~ {} regexp
======
----
[RS] 2007-11-08: When porting Awk scripts, it's also helpful to have the numbered variables:
======
proc awksplit {list sep} {
set i 0
foreach field [split $list $sep] {uplevel 1 [list set [incr i] $field]}
upvar 1 [list set NF $i]
}
======
where for example `awksplit {foo;bar;grill} {;}` assigns `foo` to `$1`, `bar` to `$2`, `grill` to `$3`.
----
[RS] 2007-02-28: Here's another piece of awk emulation:
======
proc substr {str from length} {
string range $str [expr {$from-1}] [expr {$from-2+$length}]
}
======
======none
% substr hello 2 2
el
% substr hello 2 99
ello
======
And this is trivial, but over 50% shorter to type:
======
interp alias {} length {} string length
======
** Tools **
[http://www.tikmark.com/Tclforawk/Tclforawk.html%|%Tclforawk]: a graphical front-end for managing and running Awk scripts. It acts like a combined file selector, text editor and terminal window, which makes the process of running Awk simpler if you have lots of scripts.
[tclforawk04.jpg] [tclforawk05.jpg]
** See Also **
* [how can I do this awk like operation in Tcl]
* [Scan and modify text files]
* [Text processing tips] * `[exec]`: includes an example of invoking Awk, something often requested.
* [BOOK Mastering Regular Expressions]: covers [regular expressions] in [perl], Awk, and [tcl]. * [owh - a fileless tclsh]: a Tcl variation on awk functionalitys
* [env changing arguments]: how to pass optional parameters to a script, [awk]-like
** Reference ** * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html%|%awk], The Open Group Base Specifications Issue 7, 2013%|% - login required.
* https://en.wikipedia.org/wiki/AWK%|%AWK at Wikipedia%|%
* http://rosettacode.org/wiki/Category:AWK%|%AWK at RosettaCode%|%
<<categories>> Language | String Processing