home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / gawk2.0.t.Z / gawk2.0.t / gawk.1 < prev    next >
Text File  |  1988-12-31  |  27KB  |  1,182 lines

  1. .TH GAWK 1 "Free Software Foundation"
  2. .SH NAME
  3. gawk \- pattern scanning and processing language
  4. .SH SYNOPSIS
  5. .B gawk
  6. .ig
  7. [
  8. .B \-d
  9. ] [
  10. .B \-D
  11. ] [
  12. .B \-i
  13. ] [
  14. .B \-v
  15. ]
  16. ..
  17. [
  18. .BI \-F\^ fs
  19. ]
  20. .B \-f
  21. .I program-file
  22. [
  23. .B \-f
  24. .I program-file
  25. \&.\^.\^. ] [
  26. .B \-\^\-
  27. ] file .\^.\^.
  28. .br
  29. .B gawk
  30. .ig
  31. [
  32. .B \-d
  33. ] [
  34. .B \-D
  35. ] [
  36. .B \-i
  37. ] [
  38. .B \-v
  39. ]
  40. ..
  41. [
  42. .BI \-F\^ fs
  43. ] [
  44. .B \-\^\-
  45. ]
  46. .I program-text
  47. file .\^.\^.
  48. .SH DESCRIPTION
  49. .I Gawk
  50. is the GNU Project's implementation of the AWK programming language.
  51. It conforms to the definition and description of the language in
  52. .IR "The AWK Programming Language" ,
  53. by Aho, Kernighan, and Weinberger,
  54. with the additional features defined in the System V Release 4 version
  55. of \s-1UNIX\s+1
  56. .IR awk .
  57. .PP
  58. The command line consists of options to
  59. .I gawk
  60. itself, the AWK program text (if not supplied via the
  61. .B \-f
  62. option), and values to be made
  63. available in the
  64. .B ARGC
  65. and
  66. .B ARGV
  67. pre-defined AWK variables.
  68. .PP
  69. The options that
  70. .I gawk
  71. accepts are:
  72. .TP
  73. .BI \-F fs
  74. Use
  75. .I fs
  76. for the input field separator (the value of the
  77. .B FS
  78. predefined
  79. variable). For compatibility with \s-1UNIX\s+1
  80. .IR awk ,
  81. if
  82. .I fs
  83. is ``t'', then
  84. .B FS
  85. will be set to the tab character.
  86. .TP
  87. .BI \-f " program-file"
  88. Read the AWK program source from the file
  89. .IR program-file ,
  90. instead of from the first command line argument.
  91. .TP
  92. .B \-\^\-
  93. Signal the end of options. This is useful to allow further arguments to the
  94. AWK program itself to start with a ``\-''.
  95. This is mainly for consistency with the argument parsing convention used
  96. by most other System V programs.
  97. .PP
  98. Any other options are flagged as illegal, but are otherwise ignored.
  99. (However, see the
  100. .B "GNU EXTENSIONS"
  101. section, below.)
  102. .PP
  103. An AWK program consists of a sequence of pattern-action statements
  104. and optional function definitions.
  105. .RS
  106. .PP
  107. \fIpattern\fB    { \fIaction statements\fB }\fR
  108. .br
  109. \fBfunction \fIname\fB(\fIparameter list\fB) { \fIstatements\fB }\fR
  110. .RE
  111. .PP
  112. .I Gawk
  113. first reads the program source from the
  114. .IR program-file (s)
  115. if specified, or from the first non-option argument on the command line.
  116. The
  117. .B \-f
  118. option may be used multiple times on the command line.
  119. .I Gawk
  120. will read the program text as if all the
  121. .IR program-file s
  122. had been concatenated together.  This is useful for building libraries
  123. of AWK functions, without having to include them in each new AWK
  124. program that uses them.  To use a library function in a file from a
  125. program typed in on the command line, specify
  126. .B /dev/tty
  127. as one of the
  128. .IR program-file s,
  129. type your program, and end it with a
  130. .B ^D
  131. (control-d).
  132. .PP
  133. .I Gawk
  134. compiles the program into an internal form,
  135. and then proceeds to read
  136. each file named in the
  137. .B ARGV
  138. array.
  139. If there are no files named on the command line,
  140. .I gawk
  141. reads the standard input.
  142. .PP
  143. If a ``file'' named on the command line has the form
  144. .IB var = val
  145. it is treated as a variable assignment. The variable
  146. .I var
  147. will be assigned the value
  148. .IR val .
  149. This is most useful for dynamically assigning values to the variables
  150. AWK uses to control how input is broken into fields and records. It
  151. is also useful for controlling state if multiple passes are needed over
  152. a single data file.
  153. .PP
  154. For each line in the input,
  155. .I gawk
  156. tests to see if it matches any
  157. .I pattern
  158. in the AWK program.
  159. For each pattern that the line matches, the associated
  160. .I action
  161. is executed.
  162. .SH VARIABLES AND FIELDS
  163. AWK variables are dynamic; they come into existence when they are
  164. first used. Their values are either floating-point numbers or strings,
  165. depending upon how they are used. AWK also has single dimension
  166. arrays; multiply dimensioned arrays may be simulated.
  167. There are several pre-defined variables that AWK sets as a program
  168. runs; these will be described as needed and summarized below.
  169. .PP
  170. As each input line is read,
  171. .I gawk
  172. splits the line into
  173. .IR fields ,
  174. using the value of the
  175. .B FS
  176. variable as the field separator.
  177. If
  178. .B FS
  179. is a single character, fields are separated by that character.
  180. Otherwise,
  181. .B FS
  182. is expected to be a full regular expression.
  183. In the special case that
  184. .B FS
  185. is a single blank, fields are separated
  186. by runs of blanks and/or tabs.
  187. .PP
  188. Each field in the input line may be referenced by its position,
  189. .BR $1 ,
  190. .BR $2 ,
  191. and so on.
  192. .B $0
  193. is the whole line. The value of a field may be assigned to as well.
  194. Fields need not be referenced by constants:
  195. .RS
  196. .PP
  197. .ft B
  198. n = 5
  199. .br
  200. print $n
  201. .ft R
  202. .RE
  203. .PP
  204. prints the fifth field in the input line.
  205. The variable
  206. .B NF
  207. is set to the total number of fields in the input line.
  208. .PP
  209. References to non-existent fields (i.e. fields after
  210. .BR $NF ),
  211. produce the null-string. However, assigning to a non-existent field
  212. (e.g., 
  213. .BR "$(NF+2) = 5" )
  214. will increase the value of
  215. .BR NF ,
  216. create any intervening fields with the null string as their value, and
  217. cause the value of
  218. .B $0
  219. to be recomputed, with the fields being separated by the value of
  220. .BR OFS .
  221. .SS Built-in Variables
  222. .PP
  223. AWK's built-in variables are:
  224. .PP
  225. .RS
  226. .TP \l'\fBFILENAME\fR'
  227. .B ARGC
  228. the number of command line arguments (does not include options to
  229. .IR gawk ,
  230. or the program source).
  231. .TP \l'\fBFILENAME\fR'
  232. .B ARGV
  233. array of command line arguments. The array is indexed from
  234. 0 to
  235. .B ARGC
  236. \- 1.
  237. Dynamically changing the contents of
  238. .B ARGV
  239. can control the files used for data.
  240. .TP \l'\fBFILENAME\fR'
  241. .B ENVIRON
  242. An array containing the values of the current environment.
  243. The array is indexed by the environment variables, each element being
  244. the value of that variable (e.g., \fBENVIRON["HOME"]\fP might be
  245. .BR /u/arnold ).
  246. Changing this array does not affect the environment seen by programs which
  247. .I gawk
  248. spawns via redirection or the
  249. .B system
  250. function.
  251. .TP \l'\fBFILENAME\fR'
  252. .B FILENAME
  253. the name of the current input file.
  254. If no files are specified on the command line, the value of
  255. .B FILENAME
  256. is ``\-''.
  257. .TP \l'\fBFILENAME\fR'
  258. .B FNR
  259. the input record number in the current input file.
  260. .TP \l'\fBFILENAME\fR'
  261. .B FS
  262. the input field separator, a blank by default.
  263. .TP \l'\fBFILENAME\fR'
  264. .B NF
  265. the number of fields in the current input record.
  266. .TP \l'\fBFILENAME\fR'
  267. .B NR
  268. the total number of input records seen so far.
  269. .TP \l'\fBFILENAME\fR'
  270. .B OFMT
  271. the output format for numbers,
  272. .B %.6g
  273. by default.
  274. .TP \l'\fBFILENAME\fR'
  275. .B OFS
  276. the output field separator, a blank by default.
  277. .TP \l'\fBFILENAME\fR'
  278. .B ORS
  279. the output record separator, by default a newline.
  280. .TP \l'\fBFILENAME\fR'
  281. .B RS
  282. the input record separator, by default a newline.
  283. .B RS
  284. is exceptional in that only the first character of its string
  285. value is used for separating records. If
  286. .B RS
  287. is set to the null string, then records are separated by
  288. blank lines.
  289. When
  290. .B RS
  291. is set to the null string, then the newline character always acts as
  292. a field separator, in addition to whatever value
  293. .B FS
  294. may have.
  295. .TP \l'\fBFILENAME\fR'
  296. .B RSTART
  297. the index of the first character matched by
  298. .BR match() ;
  299. 0 if no match.
  300. .TP \l'\fBFILENAME\fR'
  301. .B RLENGTH
  302. the length of the string matched by
  303. .BR match() ;
  304. \-1 if no match.
  305. .TP \l'\fBFILENAME\fR'
  306. .B SUBSEP
  307. the character used to separate multiple subscripts in array
  308. elements, by default \fB"\e034"\fR.
  309. .RE
  310. .SS Arrays
  311. .PP
  312. Arrays are subscripted with an expression between square brackets
  313. .RB ( [ " and " ] ).
  314. If the expression is an expression list
  315. .RI ( expr ", " expr " ...)"
  316. then the array subscript is a string consisting of the
  317. concatenation of the (string) value of each expression,
  318. separated by the value of the
  319. .B SUBSEP
  320. variable.
  321. This facility is used to simulate multiply dimensioned
  322. arrays. For example:
  323. .PP
  324. .RS
  325. .ft B
  326. i = "A" ;\^ j = "B" ;\^ k = "C"
  327. .br
  328. x[i,j,k] = "hello, world\en"
  329. .ft R
  330. .RE
  331. .PP
  332. assigns the string \fB"hello, world\en"\fR to the element of the array
  333. .B x
  334. which is indexed by the string \fB"A\e034B\e034C"\fR. All arrays in AWK
  335. are associative, i.e. indexed by string values.
  336. .PP
  337. The special operator
  338. .B in
  339. may be used in an
  340. .B if
  341. or
  342. .B while
  343. statement to see if an array has an index consisting of a particular
  344. value.
  345. .PP
  346. .RS
  347. .ft B
  348. .nf
  349. if (val in array)
  350.     print array[val]
  351. .fi
  352. .ft
  353. .RE
  354. .PP
  355. If the array has multiple subscripts, use
  356. .BR "(i, j) in array" .
  357. .PP
  358. The
  359. .B in
  360. construct may also be used in a
  361. .B for
  362. loop to iterate over all the elements of an array.
  363. .PP
  364. An element may be deleted from an array using the
  365. .B delete
  366. statement.
  367. .SS Variable Typing
  368. .PP
  369. Variables and fields
  370. may be (floating point) numbers, or strings, or both. How the
  371. value of a variable is interpreted depends upon its context. If used in
  372. a numeric expression, it will be treated as a number, if used as a string
  373. it will be treated as a string.
  374. .PP
  375. To force a variable to be treated as a number, add 0 to it; to force it
  376. to be treated as a string, concatenate it with the null string.
  377. .PP
  378. The AWK language defines comparisons as being done numerically if
  379. possible, otherwise one or both operands are converted to strings and
  380. a string comparison is performed.
  381. .PP
  382. Uninitialized variables have the numeric value 0 and the string value ""
  383. (the null, or empty, string).
  384. .SH PATTERNS AND ACTIONS
  385. AWK is a line oriented language. The pattern comes first, and then the
  386. action. Action statements are enclosed in
  387. .B {
  388. and
  389. .BR } .
  390. Either the pattern may be missing, or the action may be missing, but,
  391. of course, not both. If the pattern is missing, the action will be
  392. executed for every single line of input.
  393. A missing action is equivalent to
  394. .RS
  395. .PP
  396. .B "{ print }"
  397. .RE
  398. .PP
  399. which prints the entire line.
  400. .PP
  401. Comments begin with the ``#'' character, and continue until the
  402. end of the line.
  403. Blank lines may be used to separate statements.
  404. Normally, a statement ends with a newline, however, this is not the
  405. case for lines ending in
  406. a ``,'', ``{'', ``?'', ``:'', ``&&'', or ``||''.
  407. Lines ending in
  408. .B do
  409. or
  410. .B else
  411. also have their statements automatically continued on the following line.
  412. In other cases, a line can be continued by ending it with a ``\e'',
  413. in which case the newline will be ignored.
  414. .PP
  415. Multiple statements may
  416. be put on one line by separating them with a ``;''.
  417. This applies to both the statements within the action part of a
  418. pattern-action pair (the usual case),
  419. and to the pattern-action statements themselves.
  420. .SS Patterns
  421. AWK patterns may be one of the following:
  422. .PP
  423. .RS
  424. .nf
  425. .B BEGIN
  426. .B END
  427. .BI / "regular expression" /
  428. .I "relational expression"
  429. .IB pattern " && " pattern
  430. .IB pattern " || " pattern
  431. .IB pattern " ? " pattern " : " pattern
  432. .BI ( pattern )
  433. .BI ! " pattern"
  434. .IB pattern1 ", " pattern2"
  435. .fi
  436. .RE
  437. .PP
  438. .B BEGIN
  439. and
  440. .B END
  441. are two special kinds of patterns which are not tested against
  442. the input.
  443. The action parts of all
  444. .B BEGIN
  445. patterns are merged as if all the statements had
  446. been written in a single
  447. .B BEGIN
  448. block. They are executed before any
  449. of the input is read. Similarly, all the
  450. .B END
  451. blocks are merged,
  452. and executed when all the input is exhausted (or when an
  453. .B exit
  454. statement is executed).
  455. .B BEGIN
  456. and
  457. .B END
  458. patterns cannot be combined with other patterns in pattern expressions.
  459. .B BEGIN
  460. and
  461. .B END
  462. patterns cannot have missing action parts.
  463. .PP
  464. For
  465. .BI / "regular expression" /
  466. patterns, the associated statement is executed for each input line that matches
  467. the regular expression.
  468. Regular expressions are the same as those in
  469. .IR egrep (1),
  470. and are summarized below.
  471. .PP
  472. A
  473. .I "relational expression"
  474. may use any of the operators defined below in the section on actions.
  475. These generally test whether certain fields match certain regular expressions.
  476. .PP
  477. The
  478. .BR && ,
  479. .BR || ,
  480. and
  481. .B !
  482. operators are logical AND, logical OR, and logical NOT, respectively, as in C.
  483. They do short-circuit evaluation, also as in C, and are used for combining
  484. more primitive pattern expressions. As in most languages, parentheses
  485. may be used to change the order of evaluation.
  486. .PP
  487. The
  488. .B ?\^:
  489. operator is like the same operator in C. If the first pattern is true
  490. then the pattern used for testing is the second pattern, otherwise it is
  491. the third. Only one of the second and third patterns is evaluated.
  492. .PP
  493. The 
  494. .IB pattern1 ", " pattern2"
  495. form of an expression is called a range pattern.
  496. It matches all input lines starting with a line that matches
  497. .IR pattern1 ,
  498. and continuing until a line that matches
  499. .IR pattern2 ,
  500. inclusive. It does not combine with any other sort of pattern expression.
  501. .SS Regular Expressions
  502. Regular expressions are the extended kind found in
  503. .IR egrep .
  504. They are composed of characters as follows:
  505. .RS
  506. .TP \l'[^abc...]'
  507. .I c
  508. matches the non-metacharacter
  509. .IR c .
  510. .TP \l'[^abc...]'
  511. .I \ec
  512. matches the literal character
  513. .IR c .
  514. .TP \l'[^abc...]'
  515. .B .
  516. matches any character except newline.
  517. .TP \l'[^abc...]'
  518. .B ^
  519. matches the beginning of a line or a string.
  520. .TP \l'[^abc...]'
  521. .B $
  522. matches the end of a line or a string.
  523. .TP \l'[^abc...]'
  524. .BI [ abc... ]
  525. character class, matches any of the characters
  526. .IR abc... .
  527. .TP \l'[^abc...]'
  528. .BI [^ abc... ]
  529. negated character class, matches any character except
  530. .I abc...
  531. and newline.
  532. .TP \l'[^abc...]'
  533. .IB r1 | r2
  534. alternation: matches either
  535. .I r1
  536. or
  537. .IR r2 .
  538. .TP \l'[^abc...]'
  539. .I r1r2
  540. concatenation: matches
  541. .IR r1 ,
  542. and then
  543. .IR r2 .
  544. .TP \l'[^abc...]'
  545. .IB r +
  546. matches one or more
  547. .IR r 's. 
  548. .TP \l'[^abc...]'
  549. .IB r *
  550. matches zero or more
  551. .IR r 's. 
  552. .TP \l'[^abc...]'
  553. .IB r ?
  554. matches zero or one
  555. .IR r 's. 
  556. .TP \l'[^abc...]'
  557. .BI ( r )
  558. grouping: matches
  559. .IR r .
  560. .RE
  561. .SS Actions
  562. Action statements are enclosed in braces,
  563. .B {
  564. and
  565. .BR } .
  566. Action statements consist of the usual assignment, conditional, and looping
  567. statements found in most languages. The operators, control statements,
  568. and input/output statements
  569. available are patterned after those in C.
  570. .PP
  571. The operators in AWK, in order of increasing precedence, are
  572. .PP
  573. .RS
  574. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  575. .B "= += \-= *= /= %= ^="
  576. Assignment. Both absolute assignment
  577. .BI ( var " = " value )
  578. and operator-assignment (the other forms) are supported.
  579. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  580. .B ?:
  581. The C conditional expression. This has the form
  582. .IB expr1 " ? " expr2 " : " expr3\c
  583. \&. If
  584. .I expr1
  585. is true, the value of the expression is
  586. .IR expr2 ,
  587. otherwise it is
  588. .IR expr3 .
  589. Only one of
  590. .I expr2
  591. and
  592. .I expr3
  593. is evaluated.
  594. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  595. .B ||
  596. logical OR.
  597. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  598. .B &&
  599. logical AND.
  600. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  601. .B "~ !~"
  602. regular expression match, negated match.
  603. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  604. .B "< <= > >= != =="
  605. the regular relational operators.
  606. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  607. .I blank
  608. string concatenation.
  609. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  610. .B "+ \-"
  611. addition and subtraction.
  612. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  613. .B "* / %"
  614. multiplication, division, and modulus.
  615. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  616. .B "+ \- !"
  617. unary plus, unary minus, and logical negation.
  618. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  619. .B ^
  620. exponentiation (\fB**\fR may also be used, and \fB**=\fR for
  621. the assignment operator).
  622. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  623. .B "++ \-\^\-"
  624. increment and decrement, both prefix and postfix.
  625. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  626. .B $
  627. field reference.
  628. .RE
  629. .PP
  630. The control statements are
  631. as follows:
  632. .PP
  633. .RS
  634. .nf
  635. \fBif (\fIcondition\fB) \fIstatement\fR [ \fBelse\fI statement \fR]
  636. \fBwhile (\fIcondition\fB) \fIstatement \fR
  637. \fBdo \fIstatement \fBwhile (\fIcondition\fB)\fR
  638. \fBfor (\fIexpr1\fB; \fIexpr2\fB; \fIexpr3\fB) \fIstatement\fR
  639. \fBfor (\fIvar \fBin\fI array\fB) \fIstatement\fR
  640. \fBbreak\fR
  641. \fBcontinue\fR
  642. \fBdelete \fIarray\^\fB[\^\fIindex\^\fB]\fR
  643. \fBexit\fR [ \fIexpression\fR ]
  644. \fB{ \fIstatements \fB}
  645. .fi
  646. .RE
  647. .PP
  648. The input/output statements are as follows:
  649. .PP
  650. .RS
  651. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  652. .BI close( filename )
  653. close file (or pipe, see below).
  654. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  655. .B getline
  656. set
  657. .B $0
  658. from next input record; set
  659. .BR NF ,
  660. .BR NR ,
  661. .BR FNR .
  662. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  663. .BI "getline <" file
  664. set
  665. .B $0
  666. from next record of
  667. .IR file ;
  668. set
  669. .BR NF .
  670. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  671. .BI getline " var"
  672. set
  673. .I var
  674. from next input record; set
  675. .BR NF ,
  676. .BR FNR .
  677. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  678. .BI getline " var" " <" file
  679. set
  680. .I var
  681. from next record of
  682. .IR file .
  683. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  684. .B next
  685. Stop processing the current input record. The next input record
  686. is read and processing starts over with the first pattern in the
  687. AWK program. If the end of the input data is reached, the
  688. .B END
  689. block(s), if any, are executed.
  690. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  691. .B print
  692. prints the current record.
  693. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  694. .BI print " expr-list"
  695. prints expressions.
  696. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  697. .BI print " expr-list" " >" file
  698. prints expressions on
  699. .IR file .
  700. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  701. .BI printf " fmt, expr-list"
  702. format and print.
  703. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  704. .BI printf " fmt, expr-list" " >" file
  705. format and print on
  706. .IR file .
  707. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  708. .BI system( cmd-line )
  709. execute the command
  710. .IR cmd-line ,
  711. and return the exit status.
  712. (This may not be available on 
  713. systems besides \s-1UNIX\s+1 and \s-1GNU\s+1.)
  714. .RE
  715. .PP
  716. Other input/output redirections are also allowed. For
  717. .B print
  718. and
  719. .BR printf ,
  720. .BI >> file
  721. appends output to the
  722. .IR file ,
  723. while
  724. .BI | " command"
  725. writes on a pipe.
  726. In a similar fashion,
  727. .IB command " | getline"
  728. pipes into
  729. .BR getline .
  730. .BR Getline
  731. will return 0 on end of file, and \-1 on an error.
  732. .PP
  733. The AWK versions of the
  734. .B printf
  735. and
  736. .B sprintf
  737. (see below)
  738. functions accept the following conversion specification formats:
  739. .RS
  740. .TP
  741. .B %c
  742. An ASCII character.
  743. .TP
  744. .B %d
  745. A decimal number (the integer part).
  746. .TP
  747. .B %e
  748. A floating point number of the form
  749. .BR [\-]d.ddddddE[+\^\-]dd .
  750. .TP
  751. .B %f
  752. A floating point number of the form
  753. .BR [\-]ddd.dddddd .
  754. .TP
  755. .B %g
  756. Use
  757. .B e
  758. or
  759. .B f
  760. conversion, whichever is shorter, with nonsignificant zeros suppressed.
  761. .TP
  762. .B %o
  763. An unsigned octal number (again, an integer).
  764. .TP
  765. .B %s
  766. A character string.
  767. .TP
  768. .B %x
  769. An unsigned hexadecimal number (an integer).
  770. .TP
  771. .B %%
  772. A single
  773. .B %
  774. character; no argument is converted.
  775. .RE
  776. .PP
  777. There are optional, additional parameters that may lie between the
  778. .B %
  779. and the control letter:
  780. .RS
  781. .TP
  782. .B \-
  783. The expression should be left-justified within its field.
  784. .TP
  785. .I width
  786. The field should be padded to this width. If the number has a leading
  787. zero, then the field will be padded with zeros.
  788. Otherwise it is padded with blanks.
  789. .TP
  790. .BI . prec
  791. A number indicating the maximum width of strings or digits to the right
  792. of the decimal point.
  793. .RE
  794. .PP
  795. The dynamic
  796. .I width
  797. and
  798. .I prec
  799. capabilities of the C library
  800. .B printf
  801. routines are not supported.
  802. However, they may be simulated by using
  803. the AWK concatenation operation to build up
  804. a format specification dynamically.
  805. .PP
  806. AWK has the following pre-defined arithmetic functions:
  807. .PP
  808. .RS
  809. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  810. .BI atan2( y , " x" )
  811. returns the arctangent of
  812. .I y/x
  813. in radians.
  814. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  815. .BI cos( expr )
  816. returns the cosine in radians.
  817. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  818. .BI exp( expr )
  819. the exponential function.
  820. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  821. .BI int( expr )
  822. truncates to integer.
  823. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  824. .BI log( expr )
  825. the natural logarithm function.
  826. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  827. .B rand()
  828. returns a random number between 0 and 1.
  829. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  830. .BI sin( expr )
  831. returns the sine in radians.
  832. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  833. .BI sqrt( expr )
  834. the square root function.
  835. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  836. .BI srand( expr )
  837. use
  838. .I expr
  839. as a new seed for the random number generator. If no
  840. .I expr
  841. is provided, the time of day will be used.
  842. The return value is the previous seed for the random
  843. number generator.
  844. .RE
  845. .PP
  846. AWK has the following pre-defined string functions:
  847. .PP
  848. .RS
  849. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  850. \fBgsub(\fIr\fB, \fIs\fB, \fIt\fB)\fR
  851. for each substring matching the regular expression
  852. .I r
  853. in the string
  854. .IR t ,
  855. substitute the string
  856. .IR s ,
  857. and return the number of substitutions.
  858. If
  859. .I t
  860. is not supplied, use
  861. .BR $0 .
  862. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  863. .BI index( s , " t" )
  864. returns the index of the string
  865. .I t
  866. in the string
  867. .IR s ,
  868. or 0 if
  869. .I t
  870. is not present.
  871. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  872. .BI length( s )
  873. returns the length of the string
  874. .IR s .
  875. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  876. .BI match( s , " r" )
  877. returns the position in
  878. .I s
  879. where the regular expression
  880. .I r
  881. occurs, or 0 if
  882. .I r
  883. is not present, and sets the values of
  884. .B RSTART
  885. and
  886. .BR RLENGTH .
  887. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  888. \fBsplit(\fIs\fB, \fIa\fB, \fIr\fB)\fR
  889. splits the string
  890. .I s
  891. into the array
  892. .I a
  893. on the regular expression
  894. .IR r ,
  895. and returns the number of fields. If
  896. .I r
  897. is omitted,
  898. .B FS
  899. is used instead.
  900. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  901. .BI sprintf( fmt , " expr-list" )
  902. prints
  903. .I expr-list
  904. according to
  905. .IR fmt ,
  906. and returns the resulting string.
  907. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  908. \fBsub(\fIr\fB, \fIs\fB, \fIt\fB)\fR
  909. this is just like
  910. .BR gsub ,
  911. but only the first matching substring is replaced.
  912. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  913. \fBsubstr(\fIs\fB, \fIi\fB, \fIn\fB)\fR
  914. returns the
  915. .IR n -character
  916. substring of
  917. .I s
  918. starting at
  919. .IR i .
  920. If
  921. .I n
  922. is omitted, the rest of
  923. .I s
  924. is used.
  925. .RE
  926. .PP
  927. String constants in AWK are sequences of characters enclosed
  928. between double quotes (\fB"\fR). Within strings, certain
  929. .I "escape sequences"
  930. are recognized, as in C. These are:
  931. .PP
  932. .RS
  933. .TP \l'\fB\e\fIddd\fR'
  934. .B \eb
  935. backspace.
  936. .TP \l'\fB\e\fIddd\fR'
  937. .B \ef
  938. form-feed.
  939. .TP \l'\fB\e\fIddd\fR'
  940. .B \en
  941. new line.
  942. .TP \l'\fB\e\fIddd\fR'
  943. .B \er
  944. carriage return.
  945. .TP \l'\fB\e\fIddd\fR'
  946. .B \et
  947. horizontal tab.
  948. .TP \l'\fB\e\fIddd\fR'
  949. .B \ev
  950. vertical tab.
  951. .TP \l'\fB\e\fIddd\fR'
  952. .BI \e ddd
  953. The character represented by the 1-, 2-, or 3-digit sequence of octal
  954. digits. E.g. "\e033" is the ASCII ESC (escape) character.
  955. .RE
  956. .SH FUNCTIONS
  957. Functions in AWK are defined as follows:
  958. .PP
  959. .RS
  960. \fBfunction \fIname\fB(\fIparameter list\fB) { \fIstatements \fB}\fR
  961. .RE
  962. .PP
  963. Functions are executed when called from within the action parts of regular
  964. pattern-action statements. Actual parameters supplied in the function
  965. call are used to instantiate the formal parameters declared in the function.
  966. Arrays are passed by reference, other variables are passed by value.
  967. .PP
  968. Since functions were not originally part of the AWK language, the provision
  969. for local variables is rather clumsy: they are declared as extra parameters
  970. in the parameter list. The convention is to separate local variables from
  971. real parameters by extra spaces in the parameter list. For example:
  972. .PP
  973. .RS
  974. .ft B
  975. .nf
  976. function  f(p, q,     a, b) {    # a & b are local
  977.             ..... }
  978.  
  979. /abc/    { ... ; f(1, 2) ; ... }
  980. .fi
  981. .ft R
  982. .RE
  983. .PP
  984. The left parenthesis in a function call is required
  985. to immediately follow the function name,
  986. without any intervening white space.
  987. This is to avoid a syntactic ambiguity with the concatenation operator.
  988. This restriction does not apply to the built-in functions listed above.
  989. .PP
  990. Functions may call each other and may be recursive.
  991. Function parameters used as local variables are initialized
  992. to the null string and the number zero upon function invocation.
  993. .PP
  994. The word
  995. .B func
  996. may be used in place of
  997. .BR function .
  998. .SH EXAMPLES
  999. .nf
  1000. Print and sort the login names of all users:
  1001.  
  1002. .ft B
  1003.     BEGIN    { FS = ":" }
  1004.         { print $1 | "sort" }
  1005.  
  1006. .ft R
  1007. Count lines in a file:
  1008.  
  1009. .ft B
  1010.         { nlines++ }
  1011.     END    { print nlines }
  1012.  
  1013. .ft R
  1014. Precede each line by its number in the file:
  1015.  
  1016. .ft B
  1017.     { print FNR, $0 }
  1018.  
  1019. .ft R
  1020. Concatenate and line number (a variation on a theme):
  1021.  
  1022. .ft B
  1023.     { print NR, $0 }
  1024. .ft R
  1025. .SH SEE ALSO
  1026. .IR "The AWK Programming Language" ,
  1027. Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger,
  1028. Addison-Wesley, 1988. ISBN 0-201-07981-X.
  1029. .SH SYSTEM V RELEASE 4 COMPATIBILITY
  1030. A primary goal for
  1031. .I gawk
  1032. is compatibility with the latest version of \s-1UNIX\s+1
  1033. .IR awk .
  1034. To this end,
  1035. .I gawk
  1036. incorporates the following user visible
  1037. features which are not described in the AWK book,
  1038. but are part of
  1039. .I awk
  1040. in System V Release 4.
  1041. .PP
  1042. When processing arguments,
  1043. .I gawk
  1044. uses the special option ``\fB\-\^\-\fP'' to signal the end of
  1045. arguments, and warns about, but otherwise ignores, undefined options.
  1046. .PP
  1047. The AWK book does not define the return value of
  1048. .BR srand() .
  1049. The System V Release 4 version of \s-1UNIX\s+1
  1050. .I awk
  1051. has it return the seed it was using, to allow keeping track
  1052. of random number sequences. Therefore
  1053. .B srand()
  1054. in
  1055. .I gawk
  1056. also returns its current seed.
  1057. .PP
  1058. The use of multiple
  1059. .B \-f
  1060. options is a new feature, as is the
  1061. .B ENVIRON
  1062. array.
  1063. .SH GNU EXTENSIONS
  1064. .I Gawk
  1065. has some extensions to System V
  1066. .IR awk .
  1067. They are described in this section.
  1068. All features described in this section may change at some time in
  1069. the future, or may go away entirely.  They can be disabled either by
  1070. compiling
  1071. .I gawk
  1072. with
  1073. .BR \-DSTRICT ,
  1074. or by invoking
  1075. .I gawk
  1076. with the name
  1077. .IR awk .
  1078. You should not write programs that depend upon them.
  1079. .PP
  1080. The environment variable
  1081. .B AWKPATH
  1082. specifies a search path to use when finding source files named with
  1083. the 
  1084. .B \-f
  1085. option.  If this variable does not exist, the default path is
  1086. \fB".:/usr/lib/awk:/usr/local/lib/awk"\fR.
  1087. If a file name given to the
  1088. .B \-f
  1089. option contains a ``/'' character, no path search is performed.
  1090. .PP
  1091. Two new relational operators are defined,
  1092. .BR ~~ ,
  1093. and
  1094. .BR !~~ .
  1095. These perform case independent regular expression match and no-match
  1096. operations, respectively.
  1097. .PP
  1098. The AWK book does not define the return value of the
  1099. .B close
  1100. function.
  1101. .IR Gawk\^ 's
  1102. .B close
  1103. returns the value from
  1104. .IR fclose (3),
  1105. or
  1106. .IR pclose (3),
  1107. when closing a file or pipe, respectively.
  1108. .PP
  1109. .I Gawk
  1110. accepts the following additional arguments:
  1111. .ig
  1112. .TP
  1113. .B \-D
  1114. Turn on general debugging and turn on
  1115. .IR yacc (1)
  1116. or
  1117. .IR bison (1)
  1118. debugging output during program parsing.
  1119. This option should only be of interest to the
  1120. .I gawk
  1121. maintainers, and may not even be compiled into
  1122. .IR gawk .
  1123. .TP
  1124. .B \-d
  1125. Turn on general debugging and print the
  1126. .I gawk
  1127. internal tree as the program is executed.
  1128. This option should only be of interest to the
  1129. .I gawk
  1130. maintainers, and may not even be compiled into
  1131. .IR gawk .
  1132. ..
  1133. .TP
  1134. .B \-i
  1135. Ignore case when doing regular expression operations.
  1136. This causes
  1137. .B ~
  1138. and 
  1139. .B !~
  1140. to behave like the new operators
  1141. .B ~~
  1142. and
  1143. .BR !~~ ,
  1144. described above.
  1145. .TP
  1146. .B \-v
  1147. Print version information for this particular copy of
  1148. .I gawk
  1149. on the error output.
  1150. This is useful mainly for knowing if the current copy of
  1151. .I gawk
  1152. on your system
  1153. is up to date with respect to whatever the Free Software Foundation
  1154. is distributing.
  1155. .SH BUGS
  1156. The
  1157. .B \-F
  1158. option is not necessary given the command line variable assignment feature;
  1159. it remains only for backwards compatibility.
  1160. .SH AUTHORS
  1161. The original version of \s-1UNIX\s+1
  1162. .I awk
  1163. was designed and implemented by Alfred Aho,
  1164. Peter Weinberger, and Brian Kernighan of AT&T Bell Labs. Brian Kernighan
  1165. continues to maintain and enhance it.
  1166. .PP
  1167. Paul Rubin and Jay Fenlason, with John Woods,
  1168. all of the Free Software Foundation, wrote
  1169. .IR gawk ,
  1170. to be compatible with the original version of
  1171. .I awk
  1172. distributed in Seventh Edition \s-1UNIX\s+1.
  1173. David Trueman of Dalhousie University, with contributions
  1174. from Arnold Robbins at Emory University, made
  1175. .I gawk
  1176. compatible with the new version of \s-1UNIX\s+1
  1177. .IR awk .
  1178. .SH ACKNOWLEDGEMENTS
  1179. Brian Kernighan of Bell Labs
  1180. provided valuable assistance during testing and debugging.
  1181. We thank him.
  1182.