PLDB
Languages Features Calendar CSV Lists Blog About Sponsor Add Language
GitHub icon

awk

awk

awk, aka Aho, Weinberger, Kernighan, is a pl created in 1977 by Alfred Aho and Peter J. Weinberger and Brian Kernighan.

#59on PLDB 45Years Old 4.7kUsers
26Books 8Papers 3kRepos

Try now: Riju · TIO

AWK is a programming language designed for text processing and typically used as a data extraction and reporting tool. It is a standard feature of most Unix-like operating systems. The AWK language is a data-driven scripting language consisting of a set of actions to be taken against streams of textual data – either run directly on files or used as part of a pipeline – for purposes of extracting or transforming text, such as producing formatted reports. Read more on Wikipedia...


Example from Riju:
BEGIN { print "Hello, world!" }
Example from hello-world:
# awk -f awk.awk BEGIN { print "Hello World" }
Example from the Hello World Collection:
# Hello world in AWK BEGIN { print "Hello World!" }
Example from Linguist:
#!/bin/awk -f BEGIN { # It is not possible to define output file names here because # FILENAME is not define in the BEGIN section n = ""; printf "Generating data files ..."; network_max_bandwidth_in_byte = 10000000; network_max_packet_per_second = 1000000; last3 = 0; last4 = 0; last5 = 0; last6 = 0; } { if ($1 ~ /Average/) { # Skip the Average values n = ""; next; } if ($2 ~ /all/) { # This is the cpu info print $3 > FILENAME".cpu.user.dat"; # print $4 > FILENAME".cpu.nice.dat"; print $5 > FILENAME".cpu.system.dat"; # print $6 > FILENAME".cpu.iowait.dat"; print $7 > FILENAME".cpu.idle.dat"; print 100-$7 > FILENAME".cpu.busy.dat"; } if ($2 ~ /eth0/) { # This is the eth0 network info if ($3 > network_max_packet_per_second) print last3 > FILENAME".net.rxpck.dat"; # Total number of packets received per second. else { last3 = $3; print $3 > FILENAME".net.rxpck.dat"; # Total number of packets received per second. } if ($4 > network_max_packet_per_second) print last4 > FILENAME".net.txpck.dat"; # Total number of packets transmitted per second. else { last4 = $4; print $4 > FILENAME".net.txpck.dat"; # Total number of packets transmitted per second. } if ($5 > network_max_bandwidth_in_byte) print last5 > FILENAME".net.rxbyt.dat"; # Total number of bytes received per second. else { last5 = $5; print $5 > FILENAME".net.rxbyt.dat"; # Total number of bytes received per second. } if ($6 > network_max_bandwidth_in_byte) print last6 > FILENAME".net.txbyt.dat"; # Total number of bytes transmitted per second. else { last6 = $6; print $6 > FILENAME".net.txbyt.dat"; # Total number of bytes transmitted per second. } # print $7 > FILENAME".net.rxcmp.dat"; # Number of compressed packets received per second (for cslip etc.). # print $8 > FILENAME".net.txcmp.dat"; # Number of compressed packets transmitted per second. # print $9 > FILENAME".net.rxmcst.dat"; # Number of multicast packets received per second. } # Detect which is the next info to be parsed if ($2 ~ /proc|cswch|tps|kbmemfree|totsck/) { n = $2; } # Only get lines with numbers (real data !) if ($2 ~ /[0-9]/) { if (n == "proc/s") { # This is the proc/s info print $2 > FILENAME".proc.dat"; # n = ""; } if (n == "cswch/s") { # This is the context switches per second info print $2 > FILENAME".ctxsw.dat"; # n = ""; } if (n == "tps") { # This is the disk info print $2 > FILENAME".disk.tps.dat"; # total transfers per second print $3 > FILENAME".disk.rtps.dat"; # read requests per second print $4 > FILENAME".disk.wtps.dat"; # write requests per second print $5 > FILENAME".disk.brdps.dat"; # block reads per second print $6 > FILENAME".disk.bwrps.dat"; # block writes per second # n = ""; } if (n == "kbmemfree") { # This is the mem info print $2 > FILENAME".mem.kbmemfree.dat"; # Amount of free memory available in kilobytes. print $3 > FILENAME".mem.kbmemused.dat"; # Amount of used memory in kilobytes. This does not take into account memory used by the kernel itself. print $4 > FILENAME".mem.memused.dat"; # Percentage of used memory. # It appears the kbmemshrd has been removed from the sysstat output - ntolia # print $X > FILENAME".mem.kbmemshrd.dat"; # Amount of memory shared by the system in kilobytes. Always zero with 2.4 kernels. # print $5 > FILENAME".mem.kbbuffers.dat"; # Amount of memory used as buffers by the kernel in kilobytes. print $6 > FILENAME".mem.kbcached.dat"; # Amount of memory used to cache data by the kernel in kilobytes. # print $7 > FILENAME".mem.kbswpfree.dat"; # Amount of free swap space in kilobytes. # print $8 > FILENAME".mem.kbswpused.dat"; # Amount of used swap space in kilobytes. print $9 > FILENAME".mem.swpused.dat"; # Percentage of used swap space. # n = ""; } if (n == "totsck") { # This is the socket info print $2 > FILENAME".sock.totsck.dat"; # Total number of used sockets. print $3 > FILENAME".sock.tcpsck.dat"; # Number of TCP sockets currently in use. # print $4 > FILENAME".sock.udpsck.dat"; # Number of UDP sockets currently in use. # print $5 > FILENAME".sock.rawsck.dat"; # Number of RAW sockets currently in use. # print $6 > FILENAME".sock.ip-frag.dat"; # Number of IP fragments currently in use. # n = ""; } } } END { print " '" FILENAME "' done."; }
Example from Wikipedia:
BEGIN { pattern = ARGV[1] for (i = 1; i < ARGC; i++) # remove first argument ARGV[i] = ARGV[i + 1] ARGC-- if (ARGC == 1) { # the pattern was the only thing, so force read from standard input (used by book) ARGC = 2 ARGV[1] = "-" } } $0 ~ pattern { print FILENAME ":" $0 }

Language features

Feature Supported Example Token
Integers
# [0-9]+
Floats
# [0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?
Hexadecimals
# 0x[0-9a-fA-F]+
Strings
"Hello world"
"
Print() Debugging print
Line Comments
# A comment
#
Comments
Semantic Indentation ϴ

Books about awk on goodreads

title author year reviews ratings rating
The AWK Programming Language Alfred V. Aho 1988 11 142 4.25
Effective awk Programming: Text Processing and Pattern Matching Arnold Robbins 1997 2 37 3.97
GAWK: Effective Awk Programming Arnold D. Robbins 1996 0 1 4.00
AWK Programming: Questions and Answers George Duckett 2014 0 0 0.0
awk Programmer's Toolbox: Advanced awk and Unix Shell Scripting Examples and Techniques Steve Myers 2013 0 1 5.00
Awk Programming (2 Days) Sim McNally 0 0 0.0

Books about awk from ISBNdb

title authors year publisher
sed & awk Dougherty, Dale and Robbins, Arnold 1997 O'Reilly Media
The AWK Programming Language Aho, Alfred V. and Kernighan, Brian W. and Weinberger, Peter J. 1988 Pearson
Effective awk Programming (3rd Edition) Robbins, Arnold 2001 O'Reilly Media
Effective awk Programming: Universal Text Processing and Pattern Matching Robbins, Arnold 2015 O'Reilly Media
Learning AWK Programming: A fast, and simple cutting-edge utility for text-processing on the Unix-like environment Kalkhanda, Shiwang 2018 Packt Publishing
Learning AWK Programming: A fast, and simple cutting-edge utility for text-processing on the Unix-like environment Kalkhanda, Shiwang 2018-03-26T00:00:01Z Packt Publishing
Hands-On Korn Shell and AWK Scripting: Learn Unix and Linux Programming Through Advanced Scripting Examples Williams, Brian 2013 CreateSpace Independent Publishing Platform
sed & awk Dale Dougherty; Arnold Robbins 19970301 O'Reilly Media, Inc.
Effective Awk Programming Arnold Robbins 1997 O'reilly Media
sed & awk Dale Dougherty; Arnold Robbins 19970301 O'Reilly Media, Inc.
Effective awk Programming Arnold Robbins 20150303 O'Reilly Media, Inc.
Effective Awk Programming: A User's Guide For Gnu Awk, Edition 1.0.3 Arnold D. Robbins 2000 Iuniverse Inc
Effective awk Programming Arnold Robbins 20150303 O'Reilly Media, Inc.
Awk Programming (2 Days) Sim Mcnally 2000 D D C Pub
Gawk: Effective Awk Programming Arnold Robbins 2011 Lulu.com
sed and awk Pocket Reference Arnold Robbins 20020612 O'Reilly Media, Inc.
sed and awk Pocket Reference Arnold Robbins 20020612 O'Reilly Media, Inc.
Effective Awk Programming: A User's Guide For Gnuawk Arnold D. Robbins 1996 Specialized Systems Consultants
Unix Command Line And Awk Scripting: Harnessing The Power Of Unix And Linux Programming Environments Dmitri Petrovic 2013 Createspace Independent Publishing Platform
Advanced Unix Shell Scripting: How To Reduce Your Labor And Increase Your Effectiveness Through Mastery Of Unix Shell Scripting And Awk Programming Praveen Puri 2013 Createspace Independent Publishing Platform

Publications about awk from Semantic Scholar

title authors year citations influentialCitations
The awk programming language A. Aho and B. Kernighan and P. Weinberger 1988 464 23
Awk — a pattern scanning and processing language A. Aho and B. Kernighan and P. Weinberger 1979 155 7
AWK and GNU Octave Programming Languages Integrated with Generic Mapping Tools for Geomorphological Analysis Polina Lemenkova 2019 41 0
A walk through AWK L. Levy 1983 34 0
A debugger and assertion checker for the Awk programming language M. Auguston and S. Banerjee and M. Mamnani and G. Nabi and J. Reinfelds and U. Sarkans and I. Strnad 1996 6 0
From AWK to Google: Peter Weinberger Talks Search L. McLaughlin 2005 1 0
AWK — A Prototyping Language L. Levy 1987 1 0
The awk programming language [Book Review] Brian and Kemighan and P. Weinberger 1989 1 0
nodejs.html · awk.html · f-sharp.html

View source

PLDB - Build the next great programming language · v2022 · Day 28 · Docs · Build · Acknowledgements · Traffic Today · Traffic Trends · Mirrors · GitHub · feedback@pldb.com