1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966
|
'\" et
.TH AWK "1P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.SH NAME
awk
\(em pattern scanning and processing language
.SH SYNOPSIS
.LP
.nf
awk \fB[\fR\(miF \fIsepstring\fB] [\fR\(miv \fIassignment\fB]\fR... \fIprogram\fB [\fIargument\fR...\fB]\fR
.P
awk \fB[\fR\(miF \fIsepstring\fB] \fR\(mif \fIprogfile \fB[\fR\(mif \fIprogfile\fB]\fR... \fB[\fR\(miv \fIassignment\fB]\fR...
\fB[\fIargument\fR...\fB]\fR
.fi
.SH DESCRIPTION
The
.IR awk
utility shall execute programs written in the
.IR awk
programming language, which is specialized for textual data
manipulation. An
.IR awk
program is a sequence of patterns and corresponding actions. When
input is read that matches a pattern, the action associated with that
pattern is carried out.
.P
Input shall be interpreted as a sequence of records. By default, a
record is a line, less its terminating
<newline>,
but this can be changed by using the
.BR RS
built-in variable. Each record of input shall be matched in turn
against each pattern in the program. For each pattern matched, the
associated action shall be executed.
.P
The
.IR awk
utility shall interpret each input record as a sequence of fields
where, by default, a field is a string of non-\c
<blank>
non-\c
<newline>
characters. This default
<blank>
and
<newline>
field delimiter can be changed by using the
.BR FS
built-in variable or the
.BR \(miF
.IR sepstring
option. The
.IR awk
utility shall denote the first field in a record $1, the second $2, and
so on. The symbol $0 shall refer to the entire record; setting any
other field causes the re-evaluation of $0. Assigning to $0 shall reset
the values of all other fields and the
.BR NF
built-in variable.
.SH OPTIONS
The
.IR awk
utility shall conform to the Base Definitions volume of POSIX.1\(hy2008,
.IR "Section 12.2" ", " "Utility Syntax Guidelines".
.P
The following options shall be supported:
.IP "\fB\(miF\ \fIsepstring\fR" 10
Define the input field separator. This option shall be equivalent to:
.RS 10
.sp
.RS 4
.nf
\fB
-v FS=\fIsepstring
.fi \fR
.P
.RE
.P
except that if
.BR \(miF
.IR sepstring
and
.BR \(miv
.IR \fRFS=\fPsepstring\fR
are both used, it is unspecified whether the
.BR FS
assignment resulting from
.BR \(miF
.IR sepstring
is processed in command line order or is processed after the last
.BR \(miv
.IR \fRFS=\fPsepstring\fR .
See the description of the
.BR FS
built-in variable, and how it is used, in the EXTENDED DESCRIPTION
section.
.RE
.IP "\fB\(mif\ \fIprogfile\fR" 10
Specify the pathname of the file
.IR progfile
containing an
.IR awk
program. A pathname of
.BR '\(mi'
shall denote the standard input. If multiple instances of this option
are specified, the concatenation of the files specified as
.IR progfile
in the order specified shall be the
.IR awk
program. The
.IR awk
program can alternatively be specified in the command line as a single
argument.
.IP "\fB\(miv\ \fIassignment\fR" 10
.br
The application shall ensure that the
.IR assignment
argument is in the same form as an
.IR assignment
operand. The specified variable assignment shall occur prior to
executing the
.IR awk
program, including the actions associated with
.BR BEGIN
patterns (if any). Multiple occurrences of this option can be
specified.
.SH OPERANDS
The following operands shall be supported:
.IP "\fIprogram\fR" 10
If no
.BR \(mif
option is specified, the first operand to
.IR awk
shall be the text of the
.IR awk
program. The application shall supply the
.IR program
operand as a single argument to
.IR awk .
If the text does not end in a
<newline>,
.IR awk
shall interpret the text as if it did.
.IP "\fIargument\fR" 10
Either of the following two types of
.IR argument
can be intermixed:
.RS 10
.IP "\fIfile\fR" 10
A pathname of a file that contains the input to be read, which is
matched against the set of patterns in the program. If no
.IR file
operands are specified, or if a
.IR file
operand is
.BR '\(mi' ,
the standard input shall be used.
.IP "\fIassignment\fR" 10
An operand that begins with an
<underscore>
or alphabetic character from the portable character set (see the table
in the Base Definitions volume of POSIX.1\(hy2008,
.IR "Section 6.1" ", " "Portable Character Set"),
followed by a sequence of underscores, digits, and alphabetics from the
portable character set, followed by the
.BR '='
character, shall specify a variable assignment rather than a pathname.
The characters before the
.BR '='
represent the name of an
.IR awk
variable; if that name is an
.IR awk
reserved word (see
.IR "Grammar")
the behavior is undefined. The characters following the
<equals-sign>
shall be interpreted as if they appeared in the
.IR awk
program preceded and followed by a double-quote (\c
.BR '\&"' )
character, as a
.BR STRING
token (see
.IR "Grammar"),
except that if the last character is an unescaped
<backslash>,
it shall be interpreted as a literal
<backslash>
rather than as the first character of the sequence
.BR \(dq\e"\(dq .
The variable shall be assigned the value of that
.BR STRING
token and, if appropriate, shall be considered a
.IR "numeric string"
(see
.IR "Expressions in awk"),
the variable shall also be assigned its numeric value. Each such
variable assignment shall occur just prior to the processing of the
following
.IR file ,
if any. Thus, an assignment before the first
.IR file
argument shall be executed after the
.BR BEGIN
actions (if any), while an assignment after the last
.IR file
argument shall occur before the
.BR END
actions (if any). If there are no
.IR file
arguments, assignments shall be executed before processing the standard
input.
.RE
.SH STDIN
The standard input shall be used only if no
.IR file
operands are specified, or if a
.IR file
operand is
.BR '\(mi' ,
or if a
.IR progfile
option-argument is
.BR '\(mi' ;
see the INPUT FILES section. If the
.IR awk
program contains no actions and no patterns, but is otherwise a valid
.IR awk
program, standard input and any
.IR file
operands shall not be read and
.IR awk
shall exit with a return status of zero.
.SH "INPUT FILES"
Input files to the
.IR awk
program from any of the following sources shall be text files:
.IP " *" 4
Any
.IR file
operands or their equivalents, achieved by modifying the
.IR awk
variables
.BR ARGV
and
.BR ARGC
.IP " *" 4
Standard input in the absence of any
.IR file
operands
.IP " *" 4
Arguments to the
.BR getline
function
.P
Whether the variable
.BR RS
is set to a value other than a
<newline>
or not, for these files, implementations shall support records
terminated with the specified separator up to
{LINE_MAX}
bytes and may support longer records.
.P
If
.BR \(mif
.IR progfile
is specified, the application shall ensure that the files named by each
of the
.IR progfile
option-arguments are text files and their concatenation, in the same
order as they appear in the arguments, is an
.IR awk
program.
.SH "ENVIRONMENT VARIABLES"
The following environment variables shall affect the execution of
.IR awk :
.IP "\fILANG\fP" 10
Provide a default value for the internationalization variables that are
unset or null. (See the Base Definitions volume of POSIX.1\(hy2008,
.IR "Section 8.2" ", " "Internationalization Variables"
for the precedence of internationalization variables used to determine
the values of locale categories.)
.IP "\fILC_ALL\fP" 10
If set to a non-empty string value, override the values of all the
other internationalization variables.
.IP "\fILC_COLLATE\fP" 10
.br
Determine the locale for the behavior of ranges, equivalence classes,
and multi-character collating elements within regular expressions and
in comparisons of string values.
.IP "\fILC_CTYPE\fP" 10
Determine the locale for the interpretation of sequences of bytes of
text data as characters (for example, single-byte as opposed to
multi-byte characters in arguments and input files), the behavior of
character classes within regular expressions, the identification of
characters as letters, and the mapping of uppercase and lowercase
characters for the
.BR toupper
and
.BR tolower
functions.
.IP "\fILC_MESSAGES\fP" 10
.br
Determine the locale that should be used to affect the format and
contents of diagnostic messages written to standard error.
.IP "\fILC_NUMERIC\fP" 10
.br
Determine the radix character used when interpreting numeric input,
performing conversions between numeric and string values, and
formatting numeric output. Regardless of locale, the
<period>
character (the decimal-point character of the POSIX locale) is the
decimal-point character recognized in processing
.IR awk
programs (including assignments in command line arguments).
.IP "\fINLSPATH\fP" 10
Determine the location of message catalogs for the processing of
.IR LC_MESSAGES .
.IP "\fIPATH\fP" 10
Determine the search path when looking for commands executed by
\fIsystem\fR(\fIexpr\fR), or input and output pipes; see the Base Definitions volume of POSIX.1\(hy2008,
.IR "Chapter 8" ", " "Environment Variables".
.P
In addition, all environment variables shall be visible via the
.IR awk
variable
.BR ENVIRON .
.SH "ASYNCHRONOUS EVENTS"
Default.
.SH STDOUT
The nature of the output files depends on the
.IR awk
program.
.SH STDERR
The standard error shall be used only for diagnostic messages.
.SH "OUTPUT FILES"
The nature of the output files depends on the
.IR awk
program.
.br
.SH "EXTENDED DESCRIPTION"
.SS "Overall Program Structure"
.P
An
.IR awk
program is composed of pairs of the form:
.sp
.RS 4
.nf
\fB
\fIpattern\fR { \fIaction\fR }
.fi \fR
.P
.RE
.P
Either the pattern or the action (including the enclosing brace
characters) can be omitted.
.P
A missing pattern shall match any record of input, and a missing action
shall be equivalent to:
.sp
.RS 4
.nf
\fB
{ print }
.fi \fR
.P
.RE
.P
Execution of the
.IR awk
program shall start by first executing the actions associated with all
.BR BEGIN
patterns in the order they occur in the program. Then each
.IR file
operand (or standard input if no files were specified) shall be
processed in turn by reading data from the file until a record
separator is seen (\c
<newline>
by default). Before the first reference to a field in the record is
evaluated, the record shall be split into fields, according to the
rules in
.IR "Regular Expressions",
using the value of
.BR FS
that was current at the time the record was read. Each pattern in the
program then shall be evaluated in the order of occurrence, and the
action associated with each pattern that matches the current record
executed. The action for a matching pattern shall be executed before
evaluating subsequent patterns. Finally, the actions associated with
all
.BR END
patterns shall be executed in the order they occur in the program.
.SS "Expressions in awk"
.P
Expressions describe computations used in
.IR patterns
and
.IR actions .
In the following table, valid expression operations are given in groups
from highest precedence first to lowest precedence last, with
equal-precedence operators grouped between horizontal lines. In
expression evaluation, where the grammar is formally ambiguous, higher
precedence operators shall be evaluated before lower precedence
operators. In this table
.IR expr ,
.IR expr1 ,
.IR expr2 ,
and
.IR expr3
represent any expression, while lvalue represents any entity that can
be assigned to (that is, on the left side of an assignment operator).
The precise syntax of expressions is given in
.IR "Grammar".
.sp
.ce 1
\fBTable 4-1: Expressions in Decreasing Precedence in \fIawk\fP\fR
.TS
box tab(@) center;
cB | cB | cB | cB
l1f5 | l1 | l1 | l.
Syntax@Name@Type of Result@Associativity
_
( \fIexpr\fP )@Grouping@Type of \fIexpr\fP@N/A
_
$\fIexpr\fP@Field reference@String@N/A
_
lvalue ++@Post-increment@Numeric@N/A
lvalue \(mi\|\(mi@Post-decrement@Numeric@N/A
_
++ lvalue@Pre-increment@Numeric@N/A
\(mi\|\(mi lvalue@Pre-decrement@Numeric@N/A
_
\fIexpr\fP ^ \fIexpr\fP@Exponentiation@Numeric@Right
_
! \fIexpr\fP@Logical not@Numeric@N/A
+ \fIexpr\fP@Unary plus@Numeric@N/A
\(mi \fIexpr\fP@Unary minus@Numeric@N/A
_
\fIexpr\fP * \fIexpr\fP@Multiplication@Numeric@Left
\fIexpr\fP / \fIexpr\fP@Division@Numeric@Left
\fIexpr\fP % \fIexpr\fP@Modulus@Numeric@Left
_
\fIexpr\fP + \fIexpr\fP@Addition@Numeric@Left
\fIexpr\fP \(mi \fIexpr\fP@Subtraction@Numeric@Left
_
\fIexpr\fP \fIexpr\fP@String concatenation@String@Left
_
\fIexpr\fP < \fIexpr\fP@Less than@Numeric@None
\fIexpr\fP <= \fIexpr\fP@Less than or equal to@Numeric@None
\fIexpr\fP != \fIexpr\fP@Not equal to@Numeric@None
\fIexpr\fP == \fIexpr\fP@Equal to@Numeric@None
\fIexpr\fP > \fIexpr\fP@Greater than@Numeric@None
\fIexpr\fP >= \fIexpr\fP@Greater than or equal to@Numeric@None
_
\fIexpr\fP ~ \fIexpr\fP@ERE match@Numeric@None
\fIexpr\fP !~ \fIexpr\fP@ERE non-match@Numeric@None
_
\fIexpr\fP in array@Array membership@Numeric@Left
( \fIindex\fP ) in \fIarray\fP@Multi-dimension array@Numeric@Left
@membership
_
\fIexpr\fP && \fIexpr\fP@Logical AND@Numeric@Left
_
\fIexpr\fP || \fIexpr\fP@Logical OR@Numeric@Left
_
\fIexpr1\fP ? \fIexpr2\fP : \fIexpr3\fP@Conditional expression@Type of selected@Right
@@\fIexpr2\fP or \fIexpr3\fP
_
lvalue ^= \fIexpr\fP@Exponentiation assignment@Numeric@Right
lvalue %= \fIexpr\fP@Modulus assignment@Numeric@Right
lvalue *= \fIexpr\fP@Multiplication assignment@Numeric@Right
lvalue /= \fIexpr\fP@Division assignment@Numeric@Right
lvalue += \fIexpr\fP@Addition assignment@Numeric@Right
lvalue \(mi= \fIexpr\fP@Subtraction assignment@Numeric@Right
lvalue = \fIexpr\fP@Assignment@Type of \fIexpr\fP@Right
.TE
.P
Each expression shall have either a string value, a numeric value, or
both. Except as stated for specific contexts, the value of an expression
shall be implicitly converted to the type needed for the context in which
it is used. A string value shall be converted to a numeric value either by
the equivalent of the following calls to functions defined by the ISO\ C standard:
.sp
.RS 4
.nf
\fB
setlocale(LC_NUMERIC, "");
\fInumeric_value\fR = atof(\fIstring_value\fR);
.fi \fR
.P
.RE
.P
or by converting the initial portion of the string to type
.BR double
representation as follows:
.sp
.RS
The input string is decomposed into two parts: an initial, possibly empty,
sequence of white-space characters (as specified by
\fIisspace\fR())
and a subject sequence interpreted as a floating-point constant.
.P
The expected form of the subject sequence is an optional
.BR '+'
or
.BR '\(mi'
sign, then a non-empty sequence of digits optionally containing a
<period>,
then an optional exponent part. An exponent part consists of
.BR 'e'
or
.BR 'E' ,
followed by an optional sign, followed by one or more decimal digits.
.P
The sequence starting with the first digit or the
<period>
(whichever occurs first) is interpreted as a floating constant of the
C language, and if neither an exponent part nor a
<period>
appears, a
<period>
is assumed to follow the last digit in the string. If the subject
sequence begins with a minus-sign, the value resulting from the conversion
is negated.
.RE
.P
A numeric value that is exactly equal to the value of an integer (see
.IR "Section 1.1.2" ", " "Concepts Derived from the ISO C Standard")
shall be converted to a string by the equivalent of a call to the
.BR sprintf
function (see
.IR "String Functions")
with the string
.BR \(dq%d\(dq
as the
.IR fmt
argument and the numeric value being converted as the first and only
.IR expr
argument. Any other numeric value shall be converted to a string by the
equivalent of a call to the
.BR sprintf
function with the value of the variable
.BR CONVFMT
as the
.IR fmt
argument and the numeric value being converted as the first and only
.IR expr
argument. The result of the conversion is unspecified if the value of
.BR CONVFMT
is not a floating-point format specification. This volume of POSIX.1\(hy2008 specifies no
explicit conversions between numbers and strings. An application can
force an expression to be treated as a number by adding zero to it, or
can force it to be treated as a string by concatenating the null string
(\c
.BR \(dq\^\(dq )
to it.
.P
A string value shall be considered a
.IR "numeric string"
if it comes from one of the following:
.IP " 1." 4
Field variables
.IP " 2." 4
Input from the
\fIgetline\fR()
function
.IP " 3." 4
.BR FILENAME
.IP " 4." 4
.BR ARGV
array elements
.IP " 5." 4
.BR ENVIRON
array elements
.IP " 6." 4
Array elements created by the
\fIsplit\fR()
function
.IP " 7." 4
A command line variable assignment
.IP " 8." 4
Variable assignment from another numeric string variable
.P
and an implementation-dependent condition corresponding to either
case (a) or (b) below is met.
.IP " a." 4
After the equivalent of the following calls to functions defined by
the ISO\ C standard,
.IR string_value_end
would differ from
.IR string_value ,
and any characters before the terminating null character in
.IR string_value_end
would be
<blank>
characters:
.RS 4
.sp
.RS 4
.nf
\fB
char *string_value_end;
setlocale(LC_NUMERIC, "");
numeric_value = strtod (string_value, &string_value_end);
.fi \fR
.P
.RE
.RE
.IP " b." 4
After all the following conversions have been applied, the resulting
string would lexically be recognized as a
.BR NUMBER
token as described by the lexical conventions in
.IR "Grammar":
.RS 4
.IP -- 4
All leading and trailing
<blank>
characters are discarded.
.IP -- 4
If the first non-\c
<blank>
is
.BR '\(pl'
or
.BR '\(mi' ,
it is discarded.
.IP -- 4
Each occurrence of the decimal point character from the current locale
is changed to a
<period>.
.RE
In case (a) the numeric value of the
.IR "numeric string"
shall be the value that would be returned by the
\fIstrtod\fR()
call. In case (b) if the first non-\c
<blank>
is
.BR '\(mi' ,
the numeric value of the
.IR "numeric string"
shall be the negation of the numeric value of the recognized
.BR NUMBER
token; otherwise, the numeric value of the
.IR "numeric string"
shall be the numeric value of the recognized
.BR NUMBER
token. Whether or not a string is a
.IR "numeric string"
shall be relevant only in contexts where that term is used in this
section.
.P
When an expression is used in a Boolean context, if it has a numeric
value, a value of zero shall be treated as false and any other value
shall be treated as true. Otherwise, a string value of the null string
shall be treated as false and any other value shall be treated as true.
A Boolean context shall be one of the following:
.IP " *" 4
The first subexpression of a conditional expression
.IP " *" 4
An expression operated on by logical NOT, logical AND, or logical OR
.IP " *" 4
The second expression of a
.BR for
statement
.IP " *" 4
The expression of an
.BR if
statement
.IP " *" 4
The expression of the
.BR while
clause in either a
.BR while
or
.BR do .\|.\|.\c
.BR while
statement
.IP " *" 4
An expression used as a pattern (as in Overall Program Structure)
.P
All arithmetic shall follow the semantics of floating-point arithmetic as
specified by the ISO\ C standard (see
.IR "Section 1.1.2" ", " "Concepts Derived from the ISO C Standard").
.P
The value of the expression:
.sp
.RS 4
.nf
\fB
\fIexpr1\fR ^ \fIexpr2\fR
.fi \fR
.P
.RE
.P
shall be equivalent to the value returned by the ISO\ C standard function call:
.sp
.RS 4
.nf
\fB
\fRpow(\fIexpr1\fR, \fIexpr2\fR)
.fi \fR
.P
.RE
.P
The expression:
.sp
.RS 4
.nf
\fB
lvalue ^= \fIexpr\fR
.fi \fR
.P
.RE
.P
shall be equivalent to the ISO\ C standard expression:
.sp
.RS 4
.nf
\fB
lvalue = pow(lvalue, \fIexpr\fR)
.fi \fR
.P
.RE
.P
except that lvalue shall be evaluated only once. The value of the
expression:
.sp
.RS 4
.nf
\fB
\fIexpr1\fR % \fIexpr2\fR
.fi \fR
.P
.RE
.P
shall be equivalent to the value returned by the ISO\ C standard function call:
.sp
.RS 4
.nf
\fB
fmod(\fIexpr1\fR, \fIexpr2\fR)
.fi \fR
.P
.RE
.P
The expression:
.sp
.RS 4
.nf
\fB
lvalue %= \fIexpr\fR
.fi \fR
.P
.RE
.P
shall be equivalent to the ISO\ C standard expression:
.sp
.RS 4
.nf
\fB
lvalue = fmod(lvalue, \fIexpr\fR)
.fi \fR
.P
.RE
.P
except that lvalue shall be evaluated only once.
.P
Variables and fields shall be set by the assignment statement:
.sp
.RS 4
.nf
\fB
lvalue = \fIexpression\fR
.fi \fR
.P
.RE
.P
and the type of
.IR expression
shall determine the resulting variable type. The assignment includes
the arithmetic assignments (\c
.BR \(dq+=\(dq ,
.BR \(dq\(mi=\(dq ,
.BR \(dq*=\(dq ,
.BR \(dq/=\(dq ,
.BR \(dq%=\(dq ,
.BR \(dq^=\(dq ,
.BR \(dq++\(dq ,
.BR \(dq\(mi\|\(mi\(dq )
all of which shall produce a numeric result. The left-hand side of an
assignment and the target of increment and decrement operators can be
one of a variable, an array with index, or a field selector.
.P
The
.IR awk
language supplies arrays that are used for storing numbers or strings.
Arrays need not be declared. They shall initially be empty, and their
sizes shall change dynamically. The subscripts, or element identifiers,
are strings, providing a type of associative array capability. An array
name followed by a subscript within square brackets can be used as an
lvalue and thus as an expression, as described in the grammar; see
.IR "Grammar".
Unsubscripted array names can be used in only the following contexts:
.IP " *" 4
A parameter in a function definition or function call
.IP " *" 4
The
.BR NAME
token following any use of the keyword
.BR in
as specified in the grammar (see
.IR "Grammar");
if the name used in this context is not an array name, the behavior is
undefined
.P
A valid array
.IR index
shall consist of one or more
<comma>-separated
expressions, similar to the way in which multi-dimensional arrays are
indexed in some programming languages. Because
.IR awk
arrays are really one-dimensional, such a
<comma>-separated
list shall be converted to a single string by concatenating the string
values of the separate expressions, each separated from the other by
the value of the
.BR SUBSEP
variable. Thus, the following two index operations shall be
equivalent:
.sp
.RS 4
.nf
\fB
\fIvar\fB[\fIexpr1\fR, \fIexpr2\fR, ... \fIexprn\fB]
.P
\fIvar\fB[\fIexpr1\fR SUBSEP \fIexpr2\fR SUBSEP ... \fRSUBSEP \fIexprn\fB]\fR
.fi \fR
.P
.RE
.P
The application shall ensure that a multi-dimensioned
.IR index
used with the
.BR in
operator is parenthesized. The
.BR in
operator, which tests for the existence of a particular array element,
shall not cause that element to exist. Any other reference to a
nonexistent array element shall automatically create it.
.P
Comparisons (with the
.BR '<' ,
.BR \(dq<=\(dq ,
.BR \(dq!=\(dq ,
.BR \(dq==\(dq ,
.BR '>' ,
and
.BR \(dq>=\(dq
operators) shall be made numerically if both operands are numeric, if
one is numeric and the other has a string value that is a numeric
string, or if one is numeric and the other has the uninitialized value.
Otherwise, operands shall be converted to strings as required and a
string comparison shall be made using the locale-specific collation
sequence. The value of the comparison expression shall be 1 if the
relation is true, or 0 if the relation is false.
.SS "Variables and Special Variables"
.P
Variables can be used in an
.IR awk
program by referencing them. With the exception of function parameters
(see
.IR "User-Defined Functions"),
they are not explicitly declared. Function parameter names shall be
local to the function; all other variable names shall be global. The
same name shall not be used as both a function parameter name and as
the name of a function or a special
.IR awk
variable. The same name shall not be used both as a variable name with
global scope and as the name of a function. The same name shall not be
used within the same scope both as a scalar variable and as an array.
Uninitialized variables, including scalar variables, array elements,
and field variables, shall have an uninitialized value. An
uninitialized value shall have both a numeric value of zero and a
string value of the empty string. Evaluation of variables with an
uninitialized value, to either string or numeric, shall be determined
by the context in which they are used.
.P
Field variables shall be designated by a
.BR '$'
followed by a number or numerical expression. The effect of the field
number
.IR expression
evaluating to anything other than a non-negative integer is
unspecified; uninitialized variables or string values need not be
converted to numeric values in this context. New field variables can be
created by assigning a value to them. References to nonexistent fields
(that is, fields after $\fBNF\fP), shall evaluate to the uninitialized
value. Such references shall not create new fields. However, assigning
to a nonexistent field (for example, $(\fBNF\fP+2)=5) shall increase
the value of
.BR NF ;
create any intervening fields with the uninitialized value; and cause
the value of $0 to be recomputed, with the fields being separated by
the value of
.BR OFS .
Each field variable shall have a string value or an uninitialized value
when created. Field variables shall have the uninitialized value when
created from $0 using
.BR FS
and the variable does not contain any characters. If appropriate, the
field variable shall be considered a numeric string (see
.IR "Expressions in awk").
.P
Implementations shall support the following other special variables
that are set by
.IR awk :
.IP "\fBARGC\fR" 10
The number of elements in the
.BR ARGV
array.
.IP "\fBARGV\fR" 10
An array of command line arguments, excluding options and the
.IR program
argument, numbered from zero to
.BR ARGC \(mi1.
.RS 10
.P
The arguments in
.BR ARGV
can be modified or added to;
.BR ARGC
can be altered. As each input file ends,
.IR awk
shall treat the next non-null element of
.BR ARGV ,
up to the current value of
.BR ARGC \(mi1,
inclusive, as the name of the next input file. Thus, setting an element
of
.BR ARGV
to null means that it shall not be treated as an input file. The name
.BR '\(mi'
indicates the standard input. If an argument matches the format of an
.IR assignment
operand, this argument shall be treated as an
.IR assignment
rather than a
.IR file
argument.
.RE
.IP "\fBCONVFMT\fR" 10
The
.BR printf
format for converting numbers to strings (except for output statements,
where
.BR OFMT
is used);
.BR \(dq%.6g\(dq
by default.
.IP "\fBENVIRON\fR" 10
An array representing the value of the environment, as described in the
.IR exec
functions defined in the System Interfaces volume of POSIX.1\(hy2008. The indices of the array shall be
strings consisting of the names of the environment variables, and the
value of each array element shall be a string consisting of the value
of that variable. If appropriate, the environment variable shall be
considered a
.IR "numeric string"
(see
.IR "Expressions in awk");
the array element shall also have its numeric value.
.RS 10
.P
In all cases where the behavior of
.IR awk
is affected by environment variables (including the environment of any
commands that
.IR awk
executes via the
.BR system
function or via pipeline redirections with the
.BR print
statement, the
.BR printf
statement, or the
.BR getline
function), the environment used shall be the environment at the time
.IR awk
began executing; it is implementation-defined whether any
modification of
.BR ENVIRON
affects this environment.
.RE
.IP "\fBFILENAME\fR" 10
A pathname of the current input file. Inside a
.BR BEGIN
action the value is undefined. Inside an
.BR END
action the value shall be the name of the last input file processed.
.IP "\fBFNR\fR" 10
The ordinal number of the current record in the current file. Inside a
.BR BEGIN
action the value shall be zero. Inside an
.BR END
action the value shall be the number of the last record processed in
the last file processed.
.IP "\fBFS\fR" 10
Input field separator regular expression; a
<space>
by default.
.IP "\fBNF\fR" 10
The number of fields in the current record. Inside a
.BR BEGIN
action, the use of
.BR NF
is undefined unless a
.BR getline
function without a
.IR var
argument is executed previously. Inside an
.BR END
action,
.BR NF
shall retain the value it had for the last record read, unless a
subsequent, redirected,
.BR getline
function without a
.IR var
argument is performed prior to entering the
.BR END
action.
.IP "\fBNR\fR" 10
The ordinal number of the current record from the start of input.
Inside a
.BR BEGIN
action the value shall be zero. Inside an
.BR END
action the value shall be the number of the last record processed.
.IP "\fBOFMT\fR" 10
The
.BR printf
format for converting numbers to strings in output statements (see
.IR "Output Statements");
.BR \(dq%.6g\(dq
by default. The result of the conversion is unspecified if the value of
.BR OFMT
is not a floating-point format specification.
.IP "\fBOFS\fR" 10
The
.BR print
statement output field separator;
<space>
by default.
.IP "\fBORS\fR" 10
The
.BR print
statement output record separator; a
<newline>
by default.
.IP "\fBRLENGTH\fR" 10
The length of the string matched by the
.BR match
function.
.IP "\fBRS\fR" 10
The first character of the string value of
.BR RS
shall be the input record separator; a
<newline>
by default. If
.BR RS
contains more than one character, the results are unspecified. If
.BR RS
is null, then records are separated by sequences consisting of a
<newline>
plus one or more blank lines, leading or trailing blank lines shall not
result in empty records at the beginning or end of the input, and a
<newline>
shall always be a field separator, no matter what the value of
.BR FS
is.
.IP "\fBRSTART\fR" 10
The starting position of the string matched by the
.BR match
function, numbering from 1. This shall always be equivalent to the
return value of the
.BR match
function.
.IP "\fBSUBSEP\fR" 10
The subscript separator string for multi-dimensional arrays; the
default value is implementation-defined.
.SS "Regular Expressions"
.P
The
.IR awk
utility shall make use of the extended regular expression notation
(see the Base Definitions volume of POSIX.1\(hy2008,
.IR "Section 9.4" ", " "Extended Regular Expressions")
except that it shall allow the use of C-language conventions
for escaping special characters within the EREs, as specified in the
table in the Base Definitions volume of POSIX.1\(hy2008,
.IR "Chapter 5" ", " "File Format Notation"
(\c
.BR '\e\e' ,
.BR '\ea' ,
.BR '\eb' ,
.BR '\ef' ,
.BR '\en' ,
.BR '\er' ,
.BR '\et' ,
.BR '\ev' )
and the following table; these escape sequences shall be recognized
both inside and outside bracket expressions. Note that records need not
be separated by
<newline>
characters and string constants can contain
<newline>
characters, so even the
.BR \(dq\en\(dq
sequence is valid in
.IR awk
EREs. Using a
<slash>
character within an ERE requires the escaping shown in the following
table.
.br
.sp
.ce 1
\fBTable 4-2: Escape Sequences in \fIawk\fP\fR
.ad l
.TS
center tab(@) box;
cB | cB | cB
cB | cB | cB
lf5 | lw(34) | lw(34).
Escape
Sequence@Description@Meaning
_
\e"@T{
<backslash> <quotation-mark>
T}@T{
<quotation-mark> character
T}
_
\e/@T{
<backslash> <slash>
T}@T{
<slash> character
T}
_
\eddd@T{
A
<backslash>
character followed by the longest sequence of one, two, or
three octal-digit characters (01234567). If all of the digits are 0
(that is, representation of the NUL character), the behavior is
undefined.
T}@T{
The character whose encoding is represented by the one, two, or
three-digit octal integer. Multi-byte characters require
multiple, concatenated escape sequences of this type, including the
leading
<backslash>
for each byte.
T}
_
\ec@T{
A
<backslash>
character followed by any character not described in this
table or in the table in the Base Definitions volume of POSIX.1\(hy2008,
.IR "Chapter 5" ", " "File Format Notation"
(\c
.BR '\e\e' ,
.BR '\ea' ,
.BR '\eb' ,
.BR '\ef' ,
.BR '\en' ,
.BR '\er' ,
.BR '\et' ,
.BR '\ev' ).
T}@Undefined
.TE
.ad b
.P
A regular expression can be matched against a specific field or string
by using one of the two regular expression matching operators,
.BR '~'
and
.BR \(dq!~\(dq .
These operators shall interpret their right-hand operand as a regular
expression and their left-hand operand as a string. If the regular
expression matches the string, the
.BR '~'
expression shall evaluate to a value of 1, and the
.BR \(dq!~\(dq
expression shall evaluate to a value of 0. (The regular expression
matching operation is as defined by the term matched in the Base Definitions volume of POSIX.1\(hy2008,
.IR "Section 9.1" ", " "Regular Expression Definitions",
where a match occurs on any part of the string unless the regular
expression is limited with the
<circumflex>
or
<dollar-sign>
special characters.) If the regular expression does not match the
string, the
.BR '~'
expression shall evaluate to a value of 0, and the
.BR \(dq!~\(dq
expression shall evaluate to a value of 1. If the right-hand operand is
any expression other than the lexical token
.BR ERE ,
the string value of the expression shall be interpreted as an extended
regular expression, including the escape conventions described above.
Note that these same escape conventions shall also be applied in
determining the value of a string literal (the lexical token
.BR STRING ),
and thus shall be applied a second time when a string literal is used
in this context.
.P
When an
.BR ERE
token appears as an expression in any context other than as the
right-hand of the
.BR '~'
or
.BR \(dq!~\(dq
operator or as one of the built-in function arguments described below,
the value of the resulting expression shall be the equivalent of:
.sp
.RS 4
.nf
\fB
$0 " " /\fIere\fR/
.fi \fR
.P
.RE
.P
The
.IR ere
argument to the
.BR gsub ,
.BR match ,
.BR sub
functions, and the
.IR fs
argument to the
.BR split
function (see
.IR "String Functions")
shall be interpreted as extended regular expressions. These can be
either
.BR ERE
tokens or arbitrary expressions, and shall be interpreted in the same
manner as the right-hand side of the
.BR '~'
or
.BR \(dq!~\(dq
operator.
.P
An extended regular expression can be used to separate fields by assigning
a string containing the expression to the built-in variable
.BR FS ,
either directly or as a consequence of using the
.BR \(miF
.IR sepstring
option.
The default value of the
.BR FS
variable shall be a single
<space>.
The following describes
.BR FS
behavior:
.IP " 1." 4
If
.BR FS
is a null string, the behavior is unspecified.
.IP " 2." 4
If
.BR FS
is a single character:
.RS 4
.IP " a." 4
If
.BR FS
is
<space>,
skip leading and trailing
<blank>
and
<newline>
characters; fields shall be delimited by sets of one or more
<blank>
or
<newline>
characters.
.IP " b." 4
Otherwise, if
.BR FS
is any other character
.IR c ,
fields shall be delimited by each single occurrence of
.IR c .
.RE
.IP " 3." 4
Otherwise, the string value of
.BR FS
shall be considered to be an extended regular expression. Each
occurrence of a sequence matching the extended regular expression shall
delimit fields.
.P
Except for the
.BR '~'
and
.BR \(dq!~\(dq
operators, and in the
.BR gsub ,
.BR match ,
.BR split ,
and
.BR sub
built-in functions, ERE matching shall be based on input records; that
is, record separator characters (the first character of the value of
the variable
.BR RS ,
<newline>
by default) cannot be embedded in the expression, and no expression
shall match the record separator character. If the record separator is
not
<newline>,
<newline>
characters embedded in the expression can be matched. For the
.BR '~'
and
.BR \(dq!~\(dq
operators, and in those four built-in functions, ERE matching shall be
based on text strings; that is, any character (including
<newline>
and the record separator) can be embedded in the pattern, and an
appropriate pattern shall match any character. However, in all
.IR awk
ERE matching, the use of one or more NUL characters in the pattern,
input record, or text string produces undefined results.
.SS "Patterns"
.P
A
.IR pattern
is any valid
.IR expression ,
a range specified by two expressions separated by a comma, or one of the
two special patterns
.BR BEGIN
or
.BR END .
.SS "Special Patterns"
.P
The
.IR awk
utility shall recognize two special patterns,
.BR BEGIN
and
.BR END .
Each
.BR BEGIN
pattern shall be matched once and its associated action executed before
the first record of input is read\(emexcept possibly by use of the
.BR getline
function (see
.IR "Input/Output and General Functions")
in a prior
.BR BEGIN
action\(emand before command line assignment is done. Each
.BR END
pattern shall be matched once and its associated action executed after
the last record of input has been read. These two patterns shall have
associated actions.
.P
.BR BEGIN
and
.BR END
shall not combine with other patterns. Multiple
.BR BEGIN
and
.BR END
patterns shall be allowed. The actions associated with the
.BR BEGIN
patterns shall be executed in the order specified in the program, as
are the
.BR END
actions. An
.BR END
pattern can precede a
.BR BEGIN
pattern in a program.
.P
If an
.IR awk
program consists of only actions with the pattern
.BR BEGIN ,
and the
.BR BEGIN
action contains no
.BR getline
function,
.IR awk
shall exit without reading its input when the last statement in the
last
.BR BEGIN
action is executed. If an
.IR awk
program consists of only actions with the pattern
.BR END
or only actions with the patterns
.BR BEGIN
and
.BR END ,
the input shall be read before the statements in the
.BR END
actions are executed.
.SS "Expression Patterns"
.P
An expression pattern shall be evaluated as if it were an expression in
a Boolean context. If the result is true, the pattern shall be
considered to match, and the associated action (if any) shall be
executed. If the result is false, the action shall not be executed.
.SS "Pattern Ranges"
.P
A pattern range consists of two expressions separated by a comma; in
this case, the action shall be performed for all records between a
match of the first expression and the following match of the second
expression, inclusive. At this point, the pattern range can be repeated
starting at input records subsequent to the end of the matched range.
.SS "Actions"
.P
An action is a sequence of statements as shown in the grammar in
.IR "Grammar".
Any single statement can be replaced by a statement list enclosed in
curly braces. The application shall ensure that statements in a
statement list are separated by
<newline>
or
<semicolon>
characters. Statements in a statement list shall be executed sequentially
in the order that they appear.
.P
The
.IR expression
acting as the conditional in an
.BR if
statement shall be evaluated and if it is non-zero or non-null, the
following statement shall be executed; otherwise, if
.BR else
is present, the statement following the
.BR else
shall be executed.
.P
The
.BR if ,
.BR while ,
.BR do .\|.\|.\c
.BR while ,
.BR for ,
.BR break ,
and
.BR continue
statements are based on the ISO\ C standard (see
.IR "Section 1.1.2" ", " "Concepts Derived from the ISO C Standard"),
except that the Boolean expressions shall be treated as described in
.IR "Expressions in awk",
and except in the case of:
.sp
.RS 4
.nf
\fB
for (\fIvariable\fR in \fIarray\fR)
.fi \fR
.P
.RE
.P
which shall iterate, assigning each
.IR index
of
.IR array
to
.IR variable
in an unspecified order. The results of adding new elements to
.IR array
within such a
.BR for
loop are undefined. If a
.BR break
or
.BR continue
statement occurs outside of a loop, the behavior is undefined.
.P
The
.BR delete
statement shall remove an individual array element. Thus, the following
code deletes an entire array:
.sp
.RS 4
.nf
\fB
for (index in array)
delete array[index]
.fi \fR
.P
.RE
.P
The
.BR next
statement shall cause all further processing of the current input
record to be abandoned. The behavior is undefined if a
.BR next
statement appears or is invoked in a
.BR BEGIN
or
.BR END
action.
.P
The
.BR exit
statement shall invoke all
.BR END
actions in the order in which they occur in the program source and then
terminate the program without reading further input. An
.BR exit
statement inside an
.BR END
action shall terminate the program without further execution of
.BR END
actions. If an expression is specified in an
.BR exit
statement, its numeric value shall be the exit status of
.IR awk ,
unless subsequent errors are encountered or a subsequent
.BR exit
statement with an expression is executed.
.SS "Output Statements"
.P
Both
.BR print
and
.BR printf
statements shall write to standard output by default. The output shall
be written to the location specified by
.IR output_redirection
if one is supplied, as follows:
.sp
.RS 4
.nf
\fB
> \fIexpression\fR
>> \fIexpression\fR
| \fIexpression\fR
.fi \fR
.P
.RE
.P
In all cases, the
.IR expression
shall be evaluated to produce a string that is used as a pathname
into which to write (for
.BR '>'
or
.BR \(dq>>\(dq )
or as a command to be executed (for
.BR '|' ).
Using the first two forms, if the file of that name is not currently
open, it shall be opened, creating it if necessary and using the first
form, truncating the file. The output then shall be appended to the
file. As long as the file remains open, subsequent calls in which
.IR expression
evaluates to the same string value shall simply append output to the
file. The file remains open until the
.BR close
function (see
.IR "Input/Output and General Functions")
is called with an expression that evaluates to the same string value.
.P
The third form shall write output onto a stream piped to the input of a
command. The stream shall be created if no stream is currently open
with the value of
.IR expression
as its command name. The stream created shall be equivalent to one
created by a call to the
\fIpopen\fR()
function defined in the System Interfaces volume of POSIX.1\(hy2008 with the value of
.IR expression
as the
.IR command
argument and a value of
.IR w
as the
.IR mode
argument. As long as the stream remains open, subsequent calls in which
.IR expression
evaluates to the same string value shall write output to the existing
stream. The stream shall remain open until the
.BR close
function (see
.IR "Input/Output and General Functions")
is called with an expression that evaluates to the same string value.
At that time, the stream shall be closed as if by a call to the
\fIpclose\fR()
function defined in the System Interfaces volume of POSIX.1\(hy2008.
.P
As described in detail by the grammar in
.IR "Grammar",
these output statements shall take a
<comma>-separated
list of
.IR expression s
referred to in the grammar by the non-terminal symbols
.BR expr_list ,
.BR print_expr_list ,
or
.BR print_expr_list_opt .
This list is referred to here as the
.IR "expression list" ,
and each member is referred to as an
.IR "expression argument" .
.P
The
.BR print
statement shall write the value of each expression argument onto the
indicated output stream separated by the current output field separator
(see variable
.BR OFS
above), and terminated by the output record separator (see variable
.BR ORS
above). All expression arguments shall be taken as strings, being
converted if necessary; this conversion shall be as described in
.IR "Expressions in awk",
with the exception that the
.BR printf
format in
.BR OFMT
shall be used instead of the value in
.BR CONVFMT .
An empty expression list shall stand for the whole input record ($0).
.P
The
.BR printf
statement shall produce output based on a notation similar to the
File Format Notation used to describe file formats in this volume of POSIX.1\(hy2008 (see the Base Definitions volume of POSIX.1\(hy2008,
.IR "Chapter 5" ", " "File Format Notation").
Output shall be produced as specified with the first
.IR expression
argument as the string
.IR format
and subsequent
.IR expression
arguments as the strings
.IR arg1
to
.IR argn ,
inclusive, with the following exceptions:
.IP " 1." 4
The
.IR format
shall be an actual character string rather than a graphical
representation. Therefore, it cannot contain empty character
positions. The
<space>
in the
.IR format
string, in any context other than a
.IR flag
of a conversion specification, shall be treated as an ordinary
character that is copied to the output.
.IP " 2." 4
If the character set contains a
.BR ' '
character and that character appears in the
.IR format
string, it shall be treated as an ordinary character that is copied to
the output.
.IP " 3." 4
The
.IR "escape sequences"
beginning with a
<backslash>
character shall be treated as sequences of ordinary characters that are
copied to the output. Note that these same sequences shall be interpreted
lexically by
.IR awk
when they appear in literal strings, but they shall not be treated
specially by the
.BR printf
statement.
.IP " 4." 4
A
.IR "field width"
or
.IR precision
can be specified as the
.BR '*'
character instead of a digit string. In this case the next argument
from the expression list shall be fetched and its numeric value taken
as the field width or precision.
.IP " 5." 4
The implementation shall not precede or follow output from the
.BR d
or
.BR u
conversion specifier characters with
<blank>
characters not specified by the
.IR format
string.
.IP " 6." 4
The implementation shall not precede output from the
.BR o
conversion specifier character with leading zeros not specified by the
.IR format
string.
.IP " 7." 4
For the
.BR c
conversion specifier character: if the argument has a numeric value, the
character whose encoding is that value shall be output. If the value is
zero or is not the encoding of any character in the character set, the
behavior is undefined. If the argument does not have a numeric value,
the first character of the string value shall be output; if the string
does not contain any characters, the behavior is undefined.
.IP " 8." 4
For each conversion specification that consumes an argument, the next
expression argument shall be evaluated. With the exception of the
.BR c
conversion specifier character, the value shall be converted (according
to the rules specified in
.IR "Expressions in awk")
to the appropriate type for the conversion specification.
.IP " 9." 4
If there are insufficient expression arguments to satisfy all the
conversion specifications in the
.IR format
string, the behavior is undefined.
.IP 10. 4
If any character sequence in the
.IR format
string begins with a
.BR '%'
character, but does not form a valid conversion specification, the
behavior is unspecified.
.P
Both
.BR print
and
.BR printf
can output at least
{LINE_MAX}
bytes.
.SS "Functions"
.P
The
.IR awk
language has a variety of built-in functions: arithmetic, string,
input/output, and general.
.SS "Arithmetic Functions"
.P
The arithmetic functions, except for
.BR int ,
shall be based on the ISO\ C standard (see
.IR "Section 1.1.2" ", " "Concepts Derived from the ISO C Standard").
The behavior is undefined in cases where the ISO\ C standard specifies that an
error be returned or that the behavior is undefined. Although the
grammar (see
.IR "Grammar")
permits built-in functions to appear with no arguments or parentheses,
unless the argument or parentheses are indicated as optional in the
following list (by displaying them within the
.BR \(dq[]\(dq
brackets), such use is undefined.
.IP "\fBatan2\fR(\fIy\fR,\fIx\fR)" 10
Return arctangent of \fIy\fP/\fIx\fR in radians in the range
[\(mi\(*p,\(*p].
.IP "\fBcos\fR(\fIx\fR)" 10
Return cosine of \fIx\fP, where \fIx\fP is in radians.
.IP "\fBsin\fR(\fIx\fR)" 10
Return sine of \fIx\fP, where \fIx\fP is in radians.
.IP "\fBexp\fR(\fIx\fR)" 10
Return the exponential function of \fIx\fP.
.IP "\fBlog\fR(\fIx\fR)" 10
Return the natural logarithm of \fIx\fP.
.IP "\fBsqrt\fR(\fIx\fR)" 10
Return the square root of \fIx\fP.
.IP "\fBint\fR(\fIx\fR)" 10
Return the argument truncated to an integer. Truncation shall
be toward 0 when \fIx\fP>0.
.IP "\fBrand\fP(\|)" 10
Return a random number \fIn\fP, such that 0\(<=\fIn\fP<1.
.IP "\fBsrand\fR(\fB[\fIexpr\fB]\fR)" 10
Set the seed value for
.IR rand
to
.IR expr
or use the time of day if
.IR expr
is omitted. The previous seed value shall be returned.
.SS "String Functions"
.P
The string functions in the following list shall be supported.
Although the grammar (see
.IR "Grammar")
permits built-in functions to appear with no arguments or parentheses,
unless the argument or parentheses are indicated as optional in the
following list (by displaying them within the
.BR \(dq[]\(dq
brackets), such use is undefined.
.IP "\fBgsub\fR(\fIere\fR,\ \fIrepl\fB[\fR,\ \fIin\fB]\fR)" 10
.br
Behave like
.BR sub
(see below), except that it shall replace all occurrences of the
regular expression (like the
.IR ed
utility global substitute) in $0 or in the
.IR in
argument, when specified.
.IP "\fBindex\fR(\fIs\fR,\ \fIt\fR)" 10
Return the position, in characters, numbering from 1, in string
.IR s
where string
.IR t
first occurs, or zero if it does not occur at all.
.IP "\fBlength[\fR(\fB[\fIs\fB]\fR)\fB]\fR" 10
Return the length, in characters, of its argument taken as a string, or
of the whole record, $0, if there is no argument.
.IP "\fBmatch\fR(\fIs\fR,\ \fIere\fR)" 10
Return the position, in characters, numbering from 1, in string
.IR s
where the extended regular expression
.IR ere
occurs, or zero if it does not occur at all. RSTART shall be set to the
starting position (which is the same as the returned value), zero if no
match is found; RLENGTH shall be set to the length of the matched
string, \(mi1 if no match is found.
.IP "\fBsplit\fR(\fIs\fR,\ \fIa\fB[\fR,\ \fIfs\ \fB]\fR)" 10
.br
Split the string
.IR s
into array elements
.IR a [1],
.IR a [2],
\&.\|.\|.,
.IR a [ n ],
and return
.IR n .
All elements of the array shall be deleted before the split is
performed. The separation shall be done with the ERE
.IR fs
or with the field separator
.BR FS
if
.IR fs
is not given. Each array element shall have a string value when created
and, if appropriate, the array element shall be considered a numeric
string (see
.IR "Expressions in awk").
The effect of a null string as the value of
.IR fs
is unspecified.
.IP "\fBsprintf\fR(\fIfmt\fR,\ \fIexpr\fR,\ \fIexpr\fR,\ .\|.\|.)" 10
.br
Format the expressions according to the
.BR printf
format given by
.IR fmt
and return the resulting string.
.IP "\fBsub(\fIere\fR,\ \fIrepl\fB[\fR,\ \fIin\ \fB]\fR)" 10
.br
Substitute the string
.IR repl
in place of the first instance of the extended regular expression
.IR ERE
in string
.IR in
and return the number of substitutions. An
<ampersand>
(\c
.BR '&' )
appearing in the string
.IR repl
shall be replaced by the string from
.IR in
that matches the ERE. An
<ampersand>
preceded with a
<backslash>
shall be interpreted as the literal
<ampersand>
character. An occurrence of two consecutive
<backslash>
characters shall be interpreted as just a single literal
<backslash>
character. Any other occurrence of a
<backslash>
(for example, preceding any other character) shall be treated as a
literal
<backslash>
character. Note that if
.IR repl
is a string literal (the lexical token
.BR STRING ;
see
.IR "Grammar"),
the handling of the
<ampersand>
character occurs after any lexical processing, including any lexical
<backslash>-escape
sequence processing. If
.IR in
is specified and it is not an lvalue (see
.IR "Expressions in awk"),
the behavior is undefined. If
.IR in
is omitted,
.IR awk
shall use the current record ($0) in its place.
.IP "\fBsubstr\fR(\fIs\fR,\ \fIm\fB[\fR,\ \fIn\ \fB]\fR)" 10
.br
Return the at most
.IR n -character
substring of
.IR s
that begins at position
.IR m ,
numbering from 1. If
.IR n
is omitted, or if
.IR n
specifies more characters than are left in the string, the length of
the substring shall be limited by the length of the string
.IR s .
.IP "\fBtolower\fR(\fIs\fR)" 10
Return a string based on the string
.IR s .
Each character in
.IR s
that is an uppercase letter specified to have a
.BR tolower
mapping by the
.IR LC_CTYPE
category of the current locale shall be replaced in the returned string
by the lowercase letter specified by the mapping. Other characters in
.IR s
shall be unchanged in the returned string.
.IP "\fBtoupper\fR(\fIs\fR)" 10
Return a string based on the string
.IR s .
Each character in
.IR s
that is a lowercase letter specified to have a
.BR toupper
mapping by the
.IR LC_CTYPE
category of the current locale is replaced in the returned string by
the uppercase letter specified by the mapping. Other characters in
.IR s
are unchanged in the returned string.
.P
All of the preceding functions that take
.IR ERE
as a parameter expect a pattern or a string valued expression that is a
regular expression as defined in
.IR "Regular Expressions".
.SS "Input/Output and General Functions"
.P
The input/output and general functions are:
.IP "\fBclose\fR(\fIexpression\fR)" 10
.br
Close the file or pipe opened by a
.BR print
or
.BR printf
statement or a call to
.BR getline
with the same string-valued
.IR expression .
The limit on the number of open
.IR expression
arguments is implementation-defined. If the close was successful, the
function shall return zero; otherwise, it shall return non-zero.
.IP "\fIexpression\ |\ \fBgetline\ [\fIvar\fB]\fR" 10
.br
Read a record of input from a stream piped from the output of a
command. The stream shall be created if no stream is currently open
with the value of
.IR expression
as its command name. The stream created shall be equivalent to one
created by a call to the
\fIpopen\fR()
function with the value of
.IR expression
as the
.IR command
argument and a value of
.IR r
as the
.IR mode
argument. As long as the stream remains open, subsequent calls in which
.IR expression
evaluates to the same string value shall read subsequent records from
the stream. The stream shall remain open until the
.BR close
function is called with an expression that evaluates to the same string
value. At that time, the stream shall be closed as if by a call to the
\fIpclose\fR()
function. If
.IR var
is omitted, $0 and
.BR NF
shall be set; otherwise,
.IR var
shall be set and, if appropriate, it shall be considered a numeric
string (see
.IR "Expressions in awk").
.RS 10
.P
The
.BR getline
operator can form ambiguous constructs when there are unparenthesized
operators (including concatenate) to the left of the
.BR '|'
(to the beginning of the expression containing
.BR getline ).
In the context of the
.BR '$'
operator,
.BR '|'
shall behave as if it had a lower precedence than
.BR '$' .
The result of evaluating other operators is unspecified, and conforming
applications shall parenthesize properly all such usages.
.RE
.IP "\fBgetline\fR" 10
Set $0 to the next input record from the current input file. This form
of
.BR getline
shall set the
.BR NF ,
.BR NR ,
and
.BR FNR
variables.
.IP "\fBgetline\ \fIvar\fR" 10
Set variable
.IR var
to the next input record from the current input file and, if
appropriate,
.IR var
shall be considered a numeric string (see
.IR "Expressions in awk").
This form of
.BR getline
shall set the
.BR FNR
and
.BR NR
variables.
.IP "\fBgetline\ \fB[\fIvar\fB]\ \fR<\ \fIexpression\fR" 10
.br
Read the next record of input from a named file. The
.IR expression
shall be evaluated to produce a string that is used as a pathname.
If the file of that name is not currently open, it shall be opened. As
long as the stream remains open, subsequent calls in which
.IR expression
evaluates to the same string value shall read subsequent records from
the file. The file shall remain open until the
.BR close
function is called with an expression that evaluates to the same string
value. If
.IR var
is omitted, $0 and
.BR NF
shall be set; otherwise,
.IR var
shall be set and, if appropriate, it shall be considered a numeric
string (see
.IR "Expressions in awk").
.RS 10
.P
The
.BR getline
operator can form ambiguous constructs when there are unparenthesized
binary operators (including concatenate) to the right of the
.BR '<'
(up to the end of the expression containing the
.BR getline ).
The result of evaluating such a construct is unspecified, and conforming
applications shall parenthesize properly all such usages.
.RE
.IP "\fBsystem\fR(\fIexpression\fR)" 10
.br
Execute the command given by
.IR expression
in a manner equivalent to the
\fIsystem\fR()
function defined in the System Interfaces volume of POSIX.1\(hy2008 and return the exit status of the
command.
.P
All forms of
.BR getline
shall return 1 for successful input, zero for end-of-file, and \(mi1
for an error.
.P
Where strings are used as the name of a file or pipeline, the
application shall ensure that the strings are textually identical. The
terminology ``same string value'' implies that ``equivalent strings'',
even those that differ only by
<space>
characters, represent different files.
.SS "User-Defined Functions"
.P
The
.IR awk
language also provides user-defined functions. Such functions can be
defined as:
.sp
.RS 4
.nf
\fB
function \fIname\fR(\fB[\fIparameter\fR, ...\fB]\fR) { \fIstatements\fR }
.fi \fR
.P
.RE
.P
A function can be referred to anywhere in an
.IR awk
program; in particular, its use can precede its definition. The scope
of a function is global.
.P
Function parameters, if present, can be either scalars or arrays; the
behavior is undefined if an array name is passed as a parameter that
the function uses as a scalar, or if a scalar expression is passed as a
parameter that the function uses as an array. Function parameters shall
be passed by value if scalar and by reference if array name.
.P
The number of parameters in the function definition need not match the
number of parameters in the function call. Excess formal parameters can
be used as local variables. If fewer arguments are supplied in a
function call than are in the function definition, the extra parameters
that are used in the function body as scalars shall evaluate to the
uninitialized value until they are otherwise initialized, and the extra
parameters that are used in the function body as arrays shall be
treated as uninitialized arrays where each element evaluates to the
uninitialized value until otherwise initialized.
.P
When invoking a function, no white space can be placed between the
function name and the opening parenthesis. Function calls can be nested
and recursive calls can be made upon functions. Upon return from any
nested or recursive function call, the values of all of the calling
function's parameters shall be unchanged, except for array parameters
passed by reference. The
.BR return
statement can be used to return a value. If a
.BR return
statement appears outside of a function definition, the behavior is
undefined.
.P
In the function definition,
<newline>
characters shall be optional before the opening brace and after the
closing brace. Function definitions can appear anywhere in the program
where a
.IR pattern-action
pair is allowed.
.SS "Grammar"
.P
The grammar in this section and the lexical conventions in the
following section shall together describe the syntax for
.IR awk
programs. The general conventions for this style of grammar are
described in
.IR "Section 1.3" ", " "Grammar Conventions".
A valid program can be represented as the non-terminal symbol
.IR program
in the grammar. This formal syntax shall take precedence over the
preceding text syntax description.
.sp
.RS 4
.nf
\fB
%token NAME NUMBER STRING ERE
%token FUNC_NAME /* Name followed by '(' without white space. */
.P
/* Keywords */
%token Begin End
/* 'BEGIN' 'END' */
.P
%token Break Continue Delete Do Else
/* 'break' 'continue' 'delete' 'do' 'else' */
.P
%token Exit For Function If In
/* 'exit' 'for' 'function' 'if' 'in' */
.P
%token Next Print Printf Return While
/* 'next' 'print' 'printf' 'return' 'while' */
.P
/* Reserved function names */
%token BUILTIN_FUNC_NAME
/* One token for the following:
* atan2 cos sin exp log sqrt int rand srand
* gsub index length match split sprintf sub
* substr tolower toupper close system
*/
%token GETLINE
/* Syntactically different from other built-ins. */
.P
/* Two-character tokens. */
%token ADD_ASSIGN SUB_ASSIGN MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN POW_ASSIGN
/* '+=' '\(mi=' '*=' '/=' '%=' '^=' */
.P
%token OR AND NO_MATCH EQ LE GE NE INCR DECR APPEND
/* '||' '&&' '!\^~' '==' '<=' '>=' '!=' '++' '\(mi\|\(mi' '>>' */
.P
/* One-character tokens. */
%token '{' '}' '(' ')' '[' ']' ',' ';' NEWLINE
%token '+' '\(mi' '*' '%' '^' '!' '>' '<' '|' '?' ':' ' " " ' '$' '='
.P
%start program
%%
.P
program : item_list
| actionless_item_list
;
.P
item_list : newline_opt
| actionless_item_list item terminator
| item_list item terminator
| item_list action terminator
;
.P
actionless_item_list : item_list pattern terminator
| actionless_item_list pattern terminator
;
.P
item : pattern action
| Function NAME '(' param_list_opt ')'
newline_opt action
| Function FUNC_NAME '(' param_list_opt ')'
newline_opt action
;
.P
param_list_opt : /* empty */
| param_list
;
.P
param_list : NAME
| param_list ',' NAME
;
.P
pattern : Begin
| End
| expr
| expr ',' newline_opt expr
;
.P
action : '{' newline_opt '}'
| '{' newline_opt terminated_statement_list '}'
| '{' newline_opt unterminated_statement_list '}'
;
.P
terminator : terminator ';'
| terminator NEWLINE
| ';'
| NEWLINE
;
.P
terminated_statement_list : terminated_statement
| terminated_statement_list terminated_statement
;
.P
unterminated_statement_list : unterminated_statement
| terminated_statement_list unterminated_statement
;
.P
terminated_statement : action newline_opt
| If '(' expr ')' newline_opt terminated_statement
| If '(' expr ')' newline_opt terminated_statement
Else newline_opt terminated_statement
| While '(' expr ')' newline_opt terminated_statement
| For '(' simple_statement_opt ';'
expr_opt ';' simple_statement_opt ')' newline_opt
terminated_statement
| For '(' NAME In NAME ')' newline_opt
terminated_statement
| ';' newline_opt
| terminatable_statement NEWLINE newline_opt
| terminatable_statement ';' newline_opt
;
.P
unterminated_statement : terminatable_statement
| If '(' expr ')' newline_opt unterminated_statement
| If '(' expr ')' newline_opt terminated_statement
Else newline_opt unterminated_statement
| While '(' expr ')' newline_opt unterminated_statement
| For '(' simple_statement_opt ';'
expr_opt ';' simple_statement_opt ')' newline_opt
unterminated_statement
| For '(' NAME In NAME ')' newline_opt
unterminated_statement
;
.P
terminatable_statement : simple_statement
| Break
| Continue
| Next
| Exit expr_opt
| Return expr_opt
| Do newline_opt terminated_statement While '(' expr ')'
;
.P
simple_statement_opt : /* empty */
| simple_statement
;
.P
simple_statement : Delete NAME '[' expr_list ']'
| expr
| print_statement
;
.P
print_statement : simple_print_statement
| simple_print_statement output_redirection
;
.P
simple_print_statement : Print print_expr_list_opt
| Print '(' multiple_expr_list ')'
| Printf print_expr_list
| Printf '(' multiple_expr_list ')'
;
.P
output_redirection : '>' expr
| APPEND expr
| '|' expr
;
.P
expr_list_opt : /* empty */
| expr_list
;
.P
expr_list : expr
| multiple_expr_list
;
.P
multiple_expr_list : expr ',' newline_opt expr
| multiple_expr_list ',' newline_opt expr
;
.P
expr_opt : /* empty */
| expr
;
.P
expr : unary_expr
| non_unary_expr
;
.P
unary_expr : '+' expr
| '\(mi' expr
| unary_expr '^' expr
| unary_expr '*' expr
| unary_expr '/' expr
| unary_expr '%' expr
| unary_expr '+' expr
| unary_expr '\(mi' expr
| unary_expr non_unary_expr
| unary_expr '<' expr
| unary_expr LE expr
| unary_expr NE expr
| unary_expr EQ expr
| unary_expr '>' expr
| unary_expr GE expr
| unary_expr '~' expr
| unary_expr NO_MATCH expr
| unary_expr In NAME
| unary_expr AND newline_opt expr
| unary_expr OR newline_opt expr
| unary_expr '?' expr ':' expr
| unary_input_function
;
.P
non_unary_expr : '(' expr ')'
| '!' expr
| non_unary_expr '^' expr
| non_unary_expr '*' expr
| non_unary_expr '/' expr
| non_unary_expr '%' expr
| non_unary_expr '+' expr
| non_unary_expr '\(mi' expr
| non_unary_expr non_unary_expr
| non_unary_expr '<' expr
| non_unary_expr LE expr
| non_unary_expr NE expr
| non_unary_expr EQ expr
| non_unary_expr '>' expr
| non_unary_expr GE expr
| non_unary_expr '~' expr
| non_unary_expr NO_MATCH expr
| non_unary_expr In NAME
| '(' multiple_expr_list ')' In NAME
| non_unary_expr AND newline_opt expr
| non_unary_expr OR newline_opt expr
| non_unary_expr '?' expr ':' expr
| NUMBER
| STRING
| lvalue
| ERE
| lvalue INCR
| lvalue DECR
| INCR lvalue
| DECR lvalue
| lvalue POW_ASSIGN expr
| lvalue MOD_ASSIGN expr
| lvalue MUL_ASSIGN expr
| lvalue DIV_ASSIGN expr
| lvalue ADD_ASSIGN expr
| lvalue SUB_ASSIGN expr
| lvalue '=' expr
| FUNC_NAME '(' expr_list_opt ')'
/* no white space allowed before '(' */
| BUILTIN_FUNC_NAME '(' expr_list_opt ')'
| BUILTIN_FUNC_NAME
| non_unary_input_function
;
.P
print_expr_list_opt : /* empty */
| print_expr_list
;
.P
print_expr_list : print_expr
| print_expr_list ',' newline_opt print_expr
;
.P
print_expr : unary_print_expr
| non_unary_print_expr
;
.P
unary_print_expr : '+' print_expr
| '\(mi' print_expr
| unary_print_expr '^' print_expr
| unary_print_expr '*' print_expr
| unary_print_expr '/' print_expr
| unary_print_expr '%' print_expr
| unary_print_expr '+' print_expr
| unary_print_expr '\(mi' print_expr
| unary_print_expr non_unary_print_expr
| unary_print_expr '~' print_expr
| unary_print_expr NO_MATCH print_expr
| unary_print_expr In NAME
| unary_print_expr AND newline_opt print_expr
| unary_print_expr OR newline_opt print_expr
| unary_print_expr '?' print_expr ':' print_expr
;
.P
non_unary_print_expr : '(' expr ')'
| '!' print_expr
| non_unary_print_expr '^' print_expr
| non_unary_print_expr '*' print_expr
| non_unary_print_expr '/' print_expr
| non_unary_print_expr '%' print_expr
| non_unary_print_expr '+' print_expr
| non_unary_print_expr '\(mi' print_expr
| non_unary_print_expr non_unary_print_expr
| non_unary_print_expr '~' print_expr
| non_unary_print_expr NO_MATCH print_expr
| non_unary_print_expr In NAME
| '(' multiple_expr_list ')' In NAME
| non_unary_print_expr AND newline_opt print_expr
| non_unary_print_expr OR newline_opt print_expr
| non_unary_print_expr '?' print_expr ':' print_expr
| NUMBER
| STRING
| lvalue
| ERE
| lvalue INCR
| lvalue DECR
| INCR lvalue
| DECR lvalue
| lvalue POW_ASSIGN print_expr
| lvalue MOD_ASSIGN print_expr
| lvalue MUL_ASSIGN print_expr
| lvalue DIV_ASSIGN print_expr
| lvalue ADD_ASSIGN print_expr
| lvalue SUB_ASSIGN print_expr
| lvalue '=' print_expr
| FUNC_NAME '(' expr_list_opt ')'
/* no white space allowed before '(' */
| BUILTIN_FUNC_NAME '(' expr_list_opt ')'
| BUILTIN_FUNC_NAME
;
.P
lvalue : NAME
| NAME '[' expr_list ']'
| '$' expr
;
.P
non_unary_input_function : simple_get
| simple_get '<' expr
| non_unary_expr '|' simple_get
;
.P
unary_input_function : unary_expr '|' simple_get
;
.P
simple_get : GETLINE
| GETLINE lvalue
;
.P
newline_opt : /* empty */
| newline_opt NEWLINE
;
.fi \fR
.P
.RE
.P
This grammar has several ambiguities that shall be resolved as
follows:
.IP " *" 4
Operator precedence and associativity shall be as described in
.IR "Table 4-1, Expressions in Decreasing Precedence in \fIawk\fP".
.IP " *" 4
In case of ambiguity, an
.BR else
shall be associated with the most immediately preceding
.BR if
that would satisfy the grammar.
.IP " *" 4
In some contexts, a
<slash>
(\c
.BR '/' )
that is used to surround an ERE could also be the division operator.
This shall be resolved in such a way that wherever the division
operator could appear, a
<slash>
is assumed to be the division operator. (There is no unary division
operator.)
.P
Each expression in an
.IR awk
program shall conform to the precedence and associativity rules, even
when this is not needed to resolve an ambiguity. For example, because
.BR '$'
has higher precedence than
.BR '++' ,
the string
.BR \(dq$x++\(mi\|\(mi\(dq
is not a valid
.IR awk
expression, even though it is unambiguously parsed by the grammar as
.BR \(dq$(x++)\(mi\|\(mi\(dq .
.P
One convention that might not be obvious from the formal grammar is
where
<newline>
characters are acceptable. There are several obvious placements such as
terminating a statement, and a
<backslash>
can be used to escape
<newline>
characters between any lexical tokens. In addition,
<newline>
characters without
<backslash>
characters can follow a comma, an open brace, logical AND operator (\c
.BR \(dq&&\(dq ),
logical OR operator (\c
.BR \(dq||\(dq ),
the
.BR do
keyword, the
.BR else
keyword, and the closing parenthesis of an
.BR if ,
.BR for ,
or
.BR while
statement. For example:
.sp
.RS 4
.nf
\fB
{ print $1,
$2 }
.fi \fR
.P
.RE
.SS "Lexical Conventions"
.P
The lexical conventions for
.IR awk
programs, with respect to the preceding grammar, shall be as follows:
.IP " 1." 4
Except as noted,
.IR awk
shall recognize the longest possible token or delimiter beginning at a
given point.
.IP " 2." 4
A comment shall consist of any characters beginning with the
<number-sign>
character and terminated by, but excluding the next occurrence of, a
<newline>.
Comments shall have no effect, except to delimit lexical tokens.
.IP " 3." 4
The
<newline>
shall be recognized as the token
.BR NEWLINE .
.IP " 4." 4
A
<backslash>
character immediately followed by a
<newline>
shall have no effect.
.IP " 5." 4
The token
.BR STRING
shall represent a string constant. A string constant shall begin with
the character
.BR '\&"' .
Within a string constant, a
<backslash>
character shall be considered to begin an escape sequence as specified
in the table in the Base Definitions volume of POSIX.1\(hy2008,
.IR "Chapter 5" ", " "File Format Notation"
(\c
.BR '\e\e' ,
.BR '\ea' ,
.BR '\eb' ,
.BR '\ef' ,
.BR '\en' ,
.BR '\er' ,
.BR '\et' ,
.BR '\ev' ).
In addition, the escape sequences in
.IR "Table 4-2, Escape Sequences in \fIawk\fP"
shall be recognized. A
<newline>
shall not occur within a string constant. A string constant shall be
terminated by the first unescaped occurrence of the character
.BR '\&"'
after the one that begins the string constant. The value of the string
shall be the sequence of all unescaped characters and values of escape
sequences between, but not including, the two delimiting
.BR '\&"'
characters.
.IP " 6." 4
The token
.BR ERE
represents an extended regular expression constant. An ERE constant
shall begin with the
<slash>
character. Within an ERE constant, a
<backslash>
character shall be considered to begin an escape sequence as
specified in the table in the Base Definitions volume of POSIX.1\(hy2008,
.IR "Chapter 5" ", " "File Format Notation".
In addition, the escape sequences in
.IR "Table 4-2, Escape Sequences in \fIawk\fP"
shall be recognized. The application shall ensure that a
<newline>
does not occur within an ERE constant. An ERE constant shall be
terminated by the first unescaped occurrence of the
<slash>
character after the one that begins the ERE constant. The extended regular
expression represented by the ERE constant shall be the sequence of all
unescaped characters and values of escape sequences between, but not
including, the two delimiting
<slash>
characters.
.IP " 7." 4
A
<blank>
shall have no effect, except to delimit lexical tokens or within
.BR STRING
or
.BR ERE
tokens.
.IP " 8." 4
The token
.BR NUMBER
shall represent a numeric constant. Its form and numeric value shall
either be equivalent to the
.BR decimal-floating-constant
token as specified by the ISO\ C standard, or it shall be a sequence of decimal
digits and shall be evaluated as an integer constant in decimal. In
addition, implementations may accept numeric constants with the form
and numeric value equivalent to the
.BR hexadecimal-constant
and
.BR hexadecimal-floating-constant
tokens as specified by the ISO\ C standard.
.RS 4
.P
If the value is too large or too small to be representable (see
.IR "Section 1.1.2" ", " "Concepts Derived from the ISO C Standard"),
the behavior is undefined.
.RE
.IP " 9." 4
A sequence of underscores, digits, and alphabetics from the portable
character set (see the Base Definitions volume of POSIX.1\(hy2008,
.IR "Section 6.1" ", " "Portable Character Set"),
beginning with an
<underscore>
or alphabetic character, shall be considered a word.
.IP 10. 4
The following words are keywords that shall be recognized as individual
tokens; the name of the token is the same as the keyword:
.TS
tab(@);
lw(0.6i)eB leB leB leB leB leB.
T{
.nf
BEGIN
break
continue
T}@T{
.nf
delete
do
else
T}@T{
.nf
END
exit
for
T}@T{
.nf
function
getline
if
T}@T{
.nf
in
next
print
T}@T{
.nf
printf
return
while
T}
.TE
.IP 11. 4
The following words are names of built-in functions and shall be
recognized as the token
.BR BUILTIN_FUNC_NAME :
.TS
tab(@);
lw(0.6i)eB leB leB leB leB leB.
T{
.nf
atan2
close
cos
exp
T}@T{
.nf
gsub
index
int
length
T}@T{
.nf
log
match
rand
sin
T}@T{
.nf
split
sprintf
sqrt
srand
T}@T{
.nf
sub
substr
system
tolower
T}@T{
.nf
toupper
.fi
T}
.TE
.RS 4
.P
The above-listed keywords and names of built-in functions are
considered reserved words.
.RE
.IP 12. 4
The token
.BR NAME
shall consist of a word that is not a keyword or a name of a built-in
function and is not followed immediately (without any delimiters) by
the
.BR '('
character.
.IP 13. 4
The token
.BR FUNC_NAME
shall consist of a word that is not a keyword or a name of a built-in
function, followed immediately (without any delimiters) by the
.BR '('
character. The
.BR '('
character shall not be included as part of the token.
.IP 14. 4
The following two-character sequences shall be recognized as the named
tokens:
.TS
box center tab(@);
cB | cB | cB | cB
lB | cf5 | lB | cf5.
Token Name@Sequence@Token Name@Sequence
_
ADD_ASSIGN@+=@NO_MATCH@!~
SUB_ASSIGN@\(mi=@EQ@==
MUL_ASSIGN@*=@LE@<=
DIV_ASSIGN@/=@GE@>=
MOD_ASSIGN@%=@NE@!=
POW_ASSIGN@^=@INCR@++
OR@||@DECR@\(mi\|\(mi
AND@&&@APPEND@>>
.TE
.IP 15. 4
The following single characters shall be recognized as tokens whose
names are the character:
.RS 4
.sp
.RS 4
.nf
\fB
<newline> { } ( ) [ ] , ; + \(mi * % ^ ! > < | ? : " " $ =
.fi \fR
.P
.RE
.RE
.P
There is a lexical ambiguity between the token
.BR ERE
and the tokens
.BR '/'
and
.BR DIV_ASSIGN .
When an input sequence begins with a
<slash>
character in any syntactic context where the token
.BR '/'
or
.BR DIV_ASSIGN
could appear as the next token in a valid program, the longer of those
two tokens that can be recognized shall be recognized. In any other
syntactic context where the token
.BR ERE
could appear as the next token in a valid program, the token
.BR ERE
shall be recognized.
.SH "EXIT STATUS"
The following exit values shall be returned:
.IP "\00" 6
All input files were processed successfully.
.IP >0 6
An error occurred.
.P
The exit status can be altered within the program by using an
.BR exit
expression.
.SH "CONSEQUENCES OF ERRORS"
If any
.IR file
operand is specified and the named file cannot be accessed,
.IR awk
shall write a diagnostic message to standard error and terminate
without any further action.
.P
If the program specified by either the
.IR program
operand or a
.IR progfile
operand is not a valid
.IR awk
program (as specified in the EXTENDED DESCRIPTION section), the
behavior is undefined.
.LP
.IR "The following sections are informative."
.SH "APPLICATION USAGE"
The
.BR index ,
.BR length ,
.BR match ,
and
.BR substr
functions should not be confused with similar functions in the ISO\ C standard;
the
.IR awk
versions deal with characters, while the ISO\ C standard deals with bytes.
.P
Because the concatenation operation is represented by adjacent
expressions rather than an explicit operator, it is often necessary to
use parentheses to enforce the proper evaluation precedence.
.SH EXAMPLES
The
.IR awk
program specified in the command line is most easily specified within
single-quotes (for example, '\fIprogram\fP') for applications using
.IR sh ,
because
.IR awk
programs commonly contain characters that are special to the shell,
including double-quotes. In the cases where an
.IR awk
program contains single-quote characters, it is usually easiest to
specify most of the program as strings within single-quotes
concatenated by the shell with quoted single-quote characters. For
example:
.sp
.RS 4
.nf
\fB
awk '/'\e''/ { print "quote:", $0 }'
.fi \fR
.P
.RE
.P
prints all lines from the standard input containing a single-quote
character, prefixed with
.IR quote :.
.P
The following are examples of simple
.IR awk
programs:
.IP " 1." 4
Write to the standard output all input lines for which field 3 is
greater than 5:
.RS 4
.sp
.RS 4
.nf
\fB
$3 > 5
.fi \fR
.P
.RE
.RE
.IP " 2." 4
Write every tenth line:
.RS 4
.sp
.RS 4
.nf
\fB
(NR % 10) == 0
.fi \fR
.P
.RE
.RE
.IP " 3." 4
Write any line with a substring matching the regular expression:
.RS 4
.sp
.RS 4
.nf
\fB
/(G|D)(2[0\(mi9][[:alpha:]]*)/
.fi \fR
.P
.RE
.RE
.IP " 4." 4
Print any line with a substring containing a
.BR 'G'
or
.BR 'D' ,
followed by a sequence of digits and characters. This example uses
character classes
.BR digit
and
.BR alpha
to match language-independent digit and alphabetic characters
respectively:
.RS 4
.sp
.RS 4
.nf
\fB
/(G|D)([[:digit:][:alpha:]]*)/
.fi \fR
.P
.RE
.RE
.IP " 5." 4
Write any line in which the second field matches the regular expression
and the fourth field does not:
.RS 4
.sp
.RS 4
.nf
\fB
$2 " " /xyz/ && $4 ! " " /xyz/
.fi \fR
.P
.RE
.RE
.IP " 6." 4
Write any line in which the second field contains a
<backslash>:
.RS 4
.sp
.RS 4
.nf
\fB
$2 " " /\e\e/
.fi \fR
.P
.RE
.RE
.IP " 7." 4
Write any line in which the second field contains a
<backslash>.
Note that
<backslash>-escapes
are interpreted twice; once in lexical processing of the string and once
in processing the regular expression:
.RS 4
.sp
.RS 4
.nf
\fB
$2 " " "\e\e\e\e"
.fi \fR
.P
.RE
.RE
.IP " 8." 4
Write the second to the last and the last field in each line. Separate
the fields by a
<colon>:
.RS 4
.sp
.RS 4
.nf
\fB
{OFS=":";print $(NF\(mi1), $NF}
.fi \fR
.P
.RE
.RE
.IP " 9." 4
Write the line number and number of fields in each line. The three
strings representing the line number, the
<colon>,
and the number of fields are concatenated and that string is written to
standard output:
.RS 4
.sp
.RS 4
.nf
\fB
{print NR ":" NF}
.fi \fR
.P
.RE
.RE
.IP 10. 4
Write lines longer than 72 characters:
.RS 4
.sp
.RS 4
.nf
\fB
length($0) > 72
.fi \fR
.P
.RE
.RE
.IP 11. 4
Write the first two fields in opposite order separated by
.BR OFS :
.RS 4
.sp
.RS 4
.nf
\fB
{ print $2, $1 }
.fi \fR
.P
.RE
.RE
.IP 12. 4
Same, with input fields separated by a
<comma>
or
<space>
and
<tab>
characters, or both:
.RS 4
.sp
.RS 4
.nf
\fB
BEGIN { FS = ",[ \et]*|[ \et]+" }
{ print $2, $1 }
.fi \fR
.P
.RE
.RE
.IP 13. 4
Add up the first column, print sum, and average:
.RS 4
.sp
.RS 4
.nf
\fB
{s += $1 }
END {print "sum is ", s, " average is", s/NR}
.fi \fR
.P
.RE
.RE
.IP 14. 4
Write fields in reverse order, one per line (many lines out for each
line in):
.RS 4
.sp
.RS 4
.nf
\fB
{ for (i = NF; i > 0; \(mi\|\(mii) print $i }
.fi \fR
.P
.RE
.RE
.IP 15. 4
Write all lines between occurrences of the strings
.BR start
and
.BR stop :
.RS 4
.sp
.RS 4
.nf
\fB
/start/, /stop/
.fi \fR
.P
.RE
.RE
.IP 16. 4
Write all lines whose first field is different from the previous one:
.RS 4
.sp
.RS 4
.nf
\fB
$1 != prev { print; prev = $1 }
.fi \fR
.P
.RE
.RE
.IP 17. 4
Simulate
.IR echo :
.RS 4
.sp
.RS 4
.nf
\fB
BEGIN {
for (i = 1; i < ARGC; ++i)
printf("%s%s", ARGV[i], i==ARGC\(mi1?"\en":" ")
}
.fi \fR
.P
.RE
.RE
.IP 18. 4
Write the path prefixes contained in the
.IR PATH
environment variable, one per line:
.RS 4
.sp
.RS 4
.nf
\fB
BEGIN {
n = split (ENVIRON["PATH"], path, ":")
for (i = 1; i <= n; ++i)
print path[i]
}
.fi \fR
.P
.RE
.RE
.IP 19. 4
If there is a file named
.BR input
containing page headers of the form:
Page #
.RS 4
.P
and a file named
.BR program
that contains:
.sp
.RS 4
.nf
\fB
/Page/ { $2 = n++; }
{ print }
.fi \fR
.P
.RE
then the command line:
.sp
.RS 4
.nf
\fB
awk \(mif program n=5 input
.fi \fR
.P
.RE
.P
prints the file
.BR input ,
filling in page numbers starting at 5.
.RE
.SH RATIONALE
This description is based on the new
.IR awk ,
``nawk'', (see the referenced \fIThe AWK Programming Language\fP), which introduced a number of new features to
the historical
.IR awk :
.IP " 1." 4
New keywords:
.BR delete ,
.BR do ,
.BR function ,
.BR return
.IP " 2." 4
New built-in functions:
.BR atan2 ,
.BR close ,
.BR cos ,
.BR gsub ,
.BR match ,
.BR rand ,
.BR sin ,
.BR srand ,
.BR sub ,
.BR system
.IP " 3." 4
New predefined variables:
.BR FNR ,
.BR ARGC ,
.BR ARGV ,
.BR RSTART ,
.BR RLENGTH ,
.BR SUBSEP
.IP " 4." 4
New expression operators:
.BR ? ,
.BR : ,
.BR , ,
.BR ^
.IP " 5." 4
The
.BR FS
variable and the third argument to
.BR split ,
now treated as extended regular expressions.
.IP " 6." 4
The operator precedence, changed to more closely match the C language.
Two examples of code that operate differently are:
.RS 4
.sp
.RS 4
.nf
\fB
while ( n /= 10 > 1) ...
if (!"wk" ~ /bwk/) ...
.fi \fR
.P
.RE
.RE
.P
Several features have been added based on newer implementations of
.IR awk :
.IP " *" 4
Multiple instances of
.BR \(mif
.IR progfile
are permitted.
.IP " *" 4
The new option
.BR \(miv
.IR assignment.
.IP " *" 4
The new predefined variable
.BR ENVIRON .
.IP " *" 4
New built-in functions
.BR toupper
and
.BR tolower .
.IP " *" 4
More formatting capabilities are added to
.BR printf
to match the ISO\ C standard.
.P
The overall
.IR awk
syntax has always been based on the C language, with a few features
from the shell command language and other sources. Because of this, it
is not completely compatible with any other language, which has caused
confusion for some users. It is not the intent of the standard
developers to address such issues. A few relatively minor changes
toward making the language more compatible with the ISO\ C standard were
made; most of these changes are based on similar changes in recent
implementations, as described above. There remain several C-language
conventions that are not in
.IR awk .
One of the notable ones is the
<comma>
operator, which is commonly used to specify multiple expressions in the
C language
.BR for
statement. Also, there are various places where
.IR awk
is more restrictive than the C language regarding the type of
expression that can be used in a given context. These limitations are
due to the different features that the
.IR awk
language does provide.
.P
Regular expressions in
.IR awk
have been extended somewhat from historical implementations to make
them a pure superset of extended regular expressions, as defined by
POSIX.1\(hy2008 (see the Base Definitions volume of POSIX.1\(hy2008,
.IR "Section 9.4" ", " "Extended Regular Expressions").
The main extensions are internationalization
features and interval expressions. Historical implementations of
.IR awk
have long supported
<backslash>-escape
sequences as an extension to extended regular expressions, and
this extension has been retained despite inconsistency with other
utilities. The number of escape sequences recognized in both extended
regular expressions and strings has varied (generally increasing with
time) among implementations. The set specified by POSIX.1\(hy2008 includes most
sequences known to be supported by popular implementations and by the
ISO\ C standard. One sequence that is not supported is hexadecimal value escapes
beginning with
.BR '\ex' .
This would allow values expressed in more than 9 bits to be used within
.IR awk
as in the ISO\ C standard. However, because this syntax has a non-deterministic
length, it does not permit the subsequent character to be a hexadecimal
digit. This limitation can be dealt with in the C language by the use
of lexical string concatenation. In the
.IR awk
language, concatenation could also be a solution for strings, but not
for extended regular expressions (either lexical ERE tokens or strings
used dynamically as regular expressions). Because of this limitation,
the feature has not been added to POSIX.1\(hy2008.
.P
When a string variable is used in a context where an extended regular
expression normally appears (where the lexical token ERE is used in the
grammar) the string does not contain the literal
<slash>
characters.
.P
Some versions of
.IR awk
allow the form:
.sp
.RS 4
.nf
\fB
func name(args, ... ) { statements }
.fi \fR
.P
.RE
.P
This has been deprecated by the authors of the language, who asked that
it not be specified.
.P
Historical implementations of
.IR awk
produce an error if a
.BR next
statement is executed in a
.BR BEGIN
action, and cause
.IR awk
to terminate if a
.BR next
statement is executed in an
.BR END
action. This behavior has not been documented, and it was not believed
that it was necessary to standardize it.
.P
The specification of conversions between string and numeric values is
much more detailed than in the documentation of historical
implementations or in the referenced \fIThe AWK Programming Language\fP. Although most of the behavior is
designed to be intuitive, the details are necessary to ensure
compatible behavior from different implementations. This is especially
important in relational expressions since the types of the operands
determine whether a string or numeric comparison is performed. From the
perspective of an application developer, it is usually sufficient to
expect intuitive behavior and to force conversions (by adding zero or
concatenating a null string) when the type of an expression does not
obviously match what is needed. The intent has been to specify
historical practice in almost all cases. The one exception is that, in
historical implementations, variables and constants maintain both
string and numeric values after their original value is converted by
any use. This means that referencing a variable or constant can have
unexpected side-effects. For example, with historical implementations
the following program:
.sp
.RS 4
.nf
\fB
{
a = "+2"
b = 2
if (NR % 2)
c = a + b
if (a == b)
print "numeric comparison"
else
print "string comparison"
}
.fi \fR
.P
.RE
.P
would perform a numeric comparison (and output numeric comparison) for
each odd-numbered line, but perform a string comparison (and output
string comparison) for each even-numbered line. POSIX.1\(hy2008 ensures that
comparisons will be numeric if necessary. With historical
implementations, the following program:
.sp
.RS 4
.nf
\fB
BEGIN {
OFMT = "%e"
print 3.14
OFMT = "%f"
print 3.14
}
.fi \fR
.P
.RE
.P
would output
.BR \(dq3.140000e+00\(dq
twice, because in the second
.BR print
statement the constant
.BR \(dq3.14\(dq
would have a string value from the previous conversion. POSIX.1\(hy2008 requires
that the output of the second
.BR print
statement be
.BR \(dq3.140000\(dq .
The behavior of historical implementations was seen as too unintuitive
and unpredictable.
.P
It was pointed out that with the rules contained in early drafts, the
following script would print nothing:
.sp
.RS 4
.nf
\fB
BEGIN {
y[1.5] = 1
OFMT = "%e"
print y[1.5]
}
.fi \fR
.P
.RE
.P
Therefore, a new variable,
.BR CONVFMT ,
was introduced. The
.BR OFMT
variable is now restricted to affecting output conversions of numbers
to strings and
.BR CONVFMT
is used for internal conversions, such as comparisons or array
indexing. The default value is the same as that for
.BR OFMT ,
so unless a program changes
.BR CONVFMT
(which no historical program would do), it will receive the historical
behavior associated with internal string conversions.
.P
The POSIX
.IR awk
lexical and syntactic conventions are specified more formally than in
other sources. Again the intent has been to specify historical
practice. One convention that may not be obvious from the formal
grammar as in other verbal descriptions is where
<newline>
characters are acceptable. There are several obvious placements such as
terminating a statement, and a
<backslash>
can be used to escape
<newline>
characters between any lexical tokens. In addition,
<newline>
characters without
<backslash>
characters can follow a comma, an open brace, a logical AND operator (\c
.BR \(dq&&\(dq ),
a logical OR operator (\c
.BR \(dq||\(dq ),
the
.BR do
keyword, the
.BR else
keyword, and the closing parenthesis of an
.BR if ,
.BR for ,
or
.BR while
statement. For example:
.sp
.RS 4
.nf
\fB
{ print $1,
$2 }
.fi \fR
.P
.RE
.P
The requirement that
.IR awk
add a trailing
<newline>
to the program argument text is to simplify the grammar, making it
match a text file in form. There is no way for an application or test
suite to determine whether a literal
<newline>
is added or whether
.IR awk
simply acts as if it did.
.P
POSIX.1\(hy2008 requires several changes from historical implementations in order
to support internationalization. Probably the most subtle of these is
the use of the decimal-point character, defined by the
.IR LC_NUMERIC
category of the locale, in representations of floating-point numbers.
This locale-specific character is used in recognizing numeric input, in
converting between strings and numeric values, and in formatting
output. However, regardless of locale, the
<period>
character (the decimal-point character of the POSIX locale) is the
decimal-point character recognized in processing
.IR awk
programs (including assignments in command line arguments). This is
essentially the same convention as the one used in the ISO\ C standard. The
difference is that the C language includes the
\fIsetlocale\fR()
function, which permits an application to modify its locale. Because of
this capability, a C application begins executing with its locale set
to the C locale, and only executes in the environment-specified locale
after an explicit call to
\fIsetlocale\fR().
However, adding such an elaborate new feature to the
.IR awk
language was seen as inappropriate for POSIX.1\(hy2008. It is possible to execute
an
.IR awk
program explicitly in any desired locale by setting the environment in
the shell.
.P
The undefined behavior resulting from NULs in extended regular
expressions allows future extensions for the GNU
.IR gawk
program to process binary data.
.P
The behavior in the case of invalid
.IR awk
programs (including lexical, syntactic, and semantic errors) is
undefined because it was considered overly limiting on implementations
to specify. In most cases such errors can be expected to produce a
diagnostic and a non-zero exit status. However, some implementations
may choose to extend the language in ways that make use of certain
invalid constructs. Other invalid constructs might be deemed worthy of
a warning, but otherwise cause some reasonable behavior. Still other
constructs may be very difficult to detect in some implementations.
Also, different implementations might detect a given error during an
initial parsing of the program (before reading any input files) while
others might detect it when executing the program after reading some
input. Implementors should be aware that diagnosing errors as early as
possible and producing useful diagnostics can ease debugging of
applications, and thus make an implementation more usable.
.P
The unspecified behavior from using multi-character
.BR RS
values is to allow possible future extensions based on extended regular
expressions used for record separators. Historical implementations take
the first character of the string and ignore the others.
.P
Unspecified behavior when
.IR split (\c
.IR string ,\c
.IR array ,\c
<null>)
is used is to allow a proposed future extension that would split up a
string into an array of individual characters.
.P
In the context of the
.BR getline
function, equally good arguments for different precedences of the
.BR |
and
.BR <
operators can be made. Historical practice has been that:
.sp
.RS 4
.nf
\fB
getline < "a" "b"
.fi \fR
.P
.RE
.P
is parsed as:
.sp
.RS 4
.nf
\fB
( getline < "a" ) "b"
.fi \fR
.P
.RE
.P
although many would argue that the intent was that the file
.BR ab
should be read. However:
.sp
.RS 4
.nf
\fB
getline < "x" + 1
.fi \fR
.P
.RE
.P
parses as:
.sp
.RS 4
.nf
\fB
getline < ( "x" + 1 )
.fi \fR
.P
.RE
.P
Similar problems occur with the
.BR |
version of
.BR getline ,
particularly in combination with
.BR $ .
For example:
.sp
.RS 4
.nf
\fB
$"echo hi" | getline
.fi \fR
.P
.RE
.P
(This situation is particularly problematic when used in a
.BR print
statement, where the
.BR |getline
part might be a redirection of the
.BR print .)
.P
Since in most cases such constructs are not (or at least should not) be
used (because they have a natural ambiguity for which there is no
conventional parsing), the meaning of these constructs has been made
explicitly unspecified. (The effect is that a conforming application that
runs into the problem must parenthesize to resolve the ambiguity.)
There appeared to be few if any actual uses of such constructs.
.P
Grammars can be written that would cause an error under these
circumstances. Where backwards-compatibility is not a large
consideration, implementors may wish to use such grammars.
.P
Some historical implementations have allowed some built-in functions to
be called without an argument list, the result being a default argument
list chosen in some ``reasonable'' way. Use of
.BR length
as a synonym for
.BR "length($0)"
is the only one of these forms that is thought to be widely known or
widely used; this particular form is documented in various places (for
example, most historical
.IR awk
reference pages, although not in the referenced \fIThe AWK Programming Language\fP) as legitimate practice.
With this exception, default argument lists have always been
undocumented and vaguely defined, and it is not at all clear how (or
if) they should be generalized to user-defined functions. They add no
useful functionality and preclude possible future extensions that might
need to name functions without calling them. Not standardizing them
seems the simplest course. The standard developers considered that
.BR length
merited special treatment, however, since it has been documented in the
past and sees possibly substantial use in historical programs.
Accordingly, this usage has been made legitimate, but Issue\ 5
removed the obsolescent marking for XSI-conforming implementations
and many otherwise conforming applications depend on this feature.
.P
In
.BR sub
and
.BR gsub ,
if
.IR repl
is a string literal (the lexical token
.BR STRING ),
then two consecutive
<backslash>
characters should be used in the string to ensure a single
<backslash>
will precede the
<ampersand>
when the resultant string is passed to the function. (For example,
to specify one literal
<ampersand>
in the replacement string, use
.BR gsub (\c
.BR ERE ,
.BR \(dq\e\e&\(dq ).)
.P
Historically, the only special character in the
.IR repl
argument of
.BR sub
and
.BR gsub
string functions was the
<ampersand>
(\c
.BR '&' )
character and preceding it with the
<backslash>
character was used to turn off its special meaning.
.P
The description in the ISO\ POSIX\(hy2:\|1993 standard introduced behavior such that the
<backslash>
character was another special character and it was unspecified whether
there were any other special characters. This description introduced
several portability problems, some of which are described below, and so
it has been replaced with the more historical description. Some of the
problems include:
.IP " *" 4
Historically, to create the replacement string, a script could use
.BR gsub (\c
.BR ERE ,
.BR \(dq\e\e&\(dq ),
but with the ISO\ POSIX\(hy2:\|1993 standard wording, it was necessary to use
.BR gsub (\c
.BR ERE ,
.BR \(dq\e\e\e\e&\(dq ).
The
<backslash>
characters are doubled here because all string literals are subject to
lexical analysis, which would reduce each pair of
<backslash>
characters to a single
<backslash>
before being passed to
.BR gsub .
.IP " *" 4
Since it was unspecified what the special characters were, for portable
scripts to guarantee that characters are printed literally, each
character had to be preceded with a
<backslash>.
(For example, a portable script had to use
.BR gsub (\c
.BR ERE ,
.BR \(dq\e\eh\e\ei\(dq )
to produce a replacement string of
.BR \(dqhi\(dq .)
.P
The description for comparisons in the ISO\ POSIX\(hy2:\|1993 standard did not properly describe
historical practice because of the way numeric strings are compared as
numbers. The current rules cause the following code:
.sp
.RS 4
.nf
\fB
if (0 == "000")
print "strange, but true"
else
print "not true"
.fi \fR
.P
.RE
.P
to do a numeric comparison, causing the
.BR if
to succeed. It should be intuitively obvious that this is incorrect
behavior, and indeed, no historical implementation of
.IR awk
actually behaves this way.
.P
To fix this problem, the definition of
.IR "numeric string"
was enhanced to include only those values obtained from specific
circumstances (mostly external sources) where it is not possible to
determine unambiguously whether the value is intended to be a string or
a numeric.
.P
Variables that are assigned to a numeric string shall also be treated
as a numeric string. (For example, the notion of a numeric string can
be propagated across assignments.) In comparisons, all variables having
the uninitialized value are to be treated as a numeric operand
evaluating to the numeric value zero.
.P
Uninitialized variables include all types of variables including
scalars, array elements, and fields. The definition of an uninitialized
value in
.IR "Variables and Special Variables"
is necessary to describe the value placed on uninitialized variables
and on fields that are valid (for example,
.BR <
.BR $NF )
but have no characters in them and to describe how these variables are
to be used in comparisons. A valid field, such as
.BR $1 ,
that has no characters in it can be obtained from an input line of
.BR \(dq\et\et\(dq
when
.BR FS= \c
.BR '\et' .
Historically, the comparison (\c
.BR $1< 10)
was done numerically after evaluating
.BR $1
to the value zero.
.P
The phrase ``.\|.\|. also shall have the numeric value of the numeric
string'' was removed from several sections of the ISO\ POSIX\(hy2:\|1993 standard because is
specifies an unnecessary implementation detail. It is not necessary for
POSIX.1\(hy2008 to specify that these objects be assigned two different values.
It is only necessary to specify that these objects may evaluate to two
different values depending on context.
.P
Historical implementations of
.IR awk
did not parse hexadecimal integer or floating constants like
.BR \(dq0xa\(dq
and
.BR \(dq0xap0\(dq .
Due to an oversight, the 2001 through 2004 editions of this standard
required support for hexadecimal floating constants. This was due to
the reference to
\fIatof\fR().
This version of the standard allows but does not require implementations
to use
\fIatof\fR()
and includes a description of how floating-point numbers are recognized
as an alternative to match historic behavior. The intent of this change
is to allow implementations to recognize floating-point constants
according to either the ISO/IEC\ 9899:\|1990 standard or ISO/IEC\ 9899:\|1999 standard, and to allow (but not require)
implementations to recognize hexadecimal integer constants.
.P
Historical implementations of
.IR awk
did not support floating-point infinities and NaNs in
.IR "numeric strings" ;
e.g.,
.BR \(dq\(miINF\(dq
and
.BR \(dqNaN\(dq .
However, implementations that use the
\fIatof\fR()
or
\fIstrtod\fR()
functions to do the conversion picked up support for these values if they
used a ISO/IEC\ 9899:\|1999 standard version of the function instead of a ISO/IEC\ 9899:\|1990 standard version. Due to
an oversight, the 2001 through 2004 editions of this standard did not
allow support for infinities and NaNs, but in this revision support is
allowed (but not required). This is a silent change to the behavior of
.IR awk
programs; for example, in the POSIX locale the expression:
.sp
.RS 4
.nf
\fB
("-INF" + 0 < 0)
.fi \fR
.P
.RE
.P
formerly had the value 0 because
.BR \(dq\(miINF\(dq
converted to 0, but now it may have the value 0 or 1.
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
.IR "Section 1.3" ", " "Grammar Conventions",
.IR "\fIgrep\fR\^",
.IR "\fIlex\fR\^",
.IR "\fIsed\fR\^"
.P
The Base Definitions volume of POSIX.1\(hy2008,
.IR "Chapter 5" ", " "File Format Notation",
.IR "Section 6.1" ", " "Portable Character Set",
.IR "Chapter 8" ", " "Environment Variables",
.IR "Chapter 9" ", " "Regular Expressions",
.IR "Section 12.2" ", " "Utility Syntax Guidelines"
.P
The System Interfaces volume of POSIX.1\(hy2008,
.IR "\fIatof\fR\^(\|)",
.IR "\fIexec\fR\^",
.IR "\fIisspace\fR\^(\|)",
.IR "\fIpopen\fR\^(\|)",
.IR "\fIsetlocale\fR\^(\|)",
.IR "\fIstrtod\fR\^(\|)"
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 7, Copyright (C) 2013 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group.
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.unix.org/online.html .
Any typographical or formatting errors that appear
in this page are most likely
to have been introduced during the conversion of the source files to
man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .
|