LMAWK(1)
๐ LMAWK(1) โ USER COMMANDS ๐
okay bestie, welcome to the lmawk page โ manual section 1, USER COMMANDS energy. This is the pattern-scanning, text-processing language that's about to become your main character. We're doing part 1, so buckle up. โจ
๐ธ NAME ๐ธ
lmawkโ pattern scanning and text processing language. periodt.
๐ฆ SYNOPSIS ๐ฆ
lmawk [-W option] [-F value] [-v var=value] [--] 'program text' [file ...]
lmawk [-W option] [-F value] [-v var=value] [-f program-file] [--] [file ...]
โจ DESCRIPTION โจ
lmawk is literally an interpreter for the AWK Programming Language, and it's derived from mawk. The AWK language is useful for manipulation of data files, text retrieval and processing, and for prototyping and experimenting with algorithms. lmawk is a new awk, meaning it implements the AWK language as defined in Aho, Kernighan and Weinberger, The AWK Programming Language, Addison-Wesley Publishing, 1988. (Hereafter referred to as the AWK book.) mawk conforms to the Posix 1003.2 (draft 11.3) definition of the AWK language which contains a few features not described in the AWK book, and mawk provides a small number of extensions.
An AWK program is a sequence of pattern {action} pairs and function definitions. Short programs are entered on the command line, usually wrapped in ' ' so the shell doesn't get any ideas. Longer programs can be read in from a file with the -f option. Data input is read from the list of files on the command line, or from standard input when the list is empty. The input is broken into records as determined by the record separator variable, RS. Initially, RS = "\n" and records are synonymous with lines. Each record is compared against each pattern and if it matches, the program text for {action} is executed.
๐ OPTIONS ๐
-F valueโ sets the field separator,FS, tovalue.-f fileโ Program text is read fromfileinstead of from the command line. Multiple-foptions are allowed. As a libmawk extension, if file name starts with plus ('+'), it is not loaded if the same file has been loaded already by a previous-for include from any of the scripts already loaded.-b fileโ Program bytecode is read fromfile. Multiple-boptions are allowed. Bytecode can be generated using-Wcompile. Libmawk may refuse to load bytecode generated on a different system if byte order, type sizes or dump version differs.-v var=valueโ assignsvalueto program variablevar.--โ indicates the unambiguous end of options.
The above options will be available with any Posix compatible implementation of AWK, and implementation specific options are prefaced with -W. lmawk provides six:
-W versionโlmawkwrites its version and copyright to stdout and compiled limits to stderr and exits0.-W debugโ include location info in the compiled code; location information is visible in the dump and when debugging libmawk.-W dumpโ writes an assembler like listing of the internal representation of the program to stdout and exits0(on successful compilation).-W dumpsymโ writes a list of global symbols to stdout and exits0(on successful compilation).-W compileโ writes a binary dump of the bytecode to stdout. This bytecode can be loaded using the-bswitch.-W interactiveโ sets unbuffered writes to stdout and line buffered reads from stdin. Records from stdin are lines regardless of the value ofRS.-W maxmem=numโ limit dynamic memory allocation during compilation and execution tonumbytes and exit with out-of-the-memory error if more memory is to be allocated. Optional suffixes arekfor kilobyte andmfor megabyte.0means unlimited, which is also the default.-W exec fileโ Program text is read fromfileand this is the last option. Useful on systems that support the#!"magic number" convention for executable scripts.-W sprintf=numโ adjusts the size oflmawk's internal sprintf buffer tonumbytes. More than rare use of this option indicateslmawkshould be recompiled.-W posix_spaceโ forceslmawknot to consider'\n'to be space.
The short forms -W[vdiesp] are recognized and on some systems -We is mandatory to avoid command line length limitations.
๐ THE AWK LANGUAGE ๐
1. Program structure
An AWK program is a sequence of pattern {action} pairs and user function definitions.
A pattern can be:
BEGINENDexpressionexpression , expression
One, but not both, of pattern {action} can be omitted. If {action} is omitted it is implicitly { print }. If pattern is omitted, then it is implicitly matched. BEGIN and END patterns require an action.
Statements are terminated by newlines, semi-colons or both. Groups of statements such as actions or loop bodies are blocked via { ... } as in C. The last statement in a block doesn't need a terminator. Blank lines have no meaning; an empty statement is terminated with a semi-colon. Long statements can be continued with a backslash, \. A statement can be broken without a backslash after a comma, left brace, &&, ||, do, else, the right parenthesis of an if, while or for statement, and the right parenthesis of a function definition. A comment starts with # and extends to, but does not include the end of line.
The following statements control program flow inside blocks.
if ( expr ) statement
if ( expr ) statement else statement
while ( expr ) statement
do statement while ( expr )
for ( opt_expr ; opt_expr ; opt_expr ) statement
for ( var in array ) statement
continue
break
2. Data types, conversion and comparison
There are two basic data types, numeric and string. Numeric constants can be integer like -2, decimal like 1.08, or in scientific notation like -1.1e4 or .28E-3. All numbers are represented internally and all computations are done in floating point arithmetic. So for example, the expression 0.2e2 == 20 is true and true is represented as 1.0.
String constants are enclosed in double quotes.
"This is a string with a newline at the end.\n"
Strings can be continued across a line by escaping (\) the newline. The following escape sequences are recognized.
\\โ\\"โ"\aโ alert, ascii 7\bโ backspace, ascii 8\tโ tab, ascii 9\nโ newline, ascii 10\vโ vertical tab, ascii 11\fโ formfeed, ascii 12\rโ carriage return, ascii 13\dddโ 1, 2 or 3 octal digits for asciiddd\xhhโ 1 or 2 hex digits for asciihh
If you escape any other character \c, you get \c, i.e., lmawk ignores the escape.
There are really three basic data types; the third is number and string which has both a numeric value and a string value at the same time. User defined variables come into existence when first referenced and are initialized to null, a number and string value which has numeric value 0 and string value "". Non-trivial number and string typed data come from input and are typically stored in fields. (See section 4).
The type of an expression is determined by its context and automatic type conversion occurs if needed. For example, to evaluate the statements
y = x + 2 ; z = x "hello"
The value stored in variable y will be typed numeric. If x is not numeric, the value read from x is converted to numeric before it is added to 2 and stored in y. The value stored in variable z will be typed string, and the value of x will be converted to string if necessary and concatenated with "hello". (Of course, the value and type stored in x is not changed by any conversions.) A string expression is converted to numeric using its longest numeric prefix as with atof(3). A numeric expression is converted to string by replacing expr with sprintf(CONVFMT, expr), unless expr can be represented on the host machine as an exact integer then it is converted to sprintf("%d", expr). Sprintf() is an AWK built-in that duplicates the functionality of sprintf(3), and CONVFMT is a built-in variable used for internal conversion from number to string and initialized to "%.6g". Explicit type conversions can be forced, expr "" is string and expr+0 is numeric.
To evaluate, expr1 rel-op expr2, if both operands are numeric or number and string then the comparison is numeric; if both operands are string the comparison is string; if one operand is string, the non-string operand is converted and the comparison is string. The result is numeric, 1 or 0.
In boolean contexts such as, if ( expr ) statement, a string expression evaluates true if and only if it is not the empty string ""; numeric values if and only if not numerically zero.
3. Regular expressions
In the AWK language, records, fields and strings are often tested for matching a regular expression. Regular expressions are enclosed in slashes, and
expr ~ /r/
is an AWK expression that evaluates to 1 if expr "matches" r, which means a substring of expr is in the set of strings defined by r. With no match the expression evaluates to 0; replacing ~ with the "not match" operator, !~ , reverses the meaning. As pattern-action pairs,
/r/ { action } and $0 ~ /r/ { action }
are the same, and for each input record that matches r, action is executed. In fact, /r/ is an AWK expression that is equivalent to ($0 ~ /r/) anywhere except when on the right side of a match operator or passed as an argument to a built-in function that expects a regular expression argument.
AWK uses extended regular expressions as with egrep(1). The regular expression metacharacters, i.e., those with special meaning in regular expressions are
^ $ . [ ] | ( ) * + ?
Regular expressions are built up from characters as follows:
cโ matches any non-metacharacterc.\cโ matches a character defined by the same escape sequences used in string constants or the literal charactercif\cis not an escape sequence..โ matches any character (including newline).^โ matches the front of a string.$โ matches the back of a string.[c1c2c3...]โ matches any character in the classc1c2c3.... An interval of characters is denotedc1-c2inside a class[...].[^c1c2c3...]โ matches any character not in the classc1c2c3...
Regular expressions are built up from other regular expressions as follows:
okay so real quick, the regex operator vibes: ๐
r1r2โ matchesr1followed immediately byr2(that's concatenation, bestie).r1 | r2โ matchesr1orr2(alternation, choose your fighter).r*โ matchesrrepeated zero or more times.r+โ matchesrrepeated one or more times.r?โ matchesrzero or once.(r)โ matchesr, providing grouping (group chat but for regex).
The increasing precedence of operators is alternation, concatenation and unary (*, + or ?).
For example,
/^[_a-zA-Z][_a-zA-Z0-9]*$/ and
/^[-+]?([0-9]+\.?|\.[0-9])[0-9]*([eE][-+]?[0-9]+)?$/
are matched by AWK identifiers and AWK numeric constants respectively. Note that . has to be escaped to be recognized as a decimal point, and that metacharacters are not special inside character classes. (iykyk)
Any expression can be used on the right hand side of the ~ or !~ operators or passed to a built-in that expects a regular expression. If needed, it is converted to string, and then interpreted as a regular expression. For example,
BEGIN { identifier = "[_a-zA-Z][_a-zA-Z0-9]*" }
$0 ~ "^" identifier
prints all lines that start with an AWK identifier.
lmawk recognizes the empty regular expression, //, which matches the empty string and hence is matched by any string at the front, back and between every character. For example,
echo abc | lmawk { gsub(//, "X") ; print }
XaXbXcX
4. Records and fields ๐
Records are read in one at a time, and stored in the field variable $0. The record is split into fields which are stored in $1, $2, ..., $NF. The built-in variable NF is set to the number of fields, and NR and FNR are incremented by 1. Fields above $NF are set to "".
Assignment to $0 causes the fields and NF to be recomputed. Assignment to NF or to a field causes $0 to be reconstructed by concatenating the $i's separated by OFS. Assignment to a field with index greater than NF, increases NF and causes $0 to be reconstructed.
Data input stored in fields is string, unless the entire field has numeric form and then the type is number and string. For example,
echo 24 24E |
lmawk '{ print($1>100, $1>"100", $2>100, $2>"100") }'
0 1 1 1
$0 and $2 are string and $1 is number and string. The first comparison is numeric, the second is string, the third is string (100 is converted to "100"), and the last is string.
5. Expressions and operators ๐งฎ
The expression syntax is similar to C. Primary expressions are numeric constants, string constants, variables, fields, arrays and function calls. The identifier for a variable, array or function can be a sequence of letters, digits and underscores, that does not start with a digit. Variables are not declared; they exist when first referenced and are initialized to null.
New expressions are composed with the following operators in order of increasing precedence.
- assignment โ
= += -= *= /= %= ^= - conditional โ
? : - logical or โ
|| - logical and โ
&& - array membership โ
in - matching โ
~ !~ - relational โ
< > <= >= == != - concatenation โ (no explicit operator)
- add ops โ
+ - - mul ops โ
* / % - unary โ
+ - - logical not โ
! - exponentiation โ
^ - inc and dec โ
++ -- (both post and pre) - field โ
$
Assignment, conditional and exponentiation associate right to left; the other operators associate left to right. Any expression can be parenthesized.
6. Arrays ๐
Awk provides one-dimensional arrays. Array elements are expressed as array[expr]. Expr is internally converted to string type, so, for example, A[1] and A["1"] are the same element and the actual index is "1". Arrays indexed by strings are called associative arrays. Initially an array is empty; elements exist when first accessed. An expression, expr in array evaluates to 1 if array[expr] exists, else to 0.
There is a form of the for statement that loops over each index of an array.
for ( var in array ) statement
sets var to each index of array and executes statement. The order that var transverses the indices of array is not defined.
The statement, delete array[expr], causes array[expr] not to exist. lmawk supports an extension, delete array, which deletes all elements of array.
Multidimensional arrays are synthesized with concatenation using the built-in variable SUBSEP. array[expr1,expr2] is equivalent to array[expr1 SUBSEP expr2]. Testing for a multidimensional element uses a parenthesized index, such as
if ( (i, j) in A ) print A[i, j]
7. Builtin-variables ๐
The following variables are built-in and initialized before program execution.
ARGCโ number of command line arguments.ARGVโ array of command line arguments,0..ARGC-1.CONVFMTโ format for internal conversion of numbers to string, initially ="%.6g".ENVIRONโ array indexed by environment variables. An environment string,var=valueis stored asENVIRON[var] = value.FILENAMEโ name of the current input file.FNRโ current record number inFILENAME.FSโ splits records into fields as a regular expression.NFโ number of fields in the current record.NRโ current record number in the total input stream.OFMTโ format for printing numbers; initially ="%.6g".OFSโ inserted between fields on output, initially =" ".ORSโ terminates each record on output, initially ="\n".RLENGTHโ length set by the last call to the built-in function,match().RSโ input record separator, initially ="\n".RSTARTโ index set by the last call tomatch().SUBSEPโ used to build multiple array subscripts, initially ="\034".ERRNOโ misc built-in functions (libmawk extensions) use this variable to rerport error. All extension calls will set this variable before returning, thereforERRNOholds the result of the last call. An empty string value means no error. Error messages are formatted in a way that the first word is an unique integer, followed by a human readable error message from the second word.int(ERRNO)can be used to acquire the error code, which then can be used as a secondary output from the extension function. For example, an awk program can usevalueof()to determine if a global symbol exists and is a function or a variable or anything else.LIBPATHโ is a semicolon separated list of search paths. When loading an awk script by file name (-fcommand line argument or include from another awk script) these paths are inserted before the file name, in order, one by one, until the first path that allows opening the file. An empty path is equivalent to the current working directory.LIBPATHcan be modified from the command line using-v, as arguments are scanned before loading the scripts. SettingLIBPATHto empty string results in the original behaviour ofmawk.LIBPATHis ignored for script file names starting with slash ('/') as those are assumed to be absolute paths.
8. Built-in functions โจ
String functions ๐
gsub(r,s,t)/gsub(r,s)โ Global substitution, every match of regular expressionrin variabletis replaced by strings. The number of replacements is returned. Iftis omitted,$0is used. An&in the replacement stringsis replaced by the matched substring oft.\&and\\put literal&and\, respectively, in the replacement string.index(s,t)โ Iftis a substring ofs, then the position wheretstarts is returned, else0is returned. The first character ofsis in position1.length(s)โ Returns the length of strings.match(s,r)โ Returns the index of the first longest match of regular expressionrin strings. Returns0if no match. As a side effect,RSTARTis set to the return value.RLENGTHis set to the length of the match or-1if no match. If the empty string is matched,RLENGTHis set to0, and1is returned if the match is at the front, andlength(s)+1is returned if the match is at the back.split(s,A,r)/split(s,A)โ Stringsis split into fields by regular expressionrand the fields are loaded into arrayA. The number of fields is returned. See section 11 below for more detail. Ifris omitted,FSis used.sprintf(format,expr-list)โ Returns a string constructed fromexpr-listaccording toformat. See the description ofprintf()below.sub(r,s,t)/sub(r,s)โ Single substitution, same asgsub()except at most one substitution.substr(s,i,n)/substr(s,i)โ Returns the substring of strings, starting at indexi, of lengthn. Ifnis omitted, the suffix ofs, starting atiis returned.tolower(s)โ Returns a copy ofswith all upper case characters converted to lower case.toupper(s)โ Returns a copy ofswith all lower case characters converted to upper case.
Arithmetic functions ๐งฎ
atan2(y,x)โ Arctan ofy/xbetween-PIandPI.cos(x)โ Cosine function,xin radians.exp(x)โ Exponential function.int(x)โ Returnsxtruncated towards zero.log(x)โ Natural logarithm.rand()โ Returns a random number between zero and one.sin(x)โ Sine function,xin radians.sqrt(x)โ Returns square root ofx.srand(expr)andsrand()โ Seeds the random number generator, using the clock ifexpris omitted, and returns the value of the previous seed. lmawk seeds the random number generator from the clock at startup so there is no real need to callsrand().Srand(expr)is useful for repeating pseudo random sequences. ๐ฒ
โจ Misc functions (libmawk extensions) โจ
call(fname,arg1,arg2,...)โ Call awk functionfnamewith the supplied arguments. If the call fails, empty value, else the return value of the callee is returned. Built-in variableERRNOis always set. ๐acall(fname,arrname)โ Call awk functionfnamewith arguments supplied in array namedarrname(both arguments are strings naming an existing object). The array should be indexed from 1. Number of arguments is determined by looking for the first empty (non-existing) index in the array. If the call fails, empty value, else the return value of the callee is returned. Built-in variableERRNOis always set. ๐valueof(vname [,idx])โ Return the value of variablefname; if the variable is an array, return the element indexed byidx(which must be present in this case). If index is not present or is empty (""), the variable is expected to be scalar. Built-in variableERRNOis always set. NOTE:valueof()has access to the global symbol table only. It will fail to resolve anything else than global objects; most notably it will fail on local variables,$arguments and on most of the built-in variables. ๐
๐ 9. Input and output ๐
There are two output statements, print and printf. โจ
printโ writes$0ORSto standard output.print expr1, expr2, ..., exprnโ writesexpr1OFSexpr2OFS...exprnORSto standard output. Numeric expressions are converted to string withOFMT.printf format, expr-listโ duplicates theprintfC library function writing to standard output. The complete ANSI C format specifications are recognized with conversions%c,%d,%e,%E,%f,%g,%G,%i,%o,%s,%u,%x,%Xand%%, and conversion qualifiershandl.
The argument list to print or printf can optionally be enclosed in parentheses. print formats numbers using OFMT or "%d" for exact integers. "%c" with a numeric argument prints the corresponding 8 bit character, with a string argument it prints the first character of the string. The output of print and printf can be redirected to a file or command by appending > file, >> file or | command to the end of the print statement. Redirection opens file or command only once, subsequent redirections append to the already open stream. By convention, lmawk associates the filename "/dev/stderr" with stderr which allows print and printf to be redirected to stderr. lmawk also associates "-" and "/dev/stdout" with stdin and stdout which allows these streams to be passed to functions. Opening /dev/fd/N will do an fdopen() on file descriptor N, where N is an integer - this is a libmawk extension. If any of the /dev heuristics needs to be bypassed (i.e. the script wants to open the real /dev/stdout or the real /dev/fd/5), the leading slash should be doubled (e.g. //dev/fd/5). ๐
The input function getline has the following variations. ๐
getlineโ reads into$0, updates the fields,NF,NRandFNR.getline < fileโ reads into$0fromfile, updates the fields andNF.getline varโ reads the next record intovar, updatesNRandFNR.getline var < fileโ reads the next record offileintovar.command | getlineโ pipes a record fromcommandinto$0and updates the fields andNF.command | getline varโ pipes a record fromcommandintovar.
getline returns 0 on end-of-file, -1 on error, otherwise 1.
Commands on the end of pipes are executed by /bin/sh.
The function close(expr) closes the file or pipe associated with expr. close returns 0 if expr is an open file, the exit status if expr is a piped command, and -1 otherwise. close is used to reread a file or command, make sure the other end of an output pipe is finished or conserve file resources. ๐ช
The function fflush(expr) flushes the output file or pipe associated with expr. fflush returns 0 if expr is an open output stream else -1. fflush without an argument flushes stdout. fflush with an empty argument ("") flushes all open output. ๐ง
The function system(expr) uses /bin/sh to execute expr and returns the exit status of the command expr. Changes made to the ENVIRON array are not passed to commands executed with system or pipes. ๐ฅ๏ธ
โจ 10. User defined functions โจ
The syntax for a user defined function is
function name( args ) { statements }
The function body can contain a return statement
return opt_expr
A return statement is not required. Function calls may be nested or recursive. Functions are passed expressions by value and arrays by reference. Extra arguments serve as local variables and are initialized to null. For example, csplit(s,A) puts each character of s into array A and returns the length of s. ๐งฉ
function csplit(s, A, n, i)
{
n = length(s)
for( i = 1 ; i <= n ; i++ ) A[i] = substr(s, i, 1)
return n
}
Putting extra space between passed arguments and local variables is conventional. Functions can be referenced before they are defined, but the function name and the ( of the arguments must touch to avoid confusion with concatenation. ๐ฏ
๐ฆ 11. Splitting strings, records and files ๐ฆ
Awk programs use the same algorithm to split strings into arrays with split(), and records into fields on FS. lmawk uses essentially the same algorithm to split files into records on RS.
Split(expr,A,sep) works as follows:
- If
sepis omitted, it is replaced byFS.Sepcan be an expression or regular expression. If it is an expression of non-string type, it is converted to string. - If
sep = " "(a single space), then<SPACE>is trimmed from the front and back ofexpr, andsepbecomes<SPACE>. lmawk defines<SPACE>as the regular expression/[ \t\n]+/. Otherwisesepis treated as a regular expression, except that meta-characters are ignored for a string of length 1, e.g.,split(x, A, "*")andsplit(x, A, /\*/)are the same. - If
expris not string, it is converted to string. Ifexpris then the empty string"",split()returns0andAis set empty. Otherwise, all non-overlapping, non-null and longest matches ofsepinexpr, separateexprinto fields which are loaded intoA. The fields are placed inA[1],A[2], ...,A[n]andsplit()returnsn, the number of fields which is the number of matches plus one. Data placed inAthat looks numeric is typed number and string.
Splitting records into fields works the same except the pieces are loaded into $1, $2,..., $NF. If $0 is empty, NF is set to 0 and all $i to "".
lmawk splits files into records by the same algorithm, but with the slight difference that RS is really a terminator instead of a separator. (ORS is really a terminator too).
E.g., if FS = ":+" and $0 = "a::b:" , then NF = 3 and $1 = "a",
$2 = "b" and $3 = "", but if "a::b:" is the contents of an input
file and RS = ":+", then there are two records "a" and "b".
RS = " " is not special.
If FS = "", then lmawk breaks the record into individual characters, and, similarly, split(s,A,"") places the individual characters of s into A. ๐ค
๐ 12. Multi-line records ๐
Since lmawk interprets RS as a regular expression, multi-line records are easy. Setting RS = "\n\n+", makes one or more blank lines separate records. If FS = " " (the default), then single newlines, by the rules for <SPACE> above, become space and single newlines are field separators.
For example, if a file is "a b\nc\n\n", RS = "\n\n+" and FS =
" ", then there is one record "a b\nc" with three fields "a", "b"
and "c". Changing FS = "\n", gives two fields "a b" and "c";
changing FS = "", gives one field identical to the record.
If you want lines with spaces or tabs to be considered blank, set RS = "\n([ \t]*\n)+". For compatibility with other awks, setting RS = "" has the same effect as if blank lines are stripped from the front and back of files and then records are determined as if RS = "\n\n+". Posix requires that "\n" always separates records when RS = "" regardless of the value of FS. lmawk does not support this convention, because defining "\n" as <SPACE> makes it unnecessary.
Most of the time when you change RS for multi-line records, you will also want to change ORS to "\n\n" so the record spacing is preserved on output. ๐
๐ 13. Program execution ๐
This section describes the order of program execution. First ARGC is set to the total number of command line arguments passed to the execution phase of the program. ARGV[0] is set the name of the AWK interpreter and ARGV[1] ... ARGV[ARGC-1] holds the remaining command line arguments exclusive of options and program source. For example with
lmawk -f prog v=1 A t=hello B
ARGC = 5 with ARGV[0] = "lmawk", ARGV[1] = "v=1", ARGV[2] = "A", ARGV[3] = "t=hello" and ARGV[4] = "B".
Next, each BEGIN block is executed in order. If the program consists entirely of BEGIN blocks, then execution terminates, else an input stream is opened and execution continues. If ARGC equals 1, the input stream is set to stdin, else the command line arguments ARGV[1] ... ARGV[ARGC-1] are examined for a file argument. ๐
okay so bestie, when you're passing stuff on the command line, it splits into three little cliques: file arguments, assignment arguments, and empty strings "". An assignment looks like var=string. So when ARGV[i] gets peeked at as a possible file argument: if it's empty, it gets skipped, bye; if it's an assignment argument, the assignment to var happens and i skips to the next argument; otherwise ARGV[i] is opened for input. If it fails to open, execution terminates with exit code 2. If no command line argument is a file argument, then input comes from stdin. Getline in a BEGIN action opens input. And "-" as a file argument denotes stdin. ๐
Once an input stream is open, each input record is tested against each pattern, and if it matches, the associated action is executed. An expression pattern matches if it is boolean true (see the end of section 2). A BEGIN pattern matches before any input has been read, and an END pattern matches after all input has been read. A range pattern, expr1,expr2, matches every record between the match of expr1 and the match expr2 inclusively. โจ
When end of file occurs on the input stream, the remaining command line arguments are examined for a file argument, and if there is one it is opened, else the END pattern is considered matched and all END actions are executed. ๐ธ
In the example, the assignment v=1 takes place after the BEGIN actions are executed, and the data placed in v is typed number and string. Input is then read from file A. On end of file A, t is set to the string "hello", and B is opened for input. On end of file B, the END actions are executed. ๐
Program flow at the pattern {action} level can be changed with the
next
exit opt_expr
statements. A next statement causes the next input record to be read and pattern testing to restart with the first pattern {action} pair in the program. An exit statement causes immediate execution of the END actions or program termination if there are none or if the exit occurs in an END action. The opt_expr sets the exit value of the program unless overridden by a later exit or subsequent error. ๐ฅ
โจ 14. include โจ
libmawk introduces source inclusion feature. Syntax is:
include "filename"
Include statements must be on top level (outside of blocks). If file name starts with a plus sign ('+'), the script file is not loaded if it has been already loaded (by another include or -f command line argument). ๐ซถ
๐ EXAMPLES ๐
- emulate
cat.
{ print }
- emulate
wc.
{ chars += length($0) + 1 # add one for the \n
words += NF
}
END{ print NR, words, chars }
- count the number of unique "real words".
BEGIN { FS = "[^A-Za-z]+" }
{ for(i = 1 ; i <= NF ; i++) word[$i] = "" }
END { delete word[""]
for ( i in word ) cnt++
print cnt
}
- sum the second field of every record based on the first field.
$1 ~ /credit|gain/ { sum += $2 }
$1 ~ /debit|loss/ { sum -= $2 }
END { print sum }
- sort a file, comparing as string
{ line[NR] = $0 "" } # make sure of comparison type
# in case some lines look numeric
END { isort(line, NR)
for(i = 1 ; i <= NR ; i++) print line[i]
}
#insertion sort of A[1..n]
function isort( A, n, i, j, hold)
{
for( i = 2 ; i <= n ; i++)
{
hold = A[j = i]
while ( A[j-1] > hold )
{ j-- ; A[j+1] = A[j] }
A[j] = hold
}
# sentinel A[0] = "" will be created if needed
}
๐ COMPATIBILITY ISSUES ๐
The Posix 1003.2(draft 11.3) definition of the AWK language is AWK as described in the AWK book with a few extensions that appeared in SystemVR4 nawk. The extensions are:
- New functions:
toupper()andtolower(); libmawk extensions:call(),acall(),valueof(). - New variables:
ENVIRON[]andCONVFMT; libmawk extension:ERRNO,LIBPATH. As a libmawk extension,ENVIRONaffects the environment of children processes. - As a libmawk extension, new built-in variable
LIBPATHis used as a list of search paths while loading scripts from the command line or frominclude. - If a script name starts with plus (
'+'), the file is not loaded if it has been loaded earlier (to avoid double loading libs trough-fand/orinclude). This is a libmawk extension. - It is possible to include a script from another script using keyword
include "scriptname.awk"(libmawk extension). - ANSI C conversion specifications for
printf()andsprintf(). - New command options:
-v var=value, multiple-foptions and implementation options as arguments to-W.
Posix AWK is oriented to operate on files a line at a time. RS can be changed from "\n" to another single character, but it is hard to find any use for this โ there are no examples in the AWK book. By convention, RS = "", makes one or more blank lines separate records, allowing multi-line records. When RS = "", "\n" is always a field separator regardless of the value in FS. ๐ธ
lmawk, on the other hand, allows RS to be a regular expression. When "\n" appears in records, it is treated as space, and FS always determines fields. โจ
Removing the line at a time paradigm can make some programs simpler and can often improve performance. For example, redoing example 3 from above,
BEGIN { RS = "[^A-Za-z]+" }
{ word[ $0 ] = "" }
END { delete word[ "" ]
for( i in word ) cnt++
print cnt
}
counts the number of unique words by making each word a record. On moderate size files, lmawk executes twice as fast, because of the simplified inner loop. ๐ฅ
The following program replaces each comment by a single space in a C program file,
BEGIN {
RS = "/\*([^*]|\*+[^/*])*\*+/"
# comment is record separator
ORS = " "
getline hold
}
{ print hold ; hold = $0 }
END { printf "%s" , hold }
Buffering one record is needed to avoid terminating the last record with a space. ๐
With lmawk, the following are all equivalent,
x ~ /a\+b/ x ~ "a\+b" x ~ "a\\+b"
The strings get scanned twice, once as string and once as regular expression. On the string scan, lmawk ignores the escape on non-escape characters while the AWK book advocates \c be recognized as c which necessitates the double escaping of meta-characters in strings. Posix explicitly declines to define the behavior which passively forces programs that must run under a variety of awks to use the more portable but less readable, double escape. ๐ญ
Posix AWK does not recognize "/dev/std{out,err}" or \x hex escape sequences in strings. Unlike ANSI C, lmawk limits the number of digits that follows \x to two as the current implementation only supports 8 bit characters. The built-in fflush first appeared in a recent (1993) AT&T awk released to netlib, and is not part of the posix standard. Aggregate deletion with delete array is not part of the posix standard. ๐
Posix explicitly leaves the behavior of FS = "" undefined, and mentions splitting the record into characters as a possible interpretation, but currently this use is not portable across implementations. ๐
Finally, here is how lmawk handles exceptional cases not discussed in the AWK book or the Posix draft. It is unsafe to assume consistency across awks and safe to skip to the next section.
substr(s, i, n)returns the characters ofsin the intersection of the closed interval[1, length(s)]and the half-open interval[i, i+n). When this intersection is empty, the empty string is returned; sosubstr("ABC", 1, 0) = ""andsubstr("ABC", -4, 6) = "A".- Every string, including the empty string, matches the empty string at the front so,
s ~ //ands ~ "", are always1as ismatch(s, //)andmatch(s, ""). The last two setRLENGTHto0. index(s, t)is always the same asmatch(s, t1)wheret1is the same astwith metacharacters escaped. Hence consistency withmatchrequires thatindex(s, "")always returns1. Also the condition,index(s,t) != 0if and onlytis a substring ofs, requiresindex("","") = 1.- If
getlineencounters end of file,getline var, leavesvarunchanged. Similarly, on entry to theENDactions,$0, the fields andNFhave their value unaltered from the last record.
๐ฆ SEE ALSO ๐ฆ
Aho, Kernighan and Weinberger, The AWK Programming Language, Addison-Wesley Publishing, 1988, (the AWK book), defines the language, opening with a tutorial and advancing to many interesting programs that delve into issues of software design and analysis relevant to programming in any language. ๐
The GAWK Manual, The Free Software Foundation, 1991, is a tutorial and language reference that does not attempt the depth of the AWK book and assumes the reader may be a novice programmer. The section on AWK arrays is excellent. It also discusses Posix requirements for AWK. ๐
๐ BUGS ๐
lmawk cannot handle ascii NUL \0 in the source or data files. You can output NUL using printf with %c, and any other 8 bit character is acceptable input. ๐ญ
lmawk implements printf() and sprintf() using the C library functions, printf and sprintf, so full ANSI compatibility requires an ANSI C library. In practice this means the h conversion qualifier may not be available. Also lmawk inherits any bugs or limitations of the library functions. ๐
Implementors of the AWK language have shown a consistent lack of imagination when naming their programs. ๐
๐ AUTHOR ๐
mawk: Mike Brennan (brennan@whidbey.com). ๐
libmawk extensions: Tibor Palinkas (libmawk@igor2.repo.hu). ๐ซถ
Version 1.2, dated Dec 12 2010 โ that's the LMAWK(1) footer, bestie. Now go forth and awk slay. periodt. ๐ โจ
๐ฆ where this page comes from
- debian bookworm: libmawk1 1.0.2-3
- debian trixie: libmawk1t64 1.0.4-4
- fedora 43: libmawk 1.0.3-8.fc41 ยท GPLv2
- fedora 43: libmawk 1.0.3-8.fc41 ยท GPLv2
- fedora 44: libmawk 1.0.5-1.fc44 ยท GPL-2.0-only
- fedora 44: libmawk 1.0.5-1.fc44 ยท GPL-2.0-only
- ubuntu jammy: libmawk1 1.0.2-3
- ubuntu noble: libmawk1t64 1.0.4-3build1
- ubuntu questing: libmawk1t64 1.0.4-5
- ubuntu resolute: libmawk1t64 1.0.5-1
- ubuntu stonking: libmawk1t64 1.0.5-1
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms