1531 lines
216 KiB
HTML
1531 lines
216 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="renderer" content="webkit">
|
||
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
|
||
<meta name="description" content="awk:文本和数据进行处理的编程语言 - 最专业的Linux命令大全,内容包含Linux命令手册、详解、学习,值得收藏的Linux命令速查手册。">
|
||
<meta name="keywords" content="Linux,Command,命令大全,Linux命令手册,awk,文本和数据进行处理的编程语言">
|
||
<title>awk 命令,Linux awk 命令详解:文本和数据进行处理的编程语言 - Linux 命令搜索引擎</title>
|
||
<link rel="shortcut icon" href="../img/favicon.ico">
|
||
<link rel="stylesheet" type="text/css" href="../css/index.css?v=1752678601389">
|
||
<script src="../js/dark-mode.min.js"></script>
|
||
<script type="module" src="../js/github-corners.js"></script>
|
||
</head>
|
||
<body>
|
||
<dark-mode permanent dark="Dark" light="Light" style="position: fixed;left: 10px;top: 8px; z-index: 999;"></dark-mode>
|
||
<github-corners target="__blank" z-index="999" position="fixed" href="https://github.com/jaywcjlove/linux-command"></github-corners>
|
||
<div class="header header_list">
|
||
<div class="header_inner">
|
||
<div class="logo">
|
||
<a href="/">
|
||
<svg width="183px" height="48px" viewBox="0 0 183 48" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <!-- kenny wang <wowohoo@qq.com> https://github.com/jaywcjlove --> <title>logo</title> <desc>Linux Command Logo. https://github.com/jaywcjlove</desc><g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"> <g transform="translate(-576.000000, -261.000000)" fill="currentColor"> <g id="logo" transform="translate(576.000000, 261.000000)"> <path d="M20.4917792,0.000336524232 C20.1886146,-0.000100520225 19.8723998,0.0124863601 19.542641,0.0387964365 C11.2083649,0.710004944 13.4186692,9.51473754 13.2949769,12.4631618 C13.1427718,14.6193207 12.7056049,16.3185233 11.2226825,18.426428 C9.48047033,20.4982285 7.02763261,23.8514609 5.86566251,27.3420607 C5.3172635,28.9894735 5.05630425,30.6686289 5.29730805,32.2578406 C5.22181299,32.325447 5.14994103,32.3962788 5.08160039,32.469239 C4.57008792,33.0153217 4.19213625,33.6769851 3.77100458,34.1224689 C3.37755968,34.5149436 2.81698897,34.6642292 2.20097918,34.8852382 C1.58472464,35.1063477 0.908127379,35.4323261 0.497432332,36.2204265 C0.497432332,36.2204265 0.497432332,36.2207368 0.49712203,36.2207368 C0.496684986,36.222048 0.495373852,36.2233591 0.494936808,36.2246702 L0.494936808,36.2246702 C0.123151829,36.919077 0.233680372,37.718746 0.347224522,38.4596325 C0.460773043,39.2004972 0.575720105,39.9005856 0.423139144,40.3755699 C-0.0645762474,41.7086473 -0.126850712,42.6307542 0.216491414,43.2996202 C0.560563403,43.9698759 1.2680029,44.2657113 2.06756262,44.432706 C3.66668644,44.7666954 5.83232039,44.6837094 7.53908826,45.5883128 L7.68579097,45.3115063 L7.54060917,45.5891344 C9.36778716,46.5444437 11.2201913,46.8837519 12.698079,46.5461831 C13.7695766,46.3014513 14.6393781,45.6634363 15.0860725,44.6816946 C16.2420201,44.6760567 17.5104717,44.1864315 19.5423657,44.0745568 C20.9208039,43.9635213 22.6427241,44.564051 24.6233484,44.4540775 C24.6751207,44.6689591 24.750052,44.8756375 24.8526175,45.0720452 C24.8539287,45.0742305 24.8548028,45.0759786 24.8556769,45.0780328 C25.6234722,46.6133699 27.0500902,47.3156173 28.570428,47.1956355 C30.0927718,47.075492 31.711331,46.1778551 33.0196848,44.6208231 L32.7798304,44.4192582 L33.0215204,44.618616 C34.2685787,43.1065034 36.3384911,42.4794975 37.7110118,41.6516741 C38.3972721,41.2377581 38.9541192,40.7194802 38.9971462,39.966706 C39.0401514,39.2142991 38.5984218,38.3711223 37.5826868,37.2436088 L37.5823765,37.2432985 C37.5820662,37.2428614 37.5815024,37.2424244 37.5810654,37.2415503 C37.2473295,36.8644028 37.0881185,36.1654026 36.91702,35.4210285 C36.7460395,34.6771089 36.5551865,33.8746254 35.9444344,33.3545687 L35.9444344,33.3545687 C35.9431233,33.3532575 35.9418121,33.3523835 35.940938,33.3515094 L35.940938,33.3515094 C35.6978714,33.1397045 35.4452772,32.9959518 35.1909042,32.8993694 C36.0400422,30.3810493 35.7068789,27.8734149 34.8497254,25.6073831 C33.7977638,22.8263338 31.9611937,20.4030752 30.5585738,18.7453569 C28.9885703,16.7650385 27.4529665,14.8848863 27.4832187,12.1079627 C27.5299825,7.86977213 27.9494053,0.0101787654 20.4914689,0 L20.4917792,0.000336524232 Z M21.5014393,6.5296802 C21.9246338,6.5296802 22.2860696,6.65372652 22.6573476,6.92320377 C23.0345213,7.1969553 23.3062494,7.53965497 23.5252523,8.01821865 C23.7401952,8.48461502 23.843613,8.94086321 23.8537568,9.48229573 C23.8537568,9.49649968 23.8537568,9.50864951 23.8576902,9.52285346 C23.8616236,10.0784681 23.7664397,10.5509481 23.5555483,11.0335676 C23.4352081,11.3089536 23.2970146,11.540089 23.1316064,11.7401941 C23.0754506,11.7132285 23.0171357,11.6873118 22.9566488,11.6624396 L22.9566488,11.6624396 C22.5373789,11.4828667 22.2157491,11.3685884 21.9476879,11.2755722 C22.044856,11.158772 22.1257661,11.0202464 22.1969169,10.8470719 C22.3043949,10.5854877 22.3571155,10.3299784 22.367255,10.025813 C22.367255,10.0136631 22.3711884,10.00348 22.3711884,9.98931975 C22.3772633,9.69732161 22.3387597,9.44789596 22.2535753,9.19239103 C22.1643483,8.92472316 22.0507955,8.73208707 21.886541,8.57188842 C21.722291,8.41168541 21.5580366,8.33869024 21.361336,8.33260221 C21.3521143,8.33216517 21.3429364,8.33216517 21.3338459,8.33216517 C21.1488275,8.33260221 20.9881699,8.39616596 20.8219532,8.53532529 C20.6475593,8.6813331 20.5177834,8.86788115 20.4103054,9.12744186 C20.3028318,9.38699819 20.2501067,9.64452664 20.239976,9.95073309 C20.2377908,9.96288293 20.2377908,9.97306607 20.2377908,9.9852159 C20.2342945,10.1535261 20.2449584,10.307711 20.2709188,10.4574381 C19.8923378,10.2687966 19.535574,10.1401875 19.2007499,10.0604924 C19.181651,9.91570835 19.1706375,9.76629596 19.1670975,9.61007442 L19.1670975,9.56750629 C19.1610225,9.01391519 19.2522643,8.53941165 19.4651836,8.05679657 C19.6781029,7.57417712 19.9417194,7.2274173 20.3128051,6.94555422 C20.6838952,6.66369114 21.0488973,6.53391079 21.4808239,6.52985938 L21.5010154,6.52985938 L21.5014393,6.5296802 Z M15.7091362,6.98827969 C15.9898804,6.98871673 16.2410892,7.0827381 16.5009296,7.29023807 C16.7828014,7.51531597 16.9957164,7.80326271 17.1741616,8.20882685 C17.3526112,8.614391 17.4479175,9.01995077 17.4742757,9.49851445 L17.4742757,9.50244785 C17.4870374,9.70344023 17.485464,9.89263677 17.469643,10.0758677 C17.4141165,10.091645 17.3597045,10.1087334 17.3064069,10.1270762 C17.0036968,10.2312719 16.7369205,10.373272 16.5044085,10.5268101 C16.5271785,10.366205 16.5305437,10.2032486 16.5131057,10.0215605 C16.5109204,10.0114211 16.5109204,10.0032921 16.5109204,9.99319633 C16.4865771,9.75189098 16.435893,9.54911109 16.3507218,9.34429895 C16.2594713,9.131384 16.1580813,8.98132478 16.0242452,8.86573527 C15.9029566,8.76098882 15.7883592,8.71289208 15.6622806,8.71384046 C15.6492567,8.71384046 15.636058,8.71427751 15.6227281,8.71558864 C15.4807804,8.72773848 15.3631674,8.79669098 15.251638,8.93255936 C15.1401086,9.068419 15.0671091,9.23672919 15.0143884,9.4597792 C14.9616634,9.68284232 14.9474682,9.90184967 14.9697706,10.1532945 C14.9697706,10.1634339 14.9719558,10.1715629 14.9719558,10.1816586 C14.9962992,10.4249963 15.0449553,10.6277718 15.1321501,10.8325883 C15.2213771,11.0434797 15.3247905,11.1935346 15.4586266,11.3091153 C15.481047,11.3284764 15.5032139,11.3458708 15.525289,11.361399 C15.3860816,11.468536 15.3196072,11.5179308 15.2055298,11.6017559 C15.1323642,11.6554425 15.0452263,11.7193646 14.94395,11.7940162 C14.7230765,11.5871106 14.5507673,11.3270648 14.4001137,10.984671 C14.2216641,10.5791112 14.1263578,10.1735471 14.0979674,9.69498342 L14.0979674,9.69105002 C14.0716136,9.21249508 14.1182463,8.8008429 14.2500545,8.37500864 C14.3818627,7.94916563 14.5582845,7.6409444 14.813785,7.38745861 C15.06929,7.13399031 15.3268184,7.00623784 15.6370763,6.99001912 C15.6613322,6.98870799 15.6853347,6.98827095 15.709123,6.98827095 L15.7091362,6.98827969 Z M18.3434478,10.3083666 C18.9959552,10.3057443 19.7811624,10.5197693 20.731756,11.1309629 C21.3156605,11.5106715 21.7700031,11.5424053 22.8162176,11.9904939 L22.8175288,11.9909309 L22.8188399,11.991368 C23.322219,12.1979021 23.6176741,12.4671083 23.7620255,12.7505841 C23.906377,13.034073 23.9099039,13.3414857 23.7890786,13.6648724 C23.5474411,14.3116676 22.7761407,14.9923468 21.6937826,15.3303395 L21.6929085,15.3307766 L21.6920344,15.3312136 C21.1642857,15.5026793 20.7040867,15.8813302 20.1615484,16.1915706 C19.6190277,16.5017979 19.00512,16.7520102 18.1703739,16.7037212 C18.1703739,16.7037168 18.1703739,16.7037212 18.1703739,16.7037212 C17.4597396,16.6622893 17.0349499,16.4210539 16.650849,16.1123607 C16.2667437,15.8036631 15.9320245,15.4158517 15.4418966,15.1280491 L15.4405854,15.1271751 L15.4392743,15.126301 C14.6497885,14.6800742 14.2187709,14.163964 14.0834969,13.7165354 C13.9482185,13.2691068 14.075193,12.8871911 14.4668766,12.5936327 C14.9072645,12.2635986 15.2131825,12.0390976 15.4169632,11.8895453 C15.6194022,11.7409983 15.703625,11.685297 15.7680935,11.623394 C15.7684038,11.622957 15.7685305,11.622957 15.7689675,11.6225199 L15.7692735,11.6220829 C16.1001729,11.3088706 16.6276463,10.738226 17.4227569,10.4645313 C17.696421,10.3703264 18.001784,10.3095466 18.3435746,10.3081961 L18.3434478,10.3083666 Z M22.9062619,13.0069369 C22.6246829,13.0237631 22.3297828,13.1687002 21.9864013,13.3604054 C21.6430199,13.5521105 21.256878,13.7973406 20.8384079,14.0388338 C20.0014721,14.5218204 19.038047,14.9837283 18.0805918,14.9837283 C17.1216551,14.9837283 16.3555118,14.5408493 15.7808726,14.0854752 C15.4935509,13.8577838 15.2564149,13.6284404 15.0669561,13.4510265 C14.9722311,13.3623109 14.8900799,13.2867065 14.8134179,13.2276487 C14.7367734,13.1685909 14.6743853,13.1100357 14.5461303,13.1100357 L14.5400116,13.3795435 C14.5308337,13.4697233 14.5213935,13.4669437 14.5251084,13.4693999 C14.5310959,13.4733333 14.5608587,13.4835601 14.5955731,13.5103509 C14.6519344,13.5537495 14.7301609,13.6245375 14.8230067,13.7114832 C15.0086981,13.8853788 15.2554403,14.124464 15.5592255,14.3652012 C16.166796,14.8466668 17.0117515,15.3406363 18.0806093,15.3406363 C19.1509618,15.3406363 20.1636375,14.8403471 21.016875,14.3479598 C21.4434872,14.1017639 21.8309534,13.8560094 22.1604324,13.6720705 C22.4898897,13.4881185 22.7670895,13.3727781 22.9275766,13.3632068 L22.9062925,13.00695 L22.9062619,13.0069369 Z M23.8576858,14.2878093 C24.5685911,17.0901733 26.2220701,21.1378562 27.2848005,23.113201 C27.8496849,24.1612205 28.9735097,26.3878921 29.4590574,29.0706371 C29.7667148,29.0611969 30.1054855,29.1059065 30.4682673,29.1987042 C31.7377591,25.907589 29.3917525,22.3626165 28.3189832,21.375368 C27.8858722,20.9551061 27.8650557,20.7668098 28.0797669,20.7757081 C29.2436775,21.8049915 30.7716374,23.8752142 31.3275492,26.211938 C31.581035,27.2774436 31.6350799,28.3975886 31.3630809,29.5030663 C31.4961478,29.5583437 31.6312951,29.6184504 31.7682604,29.683286 C33.806037,30.6755779 34.5596721,31.5384567 34.1973186,32.716121 C34.0779748,32.7117506 33.960576,32.7126247 33.8460659,32.7152469 C33.8354021,32.7155572 33.8247819,32.715684 33.814118,32.716121 C34.1092104,31.7826203 33.4554837,31.0941179 31.7135031,30.305952 C29.9067264,29.5110511 28.4669839,29.5901387 28.2236507,31.2022427 C28.2080919,31.2867016 28.1955487,31.3727644 28.1860212,31.460029 C28.05104,31.506902 27.9153158,31.5667509 27.7792552,31.6412014 C26.9308557,32.1054213 26.4677896,32.9477371 26.2102393,33.9808403 C25.9529032,35.0131131 25.8791213,36.2610673 25.8081016,37.6639101 L25.8081016,37.6647842 C25.7647031,38.3701826 25.4746935,39.3242507 25.1805582,40.3347805 C22.2207839,42.446033 18.1128938,43.3607409 14.6251435,40.9805006 C14.3887768,40.6065916 14.1176344,40.2361221 13.8385378,39.8707355 C13.6603154,39.6373843 13.4772332,39.4054841 13.2952172,39.1768225 C13.6532528,39.1768225 13.9575843,39.1184771 14.2035442,39.0069433 C14.5094142,38.8682516 14.724322,38.6454901 14.8306462,38.3594969 C15.0432858,37.7875325 14.8297721,36.9806654 14.1489836,36.0592664 C13.4682956,35.137933 12.3154554,34.0981212 10.6214273,33.0592009 C10.6214273,33.0592009 10.621117,33.0592009 10.621117,33.0588906 C9.37658486,32.2846183 8.68057408,31.3355981 8.3547924,30.3051959 C8.02898013,29.2747018 8.07466439,28.1604089 8.32577265,27.0602631 C8.80774091,24.9485692 10.0461107,22.8947793 10.8362565,21.605909 C11.0489879,21.4493815 10.9122629,21.8964561 10.0363515,23.5230089 C9.25134103,25.0101357 7.78376323,28.4420972 9.79314068,31.1210705 C9.84684033,29.2145776 10.3021401,27.2701931 11.0664741,25.45145 C12.1797968,22.9278068 14.5084483,18.5514927 14.6933968,15.0635108 C14.7887555,15.1327561 15.1164383,15.3538525 15.2624505,15.4368866 C15.2627608,15.4373237 15.2628875,15.4373237 15.2633246,15.4373237 C15.6905181,15.6888908 16.0113611,16.0566244 16.426903,16.3905832 C16.8432709,16.7252019 17.3634237,17.0142238 18.149269,17.0600129 L18.149269,17.0600129 C19.066516,17.113092 19.7655905,16.8289912 20.3383504,16.5014745 C20.9102449,16.1744603 21.3671137,15.8125482 21.7999582,15.6712779 C21.8003952,15.6708409 21.8012693,15.6708409 21.8017063,15.6708409 C22.7163355,15.3848215 23.44356,14.8785842 23.857393,14.2881327 L23.8576858,14.2878093 Z M29.6669682,30.3312962 C30.0374289,30.329548 30.4861993,30.4530917 30.9774372,30.6728508 C32.2995623,31.2832227 32.713238,31.8063999 32.3522873,32.5749338 C32.0481219,33.1548918 30.7462669,34.0775407 29.8540319,33.8342075 C28.9435503,33.5989814 28.4994564,32.2869871 28.6474877,31.2953987 C28.7267763,30.6220356 29.1076387,30.3339097 29.6669682,30.3312962 L29.6669682,30.3312962 Z M28.1710786,32.1433306 C28.2458176,33.3559191 28.8457529,34.5926195 29.9067526,34.8602742 C31.0678224,35.1661922 32.7418469,34.16998 33.4486352,33.3574182 C33.5895689,33.3517803 33.7273516,33.3448313 33.8611658,33.3416409 C34.4811527,33.3266503 35.001157,33.3620509 35.5324588,33.8267253 L35.534207,33.8284735 L35.5359552,33.8302216 C35.9442465,34.1761336 36.1383204,34.8298385 36.3066481,35.5621545 C36.4749627,36.294475 36.6095636,37.0920505 37.11435,37.6602345 L37.115224,37.6611086 L37.1156611,37.6619827 C38.0861227,38.7388952 38.3980544,39.4668146 38.3715083,39.931406 C38.344936,40.3960279 38.008556,40.7409347 37.3873891,41.1155867 C36.1455928,41.864576 33.9453056,42.5158727 32.5399192,44.2181871 C31.3196037,45.6704378 29.8315635,46.4680352 28.5210638,46.5714617 C27.2105642,46.6748883 26.0799127,46.1309646 25.4133237,44.793071 L25.4115756,44.7900117 L25.4098274,44.7869524 C24.9962959,44.0002986 25.1685614,42.758852 25.5169207,41.4493706 C25.8652845,40.1398937 26.3657922,38.7953264 26.4327911,37.7024923 L26.4327911,37.7007442 L26.4327911,37.698996 C26.5036666,36.2987755 26.5820418,35.0759688 26.8172504,34.1324728 C27.0524633,33.1889769 27.4228236,32.5499829 28.0790545,32.1909116 C28.109604,32.1742165 28.139948,32.1584392 28.1701128,32.1435097 L28.1710786,32.1433306 Z M6.86074718,32.2348346 C6.95869322,32.2351449 7.06234705,32.2434006 7.17277507,32.2601394 C7.91559769,32.3726653 8.5633675,32.8920358 9.18746261,33.7385341 C9.8115621,34.5850455 10.3922762,35.7405823 10.9892352,37.0223812 L10.9901093,37.0241294 L10.9905463,37.0258776 C11.4702594,38.0271115 12.4825418,39.128044 13.3403902,40.2511564 C14.1982341,41.3742689 14.8621527,42.5016163 14.7756966,43.3644732 L14.7753863,43.3684066 L14.775076,43.37234 C14.6632363,44.843978 13.8330966,45.6451198 12.5587449,45.9361914 C11.2847734,46.2271625 9.55771794,45.9379396 7.83269472,45.035587 L7.83094655,45.035587 C5.9224477,44.0247687 3.65205856,44.1251622 2.19566909,43.8209924 C1.46727769,43.6688572 0.99200058,43.44013 0.773862951,43.0151698 C0.55571221,42.5902315 0.550839164,41.8488511 1.01440785,40.5847437 L1.01659307,40.5791059 L1.01834125,40.573468 C1.24759292,39.8663213 1.07793226,39.0925822 0.96663015,38.3663367 C0.855328038,37.6400911 0.800894151,36.9795116 1.04888192,36.5190765 L1.05019305,36.5168913 L1.05150418,36.514706 C1.36900824,35.9026821 1.83472718,35.683902 2.41273159,35.4765375 C2.990736,35.2691556 3.67612223,35.1062953 4.21734934,34.5638925 L4.21997161,34.5612703 L4.22259387,34.558648 C4.72293985,34.0308163 5.09877623,33.3691834 5.53863963,32.8995879 C5.90977341,32.5033678 6.28118253,32.2407652 6.84099715,32.2368755 L6.86053303,32.2368755 L6.86074718,32.2348346 Z"></path> <path d="M58.5014648,40 L58.5014648,6.7578125 L65.4194336,6.7578125 L65.4194336,35.2832031 L80.4008789,35.2832031 L80.4008789,40 L58.5014648,40 L58.5014648,40 L58.5014648,40 Z M86.2524414,40 L86.2524414,15.3378906 L92.9008789,15.3378906 L92.9008789,40 L86.2524414,40 L86.2524414,40 L86.2524414,40 Z M86.2524414,11.1826172 L86.2524414,5.63476562 L92.9008789,5.63476562 L92.9008789,11.1826172 L86.2524414,11.1826172 L86.2524414,11.1826172 L86.2524414,11.1826172 Z M100.211426,40 L100.211426,15.3378906 L106.859863,15.3378906 L106.859863,19.9873047 C109.031098,16.513329 111.8312,14.7763672 115.260254,14.7763672 C117.461437,14.7763672 119.198399,15.4726493 120.471191,16.8652344 C121.743984,18.2578195 122.380371,20.1594932 122.380371,22.5703125 L122.380371,40 L115.731934,40 L115.731934,24.2099609 C115.731934,21.4098167 114.803557,20.0097656 112.946777,20.0097656 C110.835439,20.0097656 108.806488,21.4996596 106.859863,24.4794922 L106.859863,40 L100.211426,40 L100.211426,40 L100.211426,40 Z M145.672363,40 L145.672363,35.3505859 C143.516102,38.8245616 140.716,40.5615234 137.271973,40.5615234 C135.07079,40.5615234 133.333828,39.8652413 132.061035,38.4726562 C130.788242,37.0800712 130.151855,35.1783975 130.151855,32.7675781 L130.151855,15.3378906 L136.800293,15.3378906 L136.800293,31.1279297 C136.800293,33.9280739 137.736156,35.328125 139.60791,35.328125 C141.704275,35.328125 143.725739,33.838231 145.672363,30.8583984 L145.672363,15.3378906 L152.320801,15.3378906 L152.320801,40 L145.672363,40 L145.672363,40 L145.672363,40 Z M158.003418,40 L166.471191,27.6240234 L158.362793,15.3378906 L165.932129,15.3378906 L171.23291,23.1542969 L176.309082,15.3378906 L181.699707,15.3378906 L173.973145,27.4443359 L182.26123,40 L174.691895,40 L169.166504,31.8466797 L163.506348,40 L158.003418,40 L158.003418,40 L158.003418,40 Z"></path> </g> </g> </g> </svg>
|
||
</a>
|
||
</div>
|
||
<div class="search">
|
||
<ul class="search-list" id="result">
|
||
<!-- <li><a href="#"><strong>find</strong> - 指定目录下查找文件。</a></li> -->
|
||
</ul>
|
||
<input type="text" class="query" id="query" autocomplete="off" autofocus="autofocus" placeholder="Linux 命令搜索"/>
|
||
<div class="enter-input">
|
||
<input type="hidden" id="current_path" value="/c/awk.html">
|
||
<button id="search_btn">搜索</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<script type="text/javascript" src="../js/copy-to-clipboard.js"></script>
|
||
|
||
<div class="markdown-body">
|
||
|
||
<span class="edit_btn">
|
||
<a target="_blank" href="https://github.com/jaywcjlove/linux-command/edit/master/command/awk.md">纠正错误</a>
|
||
<span class="split"></span>
|
||
<a target="_blank" href="https://github.com/jaywcjlove/linux-command/edit/master/command/awk.md">添加实例</a>
|
||
</span>
|
||
|
||
|
||
<dark-mode style="position: fixed; top: 8px; left: 10px; z-index: 999;;display: none;" dark="Dark" light="Light"></dark-mode><script>const t=document;const e="_dark_mode_theme_";const s="permanent";const o="colorschemechange";const i="permanentcolorscheme";const h="light";const r="dark";const n=(t,e,s=e)=>{Object.defineProperty(t,s,{enumerable:true,get(){const t=this.getAttribute(e);return t===null?"":t},set(t){this.setAttribute(e,t)}})};const c=(t,e,s=e)=>{Object.defineProperty(t,s,{enumerable:true,get(){return this.hasAttribute(e)},set(t){if(t){this.setAttribute(e,"")}else{this.removeAttribute(e)}}})};class a extends HTMLElement{static get observedAttributes(){return["mode",h,r,s]}LOCAL_NANE=e;constructor(){super();this.t()}connectedCallback(){n(this,"mode");n(this,r);n(this,h);c(this,s);const a=localStorage.getItem(e);if(a&&[h,r].includes(a)){this.mode=a;this.permanent=true}if(this.permanent&&!a){localStorage.setItem(e,this.mode)}const l=[h,r].includes(a);if(this.permanent&&a){this.o()}else{if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches){this.mode=r;this.o()}if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: light)").matches){this.mode=h;this.o()}}if(!this.permanent&&!l){window.matchMedia("(prefers-color-scheme: light)").onchange=t=>{this.mode=t.matches?h:r;this.o()};window.matchMedia("(prefers-color-scheme: dark)").onchange=t=>{this.mode=t.matches?r:h;this.o()}}const d=new MutationObserver(((s,h)=>{this.mode=t.documentElement.dataset.colorMode;if(this.permanent&&l){localStorage.setItem(e,this.mode);this.i(i,{permanent:this.permanent})}this.h();this.i(o,{colorScheme:this.mode})}));d.observe(t.documentElement,{attributes:true});this.i(o,{colorScheme:this.mode});this.h()}attributeChangedCallback(t,s,o){if(t==="mode"&&s!==o&&[h,r].includes(o)){const t=localStorage.getItem(e);if(this.mode===t){this.mode=o;this.h();this.o()}else if(this.mode&&this.mode!==t){this.h();this.o()}}else if((t===h||t===r)&&s!==o){this.h()}if(t==="permanent"&&typeof this.permanent==="boolean"){this.permanent?localStorage.setItem(e,this.mode):localStorage.removeItem(e)}}o(){t.documentElement.setAttribute("data-color-mode",this.mode)}h(){this.icon.textContent=this.mode===h?"🌒":"🌞";this.text.textContent=this.mode===h?this.getAttribute(r):this.getAttribute(h)}t(){var s=this.attachShadow({mode:"open"});this.label=t.createElement("span");this.label.setAttribute("class","wrapper");this.label.onclick=()=>{this.mode=this.mode===h?r:h;if(this.permanent){localStorage.setItem(e,this.mode)}this.o();this.h()};s.appendChild(this.label);this.icon=t.createElement("span");this.label.appendChild(this.icon);this.text=t.createElement("span");this.label.appendChild(this.text);const o=`
|
||
[data-color-mode*='dark'], [data-color-mode*='dark'] body {
|
||
color-scheme: dark;
|
||
--color-theme-bg: #0d1117;
|
||
--color-theme-text: #c9d1d9;
|
||
background-color: var(--color-theme-bg);
|
||
color: var(--color-theme-text);
|
||
}
|
||
|
||
[data-color-mode*='light'], [data-color-mode*='light'] body {
|
||
color-scheme: light;
|
||
--color-theme-bg: #fff;
|
||
--color-theme-text: #24292f;
|
||
background-color: var(--color-theme-bg);
|
||
color: var(--color-theme-text);
|
||
}`;const i="_dark_mode_style_";const n=t.getElementById(i);if(!n){var c=t.createElement("style");c.id=i;c.textContent=o;t.head.appendChild(c)}var a=t.createElement("style");a.textContent=`
|
||
.wrapper { cursor: pointer; user-select: none; position: relative; }
|
||
.wrapper > span + span { margin-left: .4rem; }
|
||
`;s.appendChild(a)}i(t,e){this.dispatchEvent(new CustomEvent(t,{bubbles:true,composed:true,detail:e}))}}customElements.define("dark-mode",a);</script><script>const __TEMPLATE__ = document.createElement('template');
|
||
__TEMPLATE__.innerHTML = `
|
||
<style>
|
||
|
||
markdown-style h1:hover a.anchor .octicon-link:before,
|
||
markdown-style h2:hover a.anchor .octicon-link:before,
|
||
markdown-style h3:hover a.anchor .octicon-link:before,
|
||
markdown-style h4:hover a.anchor .octicon-link:before,
|
||
markdown-style h5:hover a.anchor .octicon-link:before,
|
||
markdown-style h6:hover a.anchor .octicon-link:before {
|
||
width: 16px;
|
||
height: 16px;
|
||
content: ' ';
|
||
display: inline-block;
|
||
background-color: currentColor;
|
||
-webkit-mask-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' version='1.1' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg>");
|
||
mask-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' version='1.1' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg>");
|
||
}
|
||
[data-color-mode*='light'], [data-color-mode*='light'] body, markdown-style[theme*='light'] { --color-prettylights-syntax-comment: #6e7781; --color-prettylights-syntax-constant: #0550ae; --color-prettylights-syntax-entity: #8250df; --color-prettylights-syntax-storage-modifier-import: #24292f; --color-prettylights-syntax-entity-tag: #116329; --color-prettylights-syntax-keyword: #cf222e; --color-prettylights-syntax-string: #0a3069; --color-prettylights-syntax-variable: #953800; --color-prettylights-syntax-brackethighlighter-unmatched: #82071e; --color-prettylights-syntax-invalid-illegal-text: #f6f8fa; --color-prettylights-syntax-invalid-illegal-bg: #82071e; --color-prettylights-syntax-carriage-return-text: #f6f8fa; --color-prettylights-syntax-carriage-return-bg: #cf222e; --color-prettylights-syntax-string-regexp: #116329; --color-prettylights-syntax-markup-list: #3b2300; --color-prettylights-syntax-markup-heading: #0550ae; --color-prettylights-syntax-markup-italic: #24292f; --color-prettylights-syntax-markup-bold: #24292f; --color-prettylights-syntax-markup-deleted-text: #82071e; --color-prettylights-syntax-markup-deleted-bg: #FFEBE9; --color-prettylights-syntax-markup-inserted-text: #116329; --color-prettylights-syntax-markup-inserted-bg: #dafbe1; --color-prettylights-syntax-markup-changed-text: #953800; --color-prettylights-syntax-markup-changed-bg: #ffd8b5; --color-prettylights-syntax-markup-ignored-text: #eaeef2; --color-prettylights-syntax-markup-ignored-bg: #0550ae; --color-prettylights-syntax-meta-diff-range: #8250df; --color-prettylights-syntax-brackethighlighter-angle: #57606a; --color-prettylights-syntax-sublimelinter-gutter-mark: #8c959f; --color-prettylights-syntax-constant-other-reference-link: #0a3069; --color-fg-default: #24292f; --color-fg-muted: #57606a; --color-fg-subtle: #6e7781; --color-canvas-default: #ffffff; --color-canvas-subtle: #f6f8fa; --color-border-default: #d0d7de; --color-border-muted: hsla(210,18%,87%,1); --color-neutral-muted: rgba(175,184,193,0.2); --color-accent-fg: #0969da; --color-accent-emphasis: #0969da; --color-attention-subtle: #fff8c5; --color-danger-fg: #cf222e; } [data-color-mode*='dark'], [data-color-mode*='dark'] body, markdown-style[theme*='dark'] { --color-prettylights-syntax-comment: #8b949e; --color-prettylights-syntax-constant: #79c0ff; --color-prettylights-syntax-entity: #d2a8ff; --color-prettylights-syntax-storage-modifier-import: #c9d1d9; --color-prettylights-syntax-entity-tag: #7ee787; --color-prettylights-syntax-keyword: #ff7b72; --color-prettylights-syntax-string: #a5d6ff; --color-prettylights-syntax-variable: #ffa657; --color-prettylights-syntax-brackethighlighter-unmatched: #f85149; --color-prettylights-syntax-invalid-illegal-text: #f0f6fc; --color-prettylights-syntax-invalid-illegal-bg: #8e1519; --color-prettylights-syntax-carriage-return-text: #f0f6fc; --color-prettylights-syntax-carriage-return-bg: #b62324; --color-prettylights-syntax-string-regexp: #7ee787; --color-prettylights-syntax-markup-list: #f2cc60; --color-prettylights-syntax-markup-heading: #1f6feb; --color-prettylights-syntax-markup-italic: #c9d1d9; --color-prettylights-syntax-markup-bold: #c9d1d9; --color-prettylights-syntax-markup-deleted-text: #ffdcd7; --color-prettylights-syntax-markup-deleted-bg: #67060c; --color-prettylights-syntax-markup-inserted-text: #aff5b4; --color-prettylights-syntax-markup-inserted-bg: #033a16; --color-prettylights-syntax-markup-changed-text: #ffdfb6; --color-prettylights-syntax-markup-changed-bg: #5a1e02; --color-prettylights-syntax-markup-ignored-text: #c9d1d9; --color-prettylights-syntax-markup-ignored-bg: #1158c7; --color-prettylights-syntax-meta-diff-range: #d2a8ff; --color-prettylights-syntax-brackethighlighter-angle: #8b949e; --color-prettylights-syntax-sublimelinter-gutter-mark: #484f58; --color-prettylights-syntax-constant-other-reference-link: #a5d6ff; --color-fg-default: #c9d1d9; --color-fg-muted: #8b949e; --color-fg-subtle: #484f58; --color-canvas-default: #0d1117; --color-canvas-subtle: #161b22; --color-border-default: #30363d; --color-border-muted: #21262d; --color-neutral-muted: rgba(110,118,129,0.4); --color-accent-fg: #58a6ff; --color-accent-emphasis: #1f6feb; --color-attention-subtle: rgba(187,128,9,0.15); --color-danger-fg: #f85149; } markdown-style { display: block; -webkit-text-size-adjust: 100%; font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"; font-size: 16px; line-height: 1.5; word-wrap: break-word; color: var(--color-fg-default); background-color: var(--color-canvas-default); } markdown-style details, markdown-style figcaption, markdown-style figure { display: block; } markdown-style summary { display: list-item; } markdown-style [hidden] { display: none !important; } markdown-style a { background-color: transparent; color: var(--color-accent-fg); text-decoration: none; } markdown-style a:active, markdown-style a:hover { outline-width: 0; } markdown-style abbr[title] { border-bottom: none; text-decoration: underline dotted; } markdown-style b, markdown-style strong { font-weight: 600; } markdown-style dfn { font-style: italic; } markdown-style h1 { margin: .67em 0; font-weight: 600; padding-bottom: .3em; font-size: 2em; border-bottom: 1px solid var(--color-border-muted); } markdown-style mark { background-color: var(--color-attention-subtle); color: var(--color-text-primary); } markdown-style small { font-size: 90%; } markdown-style sub, markdown-style sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; } markdown-style sub { bottom: -0.25em; } markdown-style sup { top: -0.5em; } markdown-style img { border-style: none; max-width: 100%; box-sizing: content-box; background-color: var(--color-canvas-default); } markdown-style code, markdown-style kbd, markdown-style pre, markdown-style samp { font-family: monospace,monospace; font-size: 1em; } markdown-style figure { margin: 1em 40px; } markdown-style hr { box-sizing: content-box; overflow: hidden; background: transparent; border-bottom: 1px solid var(--color-border-muted); height: .25em; padding: 0; margin: 24px 0; background-color: var(--color-border-default); border: 0; } markdown-style input { font: inherit; margin: 0; overflow: visible; font-family: inherit; font-size: inherit; line-height: inherit; } markdown-style [type=button], markdown-style [type=reset], markdown-style [type=submit] { -webkit-appearance: button; } markdown-style [type=button]::-moz-focus-inner, markdown-style [type=reset]::-moz-focus-inner, markdown-style [type=submit]::-moz-focus-inner { border-style: none; padding: 0; } markdown-style [type=button]:-moz-focusring, markdown-style [type=reset]:-moz-focusring, markdown-style [type=submit]:-moz-focusring { outline: 1px dotted ButtonText; } markdown-style [type=checkbox], markdown-style [type=radio] { box-sizing: border-box; padding: 0; } markdown-style [type=number]::-webkit-inner-spin-button, markdown-style [type=number]::-webkit-outer-spin-button { height: auto; } markdown-style [type=search] { -webkit-appearance: textfield; outline-offset: -2px; } markdown-style [type=search]::-webkit-search-cancel-button, markdown-style [type=search]::-webkit-search-decoration { -webkit-appearance: none; } markdown-style ::-webkit-input-placeholder { color: inherit; opacity: .54; } markdown-style ::-webkit-file-upload-button { -webkit-appearance: button; font: inherit; } markdown-style a:hover { text-decoration: underline; } markdown-style hr::before { display: table; content: ""; } markdown-style hr::after { display: table; clear: both; content: ""; } markdown-style table { border-spacing: 0; border-collapse: collapse; display: block; width: max-content; max-width: 100%; overflow: auto; } markdown-style td, markdown-style th { padding: 0; } markdown-style details summary { cursor: pointer; } markdown-style details:not([open])>*:not(summary) { display: none !important; } markdown-style kbd { display: inline-block; padding: 3px 5px; font: 11px ui-monospace,SFMono-Regular,SF Mono,Menlo,Consolas,Liberation Mono,monospace; line-height: 10px; color: var(--color-fg-default); vertical-align: middle; background-color: var(--color-canvas-subtle); border: solid 1px var(--color-neutral-muted); border-bottom-color: var(--color-neutral-muted); border-radius: 6px; box-shadow: inset 0 -1px 0 var(--color-neutral-muted); } markdown-style h1, markdown-style h2, markdown-style h3, markdown-style h4, markdown-style h5, markdown-style h6 { margin-top: 24px; margin-bottom: 16px; font-weight: 600; line-height: 1.25; } markdown-style h2 { font-weight: 600; padding-bottom: .3em; font-size: 1.5em; border-bottom: 1px solid var(--color-border-muted); } markdown-style h3 { font-weight: 600; font-size: 1.25em; } markdown-style h4 { font-weight: 600; font-size: 1em; } markdown-style h5 { font-weight: 600; font-size: .875em; } markdown-style h6 { font-weight: 600; font-size: .85em; color: var(--color-fg-muted); } markdown-style p { margin-top: 0; margin-bottom: 10px; } markdown-style blockquote { margin: 0; padding: 0 1em; color: var(--color-fg-muted); border-left: .25em solid var(--color-border-default); } markdown-style ul, markdown-style ol { margin-top: 0; margin-bottom: 0; padding-left: 2em; } markdown-style ol ol, markdown-style ul ol { list-style-type: lower-roman; } markdown-style ul ul ol, markdown-style ul ol ol, markdown-style ol ul ol, markdown-style ol ol ol { list-style-type: lower-alpha; } markdown-style dd { margin-left: 0; } markdown-style tt, markdown-style code { font-family: ui-monospace,SFMono-Regular,SF Mono,Menlo,Consolas,Liberation Mono,monospace; font-size: 12px; } markdown-style pre { margin-top: 0; margin-bottom: 0; font-family: ui-monospace,SFMono-Regular,SF Mono,Menlo,Consolas,Liberation Mono,monospace; font-size: 12px; word-wrap: normal; } markdown-style .octicon { display: inline-block; overflow: visible !important; vertical-align: text-bottom; fill: currentColor; } markdown-style ::placeholder { color: var(--color-fg-subtle); opacity: 1; } markdown-style input::-webkit-outer-spin-button, markdown-style input::-webkit-inner-spin-button { margin: 0; -webkit-appearance: none; appearance: none; }
|
||
markdown-style .token.comment, markdown-style .token.prolog, markdown-style .token.doctype, markdown-style .token.cdata { color: var(--color-prettylights-syntax-comment); } markdown-style .token.namespace { opacity: 0.7; } markdown-style .token.tag, markdown-style .token.selector, markdown-style .token.constant, markdown-style .token.symbol, markdown-style .token.deleted { color: var(--color-prettylights-syntax-entity-tag); } markdown-style .token.maybe-class-name { color: var(--color-prettylights-syntax-variable); } markdown-style .token.property-access, markdown-style .token.operator, markdown-style .token.boolean, markdown-style .token.number, markdown-style .token.selector markdown-style .token.class, markdown-style .token.attr-name, markdown-style .token.string, markdown-style .token.char, markdown-style .token.builtin { color: var(--color-prettylights-syntax-constant); } markdown-style .token.deleted { color: var(--color-prettylights-syntax-markup-deleted-text); } markdown-style .token.property { color: var(--color-prettylights-syntax-constant); } markdown-style .token.punctuation { color: var(--color-prettylights-syntax-markup-bold); } markdown-style .token.function { color: var(--color-prettylights-syntax-entity); } markdown-style .code-line .token.deleted { background-color: var(--color-prettylights-syntax-markup-deleted-bg); } markdown-style .token.inserted { color: var(--color-prettylights-syntax-markup-inserted-text); } markdown-style .code-line .token.inserted { background-color: var(--color-prettylights-syntax-markup-inserted-bg); } markdown-style .token.variable { color: var(--color-prettylights-syntax-constant); } markdown-style .token.entity, markdown-style .token.url, .language-css markdown-style .token.string, .style markdown-style .token.string { color: var(--color-prettylights-syntax-string); } markdown-style .token.color, markdown-style .token.atrule, markdown-style .token.attr-value, markdown-style .token.function, markdown-style .token.class-name { color: var(--color-prettylights-syntax-string); } markdown-style .token.rule, markdown-style .token.regex, markdown-style .token.important, markdown-style .token.keyword { color: var(--color-prettylights-syntax-keyword); } markdown-style .token.coord { color: var(--color-prettylights-syntax-meta-diff-range); } markdown-style .token.important, markdown-style .token.bold { font-weight: bold; } markdown-style .token.italic { font-style: italic; } markdown-style .token.entity { cursor: help; }
|
||
markdown-style [data-catalyst] { display: block; } markdown-style g-emoji { font-family: "Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol"; font-size: 1em; font-style: normal !important; font-weight: 400; line-height: 1; vertical-align: -0.075em; } markdown-style g-emoji img { width: 1em; height: 1em; } markdown-style::before { display: table; content: ""; } markdown-style::after { display: table; clear: both; content: ""; } markdown-style>*:first-child { margin-top: 0 !important; } markdown-style>*:last-child { margin-bottom: 0 !important; } markdown-style a:not([href]) { color: inherit; text-decoration: none; } markdown-style .absent { color: var(--color-danger-fg); } markdown-style a.anchor { float: left; padding-right: 4px; margin-left: -20px; line-height: 1; } markdown-style a.anchor:focus { outline: none; } markdown-style p, markdown-style blockquote, markdown-style ul, markdown-style ol, markdown-style dl, markdown-style table, markdown-style pre, markdown-style details { margin-top: 0; margin-bottom: 16px; } markdown-style blockquote>:first-child { margin-top: 0; } markdown-style blockquote>:last-child { margin-bottom: 0; } markdown-style sup>a::before { content: "["; } markdown-style sup>a::after { content: "]"; }
|
||
markdown-style .octicon-video { border: 1px solid #d0d7de !important; border-radius: 6px !important; display: block; } markdown-style .octicon-video summary { border-bottom: 1px solid #d0d7de !important; padding: 8px 16px !important; cursor: pointer; } markdown-style .octicon-video > video { display: block !important; max-width: 100% !important; padding: 2px; box-sizing: border-box; border-bottom-right-radius: 6px !important; border-bottom-left-radius: 6px !important; } markdown-style details.octicon-video:not([open])>*:not(summary) { display: none !important; } markdown-style details.octicon-video:not([open]) > summary { border-bottom: 0 !important; } markdown-style h1 .octicon-link, markdown-style h2 .octicon-link, markdown-style h3 .octicon-link, markdown-style h4 .octicon-link, markdown-style h5 .octicon-link, markdown-style h6 .octicon-link { color: var(--color-fg-default); vertical-align: middle; visibility: hidden; } markdown-style h1:hover .anchor, markdown-style h2:hover .anchor, markdown-style h3:hover .anchor, markdown-style h4:hover .anchor, markdown-style h5:hover .anchor, markdown-style h6:hover .anchor { text-decoration: none; } markdown-style h1:hover .anchor .octicon-link, markdown-style h2:hover .anchor .octicon-link, markdown-style h3:hover .anchor .octicon-link, markdown-style h4:hover .anchor .octicon-link, markdown-style h5:hover .anchor .octicon-link, markdown-style h6:hover .anchor .octicon-link { visibility: visible; } markdown-style h1 tt, markdown-style h1 code, markdown-style h2 tt, markdown-style h2 code, markdown-style h3 tt, markdown-style h3 code, markdown-style h4 tt, markdown-style h4 code, markdown-style h5 tt, markdown-style h5 code, markdown-style h6 tt, markdown-style h6 code { padding: 0 .2em; font-size: inherit; } markdown-style ul.no-list, markdown-style ol.no-list { padding: 0; list-style-type: none; } markdown-style ol[type="1"] { list-style-type: decimal; } markdown-style ol[type=a] { list-style-type: lower-alpha; } markdown-style ol[type=i] { list-style-type: lower-roman; } markdown-style div>ol:not([type]) { list-style-type: decimal; } markdown-style ul ul, markdown-style ul ol, markdown-style ol ol, markdown-style ol ul { margin-top: 0; margin-bottom: 0; } markdown-style li>p { margin-top: 16px; } markdown-style li+li { margin-top: .25em; } markdown-style dl { padding: 0; } markdown-style dl dt { padding: 0; margin-top: 16px; font-size: 1em; font-style: italic; font-weight: 600; } markdown-style dl dd { padding: 0 16px; margin-bottom: 16px; } markdown-style table th { font-weight: 600; } markdown-style table th, markdown-style table td { padding: 6px 13px; border: 1px solid var(--color-border-default); } markdown-style table tr { background-color: var(--color-canvas-default); border-top: 1px solid var(--color-border-muted); } markdown-style table tr:nth-child(2n) { background-color: var(--color-canvas-subtle); } markdown-style table img { background-color: transparent; vertical-align: middle; } markdown-style img[align=right] { padding-left: 20px; } markdown-style img[align=left] { padding-right: 20px; } markdown-style .emoji { max-width: none; vertical-align: text-top; background-color: transparent; } markdown-style span.frame { display: block; overflow: hidden; } markdown-style span.frame>span { display: block; float: left; width: auto; padding: 7px; margin: 13px 0 0; overflow: hidden; border: 1px solid var(--color-border-default); } markdown-style span.frame span img { display: block; float: left; } markdown-style span.frame span span { display: block; padding: 5px 0 0; clear: both; color: var(--color-fg-default); } markdown-style span.align-center { display: block; overflow: hidden; clear: both; } markdown-style span.align-center>span { display: block; margin: 13px auto 0; overflow: hidden; text-align: center; } markdown-style span.align-center span img { margin: 0 auto; text-align: center; } markdown-style span.align-right { display: block; overflow: hidden; clear: both; } markdown-style span.align-right>span { display: block; margin: 13px 0 0; overflow: hidden; text-align: right; } markdown-style span.align-right span img { margin: 0; text-align: right; } markdown-style span.float-left { display: block; float: left; margin-right: 13px; overflow: hidden; } markdown-style span.float-left span { margin: 13px 0 0; } markdown-style span.float-right { display: block; float: right; margin-left: 13px; overflow: hidden; } markdown-style span.float-right>span { display: block; margin: 13px auto 0; overflow: hidden; text-align: right; } markdown-style code, markdown-style tt { padding: .2em .4em; margin: 0; font-size: 85%; background-color: var(--color-neutral-muted); border-radius: 6px; } markdown-style code br, markdown-style tt br { display: none; } markdown-style del code { text-decoration: inherit; } markdown-style pre code { font-size: 100%; } markdown-style pre>code { padding: 0; margin: 0; word-break: normal; white-space: pre; background: transparent; border: 0; } markdown-style pre { position: relative; font-size: 85%; line-height: 1.45; background-color: var(--color-canvas-subtle); border-radius: 6px; } markdown-style pre code, markdown-style pre tt { display: inline; max-width: auto; padding: 0; margin: 0; overflow: visible; line-height: inherit; word-wrap: normal; background-color: transparent; border: 0; } markdown-style pre > code { padding: 16px; overflow: auto; display: block; } markdown-style .csv-data td, markdown-style .csv-data th { padding: 5px; overflow: hidden; font-size: 12px; line-height: 1; text-align: left; white-space: nowrap; } markdown-style .csv-data .blob-num { padding: 10px 8px 9px; text-align: right; background: var(--color-canvas-default); border: 0; } markdown-style .csv-data tr { border-top: 0; } markdown-style .csv-data th { font-weight: 600; background: var(--color-canvas-subtle); border-top: 0; } markdown-style .footnotes { font-size: 12px; color: var(--color-fg-muted); border-top: 1px solid var(--color-border-default); } markdown-style .footnotes ol { padding-left: 16px; } markdown-style .footnotes li { position: relative; } markdown-style .footnotes li:target::before { position: absolute; top: -8px; right: -8px; bottom: -8px; left: -24px; pointer-events: none; content: ""; border: 2px solid var(--color-accent-emphasis); border-radius: 6px; } markdown-style .footnotes li:target { color: var(--color-fg-default); } markdown-style .footnotes .data-footnote-backref g-emoji { font-family: monospace; } markdown-style .task-list-item { list-style-type: none; } markdown-style .task-list-item label { font-weight: 400; } markdown-style .task-list-item.enabled label { cursor: pointer; } markdown-style .task-list-item+.task-list-item { margin-top: 3px; } markdown-style .task-list-item .handle { display: none; } markdown-style .task-list-item-checkbox, markdown-style input[type="checkbox"] { margin: 0 .2em .25em -1.6em; vertical-align: middle; } markdown-style .contains-task-list:dir(rtl) .task-list-item-checkbox, markdown-style .contains-task-list:dir(rtl) input[type="checkbox"] { margin: 0 -1.6em .25em .2em; } markdown-style ::-webkit-calendar-picker-indicator { filter: invert(50%); }
|
||
</style>
|
||
<slot></slot>
|
||
`;
|
||
class MarkdownStyle extends HTMLElement {
|
||
constructor() {
|
||
super();
|
||
this.shadow = this.attachShadow({ mode: 'open' });
|
||
this.shadow.appendChild(__TEMPLATE__.content.cloneNode(true));
|
||
const style = Array.prototype.slice
|
||
.call(this.shadow.children)
|
||
.find((item) => item.tagName === 'STYLE');
|
||
if (style) {
|
||
const id = '__MARKDOWN_STYLE__';
|
||
const findStyle = document.getElementById(id);
|
||
if (!findStyle) {
|
||
style.id = id;
|
||
document.head.append(style);
|
||
}
|
||
}
|
||
}
|
||
get theme() {
|
||
const value = this.getAttribute('theme');
|
||
return value === null ? '' : value;
|
||
}
|
||
set theme(name) {
|
||
this.setAttribute('theme', name);
|
||
}
|
||
connectedCallback() {
|
||
if (!this.theme) {
|
||
const { colorMode } = document.documentElement.dataset;
|
||
this.theme = colorMode;
|
||
const observer = new MutationObserver((mutationsList, observer) => {
|
||
this.theme = document.documentElement.dataset.colorMode;
|
||
});
|
||
observer.observe(document.documentElement, { attributes: true });
|
||
window.matchMedia('(prefers-color-scheme: light)').onchange = (event) => {
|
||
this.theme = event.matches ? 'light' : 'dark';
|
||
};
|
||
window.matchMedia('(prefers-color-scheme: dark)').onchange = (event) => {
|
||
this.theme = event.matches ? 'dark' : 'light';
|
||
};
|
||
}
|
||
}
|
||
}
|
||
customElements.define('markdown-style', MarkdownStyle);</script><markdown-style style="max-width: 960px; margin: 0 auto 60px auto; padding: 8px;" class="markdown-style">
|
||
<h1 id="awk"><a class="anchor" aria-hidden="true" tabindex="-1" href="#awk"><span class="octicon octicon-link"></span></a>awk</h1>
|
||
<p>文本和数据进行处理的编程语言</p>
|
||
<h2 id="补充说明"><a class="anchor" aria-hidden="true" tabindex="-1" href="#补充说明"><span class="octicon octicon-link"></span></a>补充说明</h2>
|
||
<p><strong>awk</strong> 是一种编程语言,用于在linux/unix下对文本和数据进行处理。数据可以来自标准输入(stdin)、一个或多个文件,或其它命令的输出。它支持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具。它在命令行中使用,但更多是作为脚本来使用。awk有很多内建的功能,比如数组、函数等,这是它和C语言的相同之处,灵活性是awk最大的优势。</p>
|
||
<h2 id="awk命令格式和选项"><a class="anchor" aria-hidden="true" tabindex="-1" href="#awk命令格式和选项"><span class="octicon octicon-link"></span></a>awk命令格式和选项</h2>
|
||
<p><strong>语法形式</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token punctuation">[</span>options<span class="token punctuation">]</span> <span class="token string">'script'</span> <span class="token assign-left variable">var</span><span class="token operator">=</span>value file<span class="token punctuation">(</span>s<span class="token punctuation">)</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token function">awk</span> <span class="token punctuation">[</span>options<span class="token punctuation">]</span> <span class="token parameter variable">-f</span> scriptfile <span class="token assign-left variable">var</span><span class="token operator">=</span>value file<span class="token punctuation">(</span>s<span class="token punctuation">)</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk [options] 'script' var=value file(s)
|
||
awk [options] -f scriptfile var=value file(s)
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p><strong>常用命令选项</strong></p>
|
||
<ul>
|
||
<li><strong>-F fs</strong> fs指定输入分隔符,fs可以是字符串或正则表达式,如-F:,默认的分隔符是连续的空格或制表符</li>
|
||
<li><strong>-v var=value</strong> 赋值一个用户定义变量,将外部变量传递给awk</li>
|
||
<li><strong>-f scripfile</strong> 从脚本文件中读取awk命令</li>
|
||
<li><strong>-m[fr] val</strong> 对val值设置内在限制,-mf选项限制分配给val的最大块数目;-mr选项限制记录的最大数目。这两个功能是Bell实验室版awk的扩展功能,在标准awk中不适用。</li>
|
||
</ul>
|
||
<h2 id="awk模式和操作"><a class="anchor" aria-hidden="true" tabindex="-1" href="#awk模式和操作"><span class="octicon octicon-link"></span></a>awk模式和操作</h2>
|
||
<p>awk脚本是由模式和操作组成的。</p>
|
||
<h3 id="模式"><a class="anchor" aria-hidden="true" tabindex="-1" href="#模式"><span class="octicon octicon-link"></span></a>模式</h3>
|
||
<p>模式可以是以下任意一个:</p>
|
||
<ul>
|
||
<li>/正则表达式/:使用通配符的扩展集。</li>
|
||
<li>关系表达式:使用运算符进行操作,可以是字符串或数字的比较测试。</li>
|
||
<li>模式匹配表达式:用运算符<code>~</code>(匹配)和<code>!~</code>(不匹配)。</li>
|
||
<li>BEGIN语句块、pattern语句块、END语句块:参见awk的工作原理</li>
|
||
</ul>
|
||
<h3 id="操作"><a class="anchor" aria-hidden="true" tabindex="-1" href="#操作"><span class="octicon octicon-link"></span></a>操作</h3>
|
||
<p>操作由一个或多个命令、函数、表达式组成,之间由换行符或分号隔开,并位于大括号内,主要部分是:</p>
|
||
<ul>
|
||
<li>变量或数组赋值</li>
|
||
<li>输出命令</li>
|
||
<li>内置函数</li>
|
||
<li>控制流语句</li>
|
||
</ul>
|
||
<h2 id="awk脚本基本结构"><a class="anchor" aria-hidden="true" tabindex="-1" href="#awk脚本基本结构"><span class="octicon octicon-link"></span></a>awk脚本基本结构</h2>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{ print "start" } pattern{ commands } END{ print "end" }'</span> <span class="token function">file</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{ print "start" } pattern{ commands } END{ print "end" }' file
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>一个awk脚本通常由:BEGIN语句块、能够使用模式匹配的通用语句块、END语句块3部分组成,这三个部分是可选的。任意一个部分都可以不出现在脚本中,脚本通常是被 <strong>单引号</strong> 中,例如:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{ i=0 } { i++ } END{ print i }'</span> filename
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{ i=0 } { i++ } END{ print i }' filename
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="awk的工作原理"><a class="anchor" aria-hidden="true" tabindex="-1" href="#awk的工作原理"><span class="octicon octicon-link"></span></a>awk的工作原理</h3>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{ commands } pattern{ commands } END{ commands }'</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{ commands } pattern{ commands } END{ commands }'
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<ul>
|
||
<li>第一步:执行<code>BEGIN{ commands }</code>语句块中的语句;</li>
|
||
<li>第二步:从文件或标准输入(stdin)读取一行,然后执行<code>pattern{ commands }</code>语句块,它逐行扫描文件,从第一行到最后一行重复这个过程,直到文件全部被读取完毕。</li>
|
||
<li>第三步:当读至输入流末尾时,执行<code>END{ commands }</code>语句块。</li>
|
||
</ul>
|
||
<p><strong>BEGIN语句块</strong> 在awk开始从输入流中读取行 <strong>之前</strong> 被执行,这是一个可选的语句块,比如变量初始化、打印输出表格的表头等语句通常可以写在BEGIN语句块中。</p>
|
||
<p><strong>END语句块</strong> 在awk从输入流中读取完所有的行 <strong>之后</strong> 即被执行,比如打印所有行的分析结果这类信息汇总都是在END语句块中完成,它也是一个可选语句块。</p>
|
||
<p><strong>pattern语句块</strong> 中的通用命令是最重要的部分,它也是可选的。如果没有提供pattern语句块,则默认执行<code>{ print }</code>,即打印每一个读取到的行,awk读取的每一行都会执行该语句块。</p>
|
||
<p><strong>示例</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token builtin class-name">echo</span> <span class="token parameter variable">-e</span> <span class="token string">"A line 1<span class="token entity" title="\n">\n</span>A line 2"</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'BEGIN{ print "Start" } { print } END{ print "End" }'</span>
|
||
</span><span class="code-line line-number" line="2">Start
|
||
</span><span class="code-line line-number" line="3">A line <span class="token number">1</span>
|
||
</span><span class="code-line line-number" line="4">A line <span class="token number">2</span>
|
||
</span><span class="code-line line-number" line="5">End
|
||
</span></code><div onclick="copied(this)" data-code="echo -e "A line 1\nA line 2" | awk 'BEGIN{ print "Start" } { print } END{ print "End" }'
|
||
Start
|
||
A line 1
|
||
A line 2
|
||
End
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>当使用不带参数的<code>print</code>时,它就打印当前行,当<code>print</code>的参数是以逗号进行分隔时,打印时则以空格作为定界符。在awk的print语句块中双引号是被当作拼接符使用,例如:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token builtin class-name">echo</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{ var1="v1"; var2="v2"; var3="v3"; print var1,var2,var3; }'</span>
|
||
</span><span class="code-line line-number" line="2">v1 v2 v3
|
||
</span></code><div onclick="copied(this)" data-code="echo | awk '{ var1="v1"; var2="v2"; var3="v3"; print var1,var2,var3; }'
|
||
v1 v2 v3
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>双引号拼接使用:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token builtin class-name">echo</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{ var1="v1"; var2="v2"; var3="v3"; print var1"="var2"="var3; }'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token assign-left variable">v1</span><span class="token operator">=</span>v2<span class="token operator">=</span>v3
|
||
</span></code><div onclick="copied(this)" data-code="echo | awk '{ var1="v1"; var2="v2"; var3="v3"; print var1"="var2"="var3; }'
|
||
v1=v2=v3
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>{ }类似一个循环体,会对文件中的每一行进行迭代,通常变量初始化语句(如:i=0)以及打印文件头部的语句放入BEGIN语句块中,将打印的结果等语句放在END语句块中。</p>
|
||
<h2 id="awk内置变量预定义变量"><a class="anchor" aria-hidden="true" tabindex="-1" href="#awk内置变量预定义变量"><span class="octicon octicon-link"></span></a>awk内置变量(预定义变量)</h2>
|
||
<p>说明:[A][N][P][G]表示第一个支持变量的工具,[A]=awk、[N]=nawk、[P]=POSIXawk、[G]=gawk</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"> **<span class="token variable">$n</span>** 当前记录的第n个字段,比如n为1表示第一个字段,n为2表示第二个字段。
|
||
</span><span class="code-line line-number" line="2"> **<span class="token variable">$0</span>** 这个变量包含执行过程中当前行的文本内容。
|
||
</span><span class="code-line line-number" line="3"><span class="token punctuation">[</span>N<span class="token punctuation">]</span> **ARGC** 命令行参数的数目。
|
||
</span><span class="code-line line-number" line="4"><span class="token punctuation">[</span>G<span class="token punctuation">]</span> **ARGIND** 命令行中当前文件的位置(从0开始算)。
|
||
</span><span class="code-line line-number" line="5"><span class="token punctuation">[</span>N<span class="token punctuation">]</span> **ARGV** 包含命令行参数的数组。
|
||
</span><span class="code-line line-number" line="6"><span class="token punctuation">[</span>G<span class="token punctuation">]</span> **CONVFMT** 数字转换格式(默认值为%.6g)。
|
||
</span><span class="code-line line-number" line="7"><span class="token punctuation">[</span>P<span class="token punctuation">]</span> **ENVIRON** 环境变量关联数组。
|
||
</span><span class="code-line line-number" line="8"><span class="token punctuation">[</span>N<span class="token punctuation">]</span> **ERRNO** 最后一个系统错误的描述。
|
||
</span><span class="code-line line-number" line="9"><span class="token punctuation">[</span>G<span class="token punctuation">]</span> **FIELDWIDTHS** 字段宽度列表(用空格键分隔)。
|
||
</span><span class="code-line line-number" line="10"><span class="token punctuation">[</span>A<span class="token punctuation">]</span> **FILENAME** 当前输入文件的名。
|
||
</span><span class="code-line line-number" line="11"><span class="token punctuation">[</span>P<span class="token punctuation">]</span> **FNR** 同NR,但相对于当前文件。
|
||
</span><span class="code-line line-number" line="12"><span class="token punctuation">[</span>A<span class="token punctuation">]</span> **FS** 字段分隔符(默认是任何空格)。
|
||
</span><span class="code-line line-number" line="13"><span class="token punctuation">[</span>G<span class="token punctuation">]</span> **IGNORECASE** 如果为真,则进行忽略大小写的匹配。
|
||
</span><span class="code-line line-number" line="14"><span class="token punctuation">[</span>A<span class="token punctuation">]</span> **NF** 表示字段数,在执行过程中对应于当前的字段数。
|
||
</span><span class="code-line line-number" line="15"><span class="token punctuation">[</span>A<span class="token punctuation">]</span> **NR** 表示记录数,在执行过程中对应于当前的行号。
|
||
</span><span class="code-line line-number" line="16"><span class="token punctuation">[</span>A<span class="token punctuation">]</span> **OFMT** 数字的输出格式(默认值是%.6g)。
|
||
</span><span class="code-line line-number" line="17"><span class="token punctuation">[</span>A<span class="token punctuation">]</span> **OFS** 输出字段分隔符(默认值是一个空格)。
|
||
</span><span class="code-line line-number" line="18"><span class="token punctuation">[</span>A<span class="token punctuation">]</span> **ORS** 输出记录分隔符(默认值是一个换行符)。
|
||
</span><span class="code-line line-number" line="19"><span class="token punctuation">[</span>A<span class="token punctuation">]</span> **RS** 记录分隔符(默认是一个换行符)。
|
||
</span><span class="code-line line-number" line="20"><span class="token punctuation">[</span>N<span class="token punctuation">]</span> **RSTART** 由match函数所匹配的字符串的第一个位置。
|
||
</span><span class="code-line line-number" line="21"><span class="token punctuation">[</span>N<span class="token punctuation">]</span> **RLENGTH** 由match函数所匹配的字符串的长度。
|
||
</span><span class="code-line line-number" line="22"><span class="token punctuation">[</span>N<span class="token punctuation">]</span> **SUBSEP** 数组下标分隔符(默认值是34)。
|
||
</span></code><div onclick="copied(this)" data-code=" **$n** 当前记录的第n个字段,比如n为1表示第一个字段,n为2表示第二个字段。
|
||
**$0** 这个变量包含执行过程中当前行的文本内容。
|
||
[N] **ARGC** 命令行参数的数目。
|
||
[G] **ARGIND** 命令行中当前文件的位置(从0开始算)。
|
||
[N] **ARGV** 包含命令行参数的数组。
|
||
[G] **CONVFMT** 数字转换格式(默认值为%.6g)。
|
||
[P] **ENVIRON** 环境变量关联数组。
|
||
[N] **ERRNO** 最后一个系统错误的描述。
|
||
[G] **FIELDWIDTHS** 字段宽度列表(用空格键分隔)。
|
||
[A] **FILENAME** 当前输入文件的名。
|
||
[P] **FNR** 同NR,但相对于当前文件。
|
||
[A] **FS** 字段分隔符(默认是任何空格)。
|
||
[G] **IGNORECASE** 如果为真,则进行忽略大小写的匹配。
|
||
[A] **NF** 表示字段数,在执行过程中对应于当前的字段数。
|
||
[A] **NR** 表示记录数,在执行过程中对应于当前的行号。
|
||
[A] **OFMT** 数字的输出格式(默认值是%.6g)。
|
||
[A] **OFS** 输出字段分隔符(默认值是一个空格)。
|
||
[A] **ORS** 输出记录分隔符(默认值是一个换行符)。
|
||
[A] **RS** 记录分隔符(默认是一个换行符)。
|
||
[N] **RSTART** 由match函数所匹配的字符串的第一个位置。
|
||
[N] **RLENGTH** 由match函数所匹配的字符串的长度。
|
||
[N] **SUBSEP** 数组下标分隔符(默认值是34)。
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>转义序列</p>
|
||
<pre><code class="code-highlight"><span class="code-line line-number" line="1">\\ \自身
|
||
</span><span class="code-line line-number" line="2">\$ 转义$
|
||
</span><span class="code-line line-number" line="3">\t 制表符
|
||
</span><span class="code-line line-number" line="4">\b 退格符
|
||
</span><span class="code-line line-number" line="5">\r 回车符
|
||
</span><span class="code-line line-number" line="6">\n 换行符
|
||
</span><span class="code-line line-number" line="7">\c 取消换行
|
||
</span></code><div onclick="copied(this)" data-code="\\ \自身
|
||
\$ 转义$
|
||
\t 制表符
|
||
\b 退格符
|
||
\r 回车符
|
||
\n 换行符
|
||
\c 取消换行
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p><strong>示例</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token builtin class-name">echo</span> <span class="token parameter variable">-e</span> <span class="token string">"line1 f2 f3<span class="token entity" title="\n">\n</span>line2 f4 f5<span class="token entity" title="\n">\n</span>line3 f6 f7"</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{print "Line No:"NR", No of fields:"NF, "$0="$0, "$1="$1, "$2="$2, "$3="$3}'</span>
|
||
</span><span class="code-line line-number" line="2">Line No:1, No of fields:3 <span class="token variable">$0</span><span class="token operator">=</span>line1 f2 f3 <span class="token variable">$1</span><span class="token operator">=</span>line1 <span class="token variable">$2</span><span class="token operator">=</span>f2 <span class="token variable">$3</span><span class="token operator">=</span>f3
|
||
</span><span class="code-line line-number" line="3">Line No:2, No of fields:3 <span class="token variable">$0</span><span class="token operator">=</span>line2 f4 f5 <span class="token variable">$1</span><span class="token operator">=</span>line2 <span class="token variable">$2</span><span class="token operator">=</span>f4 <span class="token variable">$3</span><span class="token operator">=</span>f5
|
||
</span><span class="code-line line-number" line="4">Line No:3, No of fields:3 <span class="token variable">$0</span><span class="token operator">=</span>line3 f6 f7 <span class="token variable">$1</span><span class="token operator">=</span>line3 <span class="token variable">$2</span><span class="token operator">=</span>f6 <span class="token variable">$3</span><span class="token operator">=</span>f7
|
||
</span></code><div onclick="copied(this)" data-code="echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7" | awk '{print "Line No:"NR", No of fields:"NF, "$0="$0, "$1="$1, "$2="$2, "$3="$3}'
|
||
Line No:1, No of fields:3 $0=line1 f2 f3 $1=line1 $2=f2 $3=f3
|
||
Line No:2, No of fields:3 $0=line2 f4 f5 $1=line2 $2=f4 $3=f5
|
||
Line No:3, No of fields:3 $0=line3 f6 f7 $1=line3 $2=f6 $3=f7
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>使用<code>print $NF</code>可以打印出一行中的最后一个字段,使用<code>$(NF-1)</code>则是打印倒数第二个字段,其他以此类推:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token builtin class-name">echo</span> <span class="token parameter variable">-e</span> <span class="token string">"line1 f2 f3<span class="token entity" title="\n">\n</span> line2 f4 f5"</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{print $NF}'</span>
|
||
</span><span class="code-line line-number" line="2">f3
|
||
</span><span class="code-line line-number" line="3">f5
|
||
</span></code><div onclick="copied(this)" data-code="echo -e "line1 f2 f3\n line2 f4 f5" | awk '{print $NF}'
|
||
f3
|
||
f5
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token builtin class-name">echo</span> <span class="token parameter variable">-e</span> <span class="token string">"line1 f2 f3<span class="token entity" title="\n">\n</span> line2 f4 f5"</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{print $(NF-1)}'</span>
|
||
</span><span class="code-line line-number" line="2">f2
|
||
</span><span class="code-line line-number" line="3">f4
|
||
</span><span class="code-line line-number" line="4">
|
||
</span></code><div onclick="copied(this)" data-code="echo -e "line1 f2 f3\n line2 f4 f5" | awk '{print $(NF-1)}'
|
||
f2
|
||
f4
|
||
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>打印每一行的第二和第三个字段:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'{ print $2,$3 }'</span> filename
|
||
</span></code><div onclick="copied(this)" data-code="awk '{ print $2,$3 }' filename
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>统计文件中的行数:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'END{ print NR }'</span> filename
|
||
</span></code><div onclick="copied(this)" data-code="awk 'END{ print NR }' filename
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>以上命令只使用了END语句块,在读入每一行的时,awk会将NR更新为对应的行号,当到达最后一行NR的值就是最后一行的行号,所以END语句块中的NR就是文件的行数。</p>
|
||
<p>一个每一行中第一个字段值累加的例子:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">seq</span> <span class="token number">5</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'BEGIN{ sum=0; print "总和:" } { print $1"+"; sum+=$1 } END{ print "等于"; print sum }'</span>
|
||
</span><span class="code-line line-number" line="2">总和:
|
||
</span><span class="code-line line-number" line="3"><span class="token number">1</span>+
|
||
</span><span class="code-line line-number" line="4"><span class="token number">2</span>+
|
||
</span><span class="code-line line-number" line="5"><span class="token number">3</span>+
|
||
</span><span class="code-line line-number" line="6"><span class="token number">4</span>+
|
||
</span><span class="code-line line-number" line="7"><span class="token number">5</span>+
|
||
</span><span class="code-line line-number" line="8">等于
|
||
</span><span class="code-line line-number" line="9"><span class="token number">15</span>
|
||
</span></code><div onclick="copied(this)" data-code="seq 5 | awk 'BEGIN{ sum=0; print "总和:" } { print $1"+"; sum+=$1 } END{ print "等于"; print sum }'
|
||
总和:
|
||
1+
|
||
2+
|
||
3+
|
||
4+
|
||
5+
|
||
等于
|
||
15
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h2 id="将外部变量值传递给awk"><a class="anchor" aria-hidden="true" tabindex="-1" href="#将外部变量值传递给awk"><span class="octicon octicon-link"></span></a>将外部变量值传递给awk</h2>
|
||
<p>借助 <strong><code>-v</code>选项</strong> ,可以将外部值(并非来自stdin)传递给awk:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token assign-left variable">VAR</span><span class="token operator">=</span><span class="token number">10000</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token builtin class-name">echo</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token parameter variable">-v</span> <span class="token assign-left variable">VARIABLE</span><span class="token operator">=</span><span class="token variable">$VAR</span> <span class="token string">'{ print VARIABLE }'</span>
|
||
</span></code><div onclick="copied(this)" data-code="VAR=10000
|
||
echo | awk -v VARIABLE=$VAR '{ print VARIABLE }'
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>另一种传递外部变量方法:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token assign-left variable">var1</span><span class="token operator">=</span><span class="token string">"aaa"</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token assign-left variable">var2</span><span class="token operator">=</span><span class="token string">"bbb"</span>
|
||
</span><span class="code-line line-number" line="3"><span class="token builtin class-name">echo</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{ print v1,v2 }'</span> <span class="token assign-left variable">v1</span><span class="token operator">=</span><span class="token variable">$var1</span> <span class="token assign-left variable">v2</span><span class="token operator">=</span><span class="token variable">$var2</span>
|
||
</span></code><div onclick="copied(this)" data-code="var1="aaa"
|
||
var2="bbb"
|
||
echo | awk '{ print v1,v2 }' v1=$var1 v2=$var2
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>当输入来自于文件时使用:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'{ print v1,v2 }'</span> <span class="token assign-left variable">v1</span><span class="token operator">=</span><span class="token variable">$var1</span> <span class="token assign-left variable">v2</span><span class="token operator">=</span><span class="token variable">$var2</span> filename
|
||
</span></code><div onclick="copied(this)" data-code="awk '{ print v1,v2 }' v1=$var1 v2=$var2 filename
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>以上方法中,变量之间用空格分隔作为awk的命令行参数跟随在BEGIN、{}和END语句块之后。</p>
|
||
<h2 id="查找进程pid"><a class="anchor" aria-hidden="true" tabindex="-1" href="#查找进程pid"><span class="octicon octicon-link"></span></a>查找进程pid</h2>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">netstat</span> <span class="token parameter variable">-antup</span> <span class="token operator">|</span> <span class="token function">grep</span> <span class="token number">7770</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{ print $NF NR}'</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{ print $1}'</span>
|
||
</span></code><div onclick="copied(this)" data-code="netstat -antup | grep 7770 | awk '{ print $NF NR}' | awk '{ print $1}'
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h2 id="awk运算与判断"><a class="anchor" aria-hidden="true" tabindex="-1" href="#awk运算与判断"><span class="octicon octicon-link"></span></a>awk运算与判断</h2>
|
||
<p>作为一种程序设计语言所应具有的特点之一,awk支持多种运算,这些运算与C语言提供的基本相同。awk还提供了一系列内置的运算函数(如log、sqr、cos、sin等)和一些用于对字符串进行操作(运算)的函数(如length、substr等等)。这些函数的引用大大的提高了awk的运算功能。作为对条件转移指令的一部分,关系判断是每种程序设计语言都具备的功能,awk也不例外,awk中允许进行多种测试,作为样式匹配,还提供了模式匹配表达式~(匹配)和!~(不匹配)。作为对测试的一种扩充,awk也支持用逻辑运算符。</p>
|
||
<h3 id="算术运算符"><a class="anchor" aria-hidden="true" tabindex="-1" href="#算术运算符"><span class="octicon octicon-link"></span></a>算术运算符</h3>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>运算符</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>+ -</td>
|
||
<td>加,减</td>
|
||
</tr>
|
||
<tr>
|
||
<td>* / &</td>
|
||
<td>乘,除与求余</td>
|
||
</tr>
|
||
<tr>
|
||
<td>+ - !</td>
|
||
<td>一元加,减和逻辑非</td>
|
||
</tr>
|
||
<tr>
|
||
<td>^ ***</td>
|
||
<td>求幂</td>
|
||
</tr>
|
||
<tr>
|
||
<td>++ --</td>
|
||
<td>增加或减少,作为前缀或后缀</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>例:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{a="b";print a++,++a;}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">0</span> <span class="token number">2</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{a="b";print a++,++a;}'
|
||
0 2
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>注意:所有用作算术运算符进行操作,操作数自动转为数值,所有非数值都变为0</p>
|
||
<h3 id="赋值运算符"><a class="anchor" aria-hidden="true" tabindex="-1" href="#赋值运算符"><span class="octicon octicon-link"></span></a>赋值运算符</h3>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>运算符</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>= += -= *= /= %= ^= **=</td>
|
||
<td>赋值语句</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>例:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token assign-left variable">a</span><span class="token operator">+=</span><span class="token number">5</span><span class="token punctuation">;</span> 等价于:a<span class="token operator">=</span>a+5<span class="token punctuation">;</span> 其它同类
|
||
</span></code><div onclick="copied(this)" data-code="a+=5; 等价于:a=a+5; 其它同类
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="逻辑运算符"><a class="anchor" aria-hidden="true" tabindex="-1" href="#逻辑运算符"><span class="octicon octicon-link"></span></a>逻辑运算符</h3>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>运算符</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td><code>||</code></td>
|
||
<td>逻辑或</td>
|
||
</tr>
|
||
<tr>
|
||
<td>&&</td>
|
||
<td>逻辑与</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>例:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{a=1;b=2;print (a>5 && b<=2),(a>5 || b<=2);}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">0</span> <span class="token number">1</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{a=1;b=2;print (a>5 && b<=2),(a>5 || b<=2);}'
|
||
0 1
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="正则运算符"><a class="anchor" aria-hidden="true" tabindex="-1" href="#正则运算符"><span class="octicon octicon-link"></span></a>正则运算符</h3>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>运算符</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>~ !~</td>
|
||
<td>匹配正则表达式和不匹配正则表达式</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<pre><code class="code-highlight"><span class="code-line line-number" line="1">^ 行首
|
||
</span><span class="code-line line-number" line="2">$ 行尾
|
||
</span><span class="code-line line-number" line="3">. 除了换行符以外的任意单个字符
|
||
</span><span class="code-line line-number" line="4">* 前导字符的零个或多个
|
||
</span><span class="code-line line-number" line="5">.* 所有字符
|
||
</span><span class="code-line line-number" line="6">[] 字符组内的任一字符
|
||
</span><span class="code-line line-number" line="7">[^]对字符组内的每个字符取反(不匹配字符组内的每个字符)
|
||
</span><span class="code-line line-number" line="8">^[^] 非字符组内的字符开头的行
|
||
</span><span class="code-line line-number" line="9">[a-z] 小写字母
|
||
</span><span class="code-line line-number" line="10">[A-Z] 大写字母
|
||
</span><span class="code-line line-number" line="11">[a-Z] 小写和大写字母
|
||
</span><span class="code-line line-number" line="12">[0-9] 数字
|
||
</span><span class="code-line line-number" line="13">\< 单词头单词一般以空格或特殊字符做分隔,连续的字符串被当做单词
|
||
</span><span class="code-line line-number" line="14">\> 单词尾
|
||
</span></code><div onclick="copied(this)" data-code="^ 行首
|
||
$ 行尾
|
||
. 除了换行符以外的任意单个字符
|
||
* 前导字符的零个或多个
|
||
.* 所有字符
|
||
[] 字符组内的任一字符
|
||
[^]对字符组内的每个字符取反(不匹配字符组内的每个字符)
|
||
^[^] 非字符组内的字符开头的行
|
||
[a-z] 小写字母
|
||
[A-Z] 大写字母
|
||
[a-Z] 小写和大写字母
|
||
[0-9] 数字
|
||
\< 单词头单词一般以空格或特殊字符做分隔,连续的字符串被当做单词
|
||
\> 单词尾
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<blockquote>
|
||
<p>正则需要用 /正则/ 包围住</p>
|
||
</blockquote>
|
||
<p>例:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{a="100testa";if(a ~ /^100*/){print "ok";}}'</span>
|
||
</span><span class="code-line line-number" line="2">ok
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{a="100testa";if(a ~ /^100*/){print "ok";}}'
|
||
ok
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="关系运算符"><a class="anchor" aria-hidden="true" tabindex="-1" href="#关系运算符"><span class="octicon octicon-link"></span></a>关系运算符</h3>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>运算符</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>< <= > >= != ==</td>
|
||
<td>关系运算符</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>例:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{a=11;if(a >= 9){print "ok";}}'</span>
|
||
</span><span class="code-line line-number" line="2">ok
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{a=11;if(a >= 9){print "ok";}}'
|
||
ok
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>注意:> < 可以作为字符串比较,也可以用作数值比较,关键看操作数如果是字符串就会转换为字符串比较。两个都为数字才转为数值比较。字符串比较:按照ASCII码顺序比较。</p>
|
||
<h3 id="其它运算符"><a class="anchor" aria-hidden="true" tabindex="-1" href="#其它运算符"><span class="octicon octicon-link"></span></a>其它运算符</h3>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>运算符</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>$</td>
|
||
<td>字段引用</td>
|
||
</tr>
|
||
<tr>
|
||
<td>空格</td>
|
||
<td>字符串连接符</td>
|
||
</tr>
|
||
<tr>
|
||
<td>?:</td>
|
||
<td>C条件表达式</td>
|
||
</tr>
|
||
<tr>
|
||
<td>in</td>
|
||
<td>数组中是否存在某键值</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>例:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{a="b";print a=="b"?"ok":"err";}'</span>
|
||
</span><span class="code-line line-number" line="2">ok
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{a="b";print a=="b"?"ok":"err";}'
|
||
ok
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{a="b";arr[0]="b";arr[1]="c";print (a in arr);}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">0</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{a="b";arr[0]="b";arr[1]="c";print (a in arr);}'
|
||
0
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<pre><code class="code-highlight"><span class="code-line line-number" line="1">awk 'BEGIN{a="b";arr[0]="b";arr["b"]="c";print (a in arr);}'
|
||
</span><span class="code-line line-number" line="2">1
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{a="b";arr[0]="b";arr["b"]="c";print (a in arr);}'
|
||
1
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="运算级优先级表"><a class="anchor" aria-hidden="true" tabindex="-1" href="#运算级优先级表"><span class="octicon octicon-link"></span></a>运算级优先级表</h3>
|
||
<p>!级别越高越优先<br>级别越高越优先</p>
|
||
<h2 id="awk高级输入输出"><a class="anchor" aria-hidden="true" tabindex="-1" href="#awk高级输入输出"><span class="octicon octicon-link"></span></a>awk高级输入输出</h2>
|
||
<h3 id="读取下一条记录"><a class="anchor" aria-hidden="true" tabindex="-1" href="#读取下一条记录"><span class="octicon octicon-link"></span></a>读取下一条记录</h3>
|
||
<p>awk中<code>next</code>语句使用:在循环逐行匹配,如果遇到next,就会跳过当前行,直接忽略下面语句。而进行下一行匹配。next语句一般用于多行合并:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">cat</span> text.txt
|
||
</span><span class="code-line line-number" line="2">a
|
||
</span><span class="code-line line-number" line="3">b
|
||
</span><span class="code-line line-number" line="4">c
|
||
</span><span class="code-line line-number" line="5">d
|
||
</span><span class="code-line line-number" line="6">e
|
||
</span><span class="code-line line-number" line="7">
|
||
</span><span class="code-line line-number" line="8"><span class="token function">awk</span> <span class="token string">'NR%2==1{next}{print NR,$0;}'</span> text.txt
|
||
</span><span class="code-line line-number" line="9"><span class="token number">2</span> b
|
||
</span><span class="code-line line-number" line="10"><span class="token number">4</span> d
|
||
</span></code><div onclick="copied(this)" data-code="cat text.txt
|
||
a
|
||
b
|
||
c
|
||
d
|
||
e
|
||
|
||
awk 'NR%2==1{next}{print NR,$0;}' text.txt
|
||
2 b
|
||
4 d
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>当记录行号除以2余1,就跳过当前行。下面的<code>print NR,$0</code>也不会执行。下一行开始,程序有开始判断<code>NR%2</code>值。这个时候记录行号是<code>:2</code> ,就会执行下面语句块:<code>'print NR,$0'</code></p>
|
||
<p>跳过以“web”为首的行,再将该行内容分别与下面不以“web”为首的行合并打印,使用一个“:”和一个制表符连接:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">cat</span> text.txt
|
||
</span><span class="code-line line-number" line="2">web01<span class="token punctuation">[</span><span class="token number">192.168</span>.2.100<span class="token punctuation">]</span>
|
||
</span><span class="code-line line-number" line="3">httpd ok
|
||
</span><span class="code-line line-number" line="4">tomcat ok
|
||
</span><span class="code-line line-number" line="5"><span class="token function">sendmail</span> ok
|
||
</span><span class="code-line line-number" line="6">web02<span class="token punctuation">[</span><span class="token number">192.168</span>.2.101<span class="token punctuation">]</span>
|
||
</span><span class="code-line line-number" line="7">httpd ok
|
||
</span><span class="code-line line-number" line="8">postfix ok
|
||
</span><span class="code-line line-number" line="9">web03<span class="token punctuation">[</span><span class="token number">192.168</span>.2.102<span class="token punctuation">]</span>
|
||
</span><span class="code-line line-number" line="10">mysqld ok
|
||
</span><span class="code-line line-number" line="11">httpd ok
|
||
</span><span class="code-line line-number" line="12"><span class="token number">0</span>
|
||
</span><span class="code-line line-number" line="13"><span class="token function">awk</span> <span class="token string">'/^web/{T=$0;next;}{print T":\t"$0;}'</span> text.txt
|
||
</span><span class="code-line line-number" line="14">web01<span class="token punctuation">[</span><span class="token number">192.168</span>.2.100<span class="token punctuation">]</span>: httpd ok
|
||
</span><span class="code-line line-number" line="15">web01<span class="token punctuation">[</span><span class="token number">192.168</span>.2.100<span class="token punctuation">]</span>: tomcat ok
|
||
</span><span class="code-line line-number" line="16">web01<span class="token punctuation">[</span><span class="token number">192.168</span>.2.100<span class="token punctuation">]</span>: <span class="token function">sendmail</span> ok
|
||
</span><span class="code-line line-number" line="17">web02<span class="token punctuation">[</span><span class="token number">192.168</span>.2.101<span class="token punctuation">]</span>: httpd ok
|
||
</span><span class="code-line line-number" line="18">web02<span class="token punctuation">[</span><span class="token number">192.168</span>.2.101<span class="token punctuation">]</span>: postfix ok
|
||
</span><span class="code-line line-number" line="19">web03<span class="token punctuation">[</span><span class="token number">192.168</span>.2.102<span class="token punctuation">]</span>: mysqld ok
|
||
</span><span class="code-line line-number" line="20">web03<span class="token punctuation">[</span><span class="token number">192.168</span>.2.102<span class="token punctuation">]</span>: httpd ok
|
||
</span></code><div onclick="copied(this)" data-code="cat text.txt
|
||
web01[192.168.2.100]
|
||
httpd ok
|
||
tomcat ok
|
||
sendmail ok
|
||
web02[192.168.2.101]
|
||
httpd ok
|
||
postfix ok
|
||
web03[192.168.2.102]
|
||
mysqld ok
|
||
httpd ok
|
||
0
|
||
awk '/^web/{T=$0;next;}{print T":\t"$0;}' text.txt
|
||
web01[192.168.2.100]: httpd ok
|
||
web01[192.168.2.100]: tomcat ok
|
||
web01[192.168.2.100]: sendmail ok
|
||
web02[192.168.2.101]: httpd ok
|
||
web02[192.168.2.101]: postfix ok
|
||
web03[192.168.2.102]: mysqld ok
|
||
web03[192.168.2.102]: httpd ok
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="简单地读取一条记录"><a class="anchor" aria-hidden="true" tabindex="-1" href="#简单地读取一条记录"><span class="octicon octicon-link"></span></a>简单地读取一条记录</h3>
|
||
<p><code>awk getline</code>用法:输出重定向需用到<code>getline函数</code>。getline从标准输入、管道或者当前正在处理的文件之外的其他输入文件获得输入。它负责从输入获得下一行的内容,并给NF,NR和FNR等内建变量赋值。如果得到一条记录,getline函数返回1,如果到达文件的末尾就返回0,如果出现错误,例如打开文件失败,就返回-1。</p>
|
||
<p>getline语法:getline var,变量var包含了特定行的内容。</p>
|
||
<p>awk getline从整体上来说,用法说明:</p>
|
||
<ul>
|
||
<li><strong>当其左右无重定向符<code>|</code>或<code><</code>时:</strong> getline作用于当前文件,读入当前文件的第一行给其后跟的变量<code>var</code>或<code>$0</code>(无变量),应该注意到,由于awk在处理getline之前已经读入了一行,所以getline得到的返回结果是隔行的。</li>
|
||
<li><strong>当其左右有重定向符<code>|</code>或<code><</code>时:</strong> getline则作用于定向输入文件,由于该文件是刚打开,并没有被awk读入一行,只是getline读入,那么getline返回的是该文件的第一行,而不是隔行。</li>
|
||
</ul>
|
||
<p><strong>示例:</strong></p>
|
||
<p>执行linux的<code>date</code>命令,并通过管道输出给<code>getline</code>,然后再把输出赋值给自定义变量out,并打印它:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{ "date" | getline out; print out }'</span> <span class="token builtin class-name">test</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{ "date" | getline out; print out }' test
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>执行shell的date命令,并通过管道输出给getline,然后getline从管道中读取并将输入赋值给out,split函数把变量out转化成数组mon,然后打印数组mon的第二个元素:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{ "date" | getline out; split(out,mon); print mon[2] }'</span> <span class="token builtin class-name">test</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{ "date" | getline out; split(out,mon); print mon[2] }' test
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>命令ls的输出传递给geline作为输入,循环使getline从ls的输出中读取一行,并把它打印到屏幕。这里没有输入文件,因为BEGIN块在打开输入文件前执行,所以可以忽略输入文件。</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{ while( "ls" | getline) print }'</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{ while( "ls" | getline) print }'
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="关闭文件"><a class="anchor" aria-hidden="true" tabindex="-1" href="#关闭文件"><span class="octicon octicon-link"></span></a>关闭文件</h3>
|
||
<p>awk中允许在程序中关闭一个输入或输出文件,方法是使用awk的close语句。</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1">close<span class="token punctuation">(</span><span class="token string">"filename"</span><span class="token punctuation">)</span>
|
||
</span></code><div onclick="copied(this)" data-code="close("filename")
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>filename可以是getline打开的文件,也可以是stdin,包含文件名的变量或者getline使用的确切命令。或一个输出文件,可以是stdout,包含文件名的变量或使用管道的确切命令。</p>
|
||
<h3 id="输出到一个文件"><a class="anchor" aria-hidden="true" tabindex="-1" href="#输出到一个文件"><span class="octicon octicon-link"></span></a>输出到一个文件</h3>
|
||
<p>awk中允许用如下方式将结果输出到一个文件:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token builtin class-name">echo</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{printf("hello word!n") > "datafile"}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token comment"># 或</span>
|
||
</span><span class="code-line line-number" line="3"><span class="token builtin class-name">echo</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{printf("hello word!n") >> "datafile"}'</span>
|
||
</span></code><div onclick="copied(this)" data-code="echo | awk '{printf("hello word!n") > "datafile"}'
|
||
# 或
|
||
echo | awk '{printf("hello word!n") >> "datafile"}'
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h2 id="设置字段定界符"><a class="anchor" aria-hidden="true" tabindex="-1" href="#设置字段定界符"><span class="octicon octicon-link"></span></a>设置字段定界符</h2>
|
||
<p>默认的字段定界符是空格,可以使用<code>-F "定界符"</code> 明确指定一个定界符:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> -F: <span class="token string">'{ print $NF }'</span> /etc/passwd
|
||
</span><span class="code-line line-number" line="2"><span class="token comment"># 或</span>
|
||
</span><span class="code-line line-number" line="3"><span class="token function">awk</span> <span class="token string">'BEGIN{ FS=":" } { print $NF }'</span> /etc/passwd
|
||
</span></code><div onclick="copied(this)" data-code="awk -F: '{ print $NF }' /etc/passwd
|
||
# 或
|
||
awk 'BEGIN{ FS=":" } { print $NF }' /etc/passwd
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>在<code>BEGIN语句块</code>中则可以用<code>OFS=“定界符”</code>设置输出字段的定界符。</p>
|
||
<h2 id="流程控制语句"><a class="anchor" aria-hidden="true" tabindex="-1" href="#流程控制语句"><span class="octicon octicon-link"></span></a>流程控制语句</h2>
|
||
<p>在linux awk的while、do-while和for语句中允许使用break,continue语句来控制流程走向,也允许使用exit这样的语句来退出。break中断当前正在执行的循环并跳到循环外执行下一条语句。if 是流程选择用法。awk中,流程控制语句,语法结构,与c语言类型。有了这些语句,其实很多shell程序都可以交给awk,而且性能是非常快的。下面是各个语句用法。</p>
|
||
<h3 id="条件判断语句"><a class="anchor" aria-hidden="true" tabindex="-1" href="#条件判断语句"><span class="octicon octicon-link"></span></a>条件判断语句</h3>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1">if<span class="token punctuation">(</span>表达式<span class="token punctuation">)</span>
|
||
</span><span class="code-line line-number" line="2"> 语句1
|
||
</span><span class="code-line line-number" line="3"><span class="token keyword">else</span>
|
||
</span><span class="code-line line-number" line="4"> 语句2
|
||
</span></code><div onclick="copied(this)" data-code="if(表达式)
|
||
语句1
|
||
else
|
||
语句2
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>格式中语句1可以是多个语句,为了方便判断和阅读,最好将多个语句用{}括起来。awk分枝结构允许嵌套,其格式为:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1">if<span class="token punctuation">(</span>表达式<span class="token punctuation">)</span>
|
||
</span><span class="code-line line-number" line="2"> <span class="token punctuation">{</span>语句1<span class="token punctuation">}</span>
|
||
</span><span class="code-line line-number" line="3"><span class="token keyword">else</span> if<span class="token punctuation">(</span>表达式<span class="token punctuation">)</span>
|
||
</span><span class="code-line line-number" line="4"> <span class="token punctuation">{</span>语句2<span class="token punctuation">}</span>
|
||
</span><span class="code-line line-number" line="5"><span class="token keyword">else</span>
|
||
</span><span class="code-line line-number" line="6"> <span class="token punctuation">{</span>语句3<span class="token punctuation">}</span>
|
||
</span></code><div onclick="copied(this)" data-code="if(表达式)
|
||
{语句1}
|
||
else if(表达式)
|
||
{语句2}
|
||
else
|
||
{语句3}
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>示例:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{
|
||
</span></span><span class="code-line line-number" line="2"><span class="token string">test=100;
|
||
</span></span><span class="code-line line-number" line="3"><span class="token string">if(test>90){
|
||
</span></span><span class="code-line line-number" line="4"><span class="token string"> print "very good";
|
||
</span></span><span class="code-line line-number" line="5"><span class="token string"> }
|
||
</span></span><span class="code-line line-number" line="6"><span class="token string"> else if(test>60){
|
||
</span></span><span class="code-line line-number" line="7"><span class="token string"> print "good";
|
||
</span></span><span class="code-line line-number" line="8"><span class="token string"> }
|
||
</span></span><span class="code-line line-number" line="9"><span class="token string"> else{
|
||
</span></span><span class="code-line line-number" line="10"><span class="token string"> print "no pass";
|
||
</span></span><span class="code-line line-number" line="11"><span class="token string"> }
|
||
</span></span><span class="code-line line-number" line="12"><span class="token string">}'</span>
|
||
</span><span class="code-line line-number" line="13">
|
||
</span><span class="code-line line-number" line="14">very good
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{
|
||
test=100;
|
||
if(test>90){
|
||
print "very good";
|
||
}
|
||
else if(test>60){
|
||
print "good";
|
||
}
|
||
else{
|
||
print "no pass";
|
||
}
|
||
}'
|
||
|
||
very good
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>每条命令语句后面可以用<code>;</code> <strong>分号</strong> 结尾。</p>
|
||
<h3 id="循环语句"><a class="anchor" aria-hidden="true" tabindex="-1" href="#循环语句"><span class="octicon octicon-link"></span></a>循环语句</h3>
|
||
<h3 id="-while语句"><a class="anchor" aria-hidden="true" tabindex="-1" href="#-while语句"><span class="octicon octicon-link"></span></a># while语句</h3>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1">while<span class="token punctuation">(</span>表达式<span class="token punctuation">)</span>
|
||
</span><span class="code-line line-number" line="2"> <span class="token punctuation">{</span>语句<span class="token punctuation">}</span>
|
||
</span></code><div onclick="copied(this)" data-code="while(表达式)
|
||
{语句}
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>示例:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{
|
||
</span></span><span class="code-line line-number" line="2"><span class="token string">test=100;
|
||
</span></span><span class="code-line line-number" line="3"><span class="token string">total=0;
|
||
</span></span><span class="code-line line-number" line="4"><span class="token string">while(i<=test){
|
||
</span></span><span class="code-line line-number" line="5"><span class="token string"> total+=i;
|
||
</span></span><span class="code-line line-number" line="6"><span class="token string"> i++;
|
||
</span></span><span class="code-line line-number" line="7"><span class="token string">}
|
||
</span></span><span class="code-line line-number" line="8"><span class="token string">print total;
|
||
</span></span><span class="code-line line-number" line="9"><span class="token string">}'</span>
|
||
</span><span class="code-line line-number" line="10"><span class="token number">5050</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{
|
||
test=100;
|
||
total=0;
|
||
while(i<=test){
|
||
total+=i;
|
||
i++;
|
||
}
|
||
print total;
|
||
}'
|
||
5050
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="-for循环"><a class="anchor" aria-hidden="true" tabindex="-1" href="#-for循环"><span class="octicon octicon-link"></span></a># for循环</h3>
|
||
<p>for循环有两种格式:</p>
|
||
<p>格式1:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1">for<span class="token punctuation">(</span>变量 <span class="token keyword">in</span> 数组<span class="token punctuation">)</span>
|
||
</span><span class="code-line line-number" line="2"> <span class="token punctuation">{</span>语句<span class="token punctuation">}</span>
|
||
</span></code><div onclick="copied(this)" data-code="for(变量 in 数组)
|
||
{语句}
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>示例:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{
|
||
</span></span><span class="code-line line-number" line="2"><span class="token string">for(k in ENVIRON){
|
||
</span></span><span class="code-line line-number" line="3"><span class="token string"> print k"="ENVIRON[k];
|
||
</span></span><span class="code-line line-number" line="4"><span class="token string">}
|
||
</span></span><span class="code-line line-number" line="5"><span class="token string">
|
||
</span></span><span class="code-line line-number" line="6"><span class="token string">}'</span>
|
||
</span><span class="code-line line-number" line="7"><span class="token assign-left variable"><span class="token environment constant">TERM</span></span><span class="token operator">=</span>linux
|
||
</span><span class="code-line line-number" line="8"><span class="token assign-left variable">G_BROKEN_FILENAMES</span><span class="token operator">=</span><span class="token number">1</span>
|
||
</span><span class="code-line line-number" line="9"><span class="token assign-left variable"><span class="token environment constant">SHLVL</span></span><span class="token operator">=</span><span class="token number">1</span>
|
||
</span><span class="code-line line-number" line="10"><span class="token assign-left variable">pwd</span><span class="token operator">=</span>/root/text
|
||
</span><span class="code-line line-number" line="11"><span class="token punctuation">..</span>.
|
||
</span><span class="code-line line-number" line="12"><span class="token assign-left variable">logname</span><span class="token operator">=</span>root
|
||
</span><span class="code-line line-number" line="13"><span class="token assign-left variable"><span class="token environment constant">HOME</span></span><span class="token operator">=</span>/root
|
||
</span><span class="code-line line-number" line="14"><span class="token assign-left variable">SSH_CLIENT</span><span class="token operator">=</span><span class="token number">192.168</span>.1.21 <span class="token number">53087</span> <span class="token number">22</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{
|
||
for(k in ENVIRON){
|
||
print k"="ENVIRON[k];
|
||
}
|
||
|
||
}'
|
||
TERM=linux
|
||
G_BROKEN_FILENAMES=1
|
||
SHLVL=1
|
||
pwd=/root/text
|
||
...
|
||
logname=root
|
||
HOME=/root
|
||
SSH_CLIENT=192.168.1.21 53087 22
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>注:ENVIRON是awk常量,是子典型数组。</p>
|
||
<p>格式2:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1">for<span class="token punctuation">(</span>变量<span class="token punctuation">;</span>条件<span class="token punctuation">;</span>表达式<span class="token punctuation">)</span>
|
||
</span><span class="code-line line-number" line="2"> <span class="token punctuation">{</span>语句<span class="token punctuation">}</span>
|
||
</span></code><div onclick="copied(this)" data-code="for(变量;条件;表达式)
|
||
{语句}
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>示例:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{
|
||
</span></span><span class="code-line line-number" line="2"><span class="token string">total=0;
|
||
</span></span><span class="code-line line-number" line="3"><span class="token string">for(i=0;i<=100;i++){
|
||
</span></span><span class="code-line line-number" line="4"><span class="token string"> total+=i;
|
||
</span></span><span class="code-line line-number" line="5"><span class="token string">}
|
||
</span></span><span class="code-line line-number" line="6"><span class="token string">print total;
|
||
</span></span><span class="code-line line-number" line="7"><span class="token string">}'</span>
|
||
</span><span class="code-line line-number" line="8"><span class="token number">5050</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{
|
||
total=0;
|
||
for(i=0;i<=100;i++){
|
||
total+=i;
|
||
}
|
||
print total;
|
||
}'
|
||
5050
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="-do循环"><a class="anchor" aria-hidden="true" tabindex="-1" href="#-do循环"><span class="octicon octicon-link"></span></a># do循环</h3>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token keyword">do</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token punctuation">{</span>语句<span class="token punctuation">}</span> while<span class="token punctuation">(</span>条件<span class="token punctuation">)</span>
|
||
</span></code><div onclick="copied(this)" data-code="do
|
||
{语句} while(条件)
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>例子:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{
|
||
</span></span><span class="code-line line-number" line="2"><span class="token string">total=0;
|
||
</span></span><span class="code-line line-number" line="3"><span class="token string">i=0;
|
||
</span></span><span class="code-line line-number" line="4"><span class="token string">do {total+=i;i++;} while(i<=100)
|
||
</span></span><span class="code-line line-number" line="5"><span class="token string"> print total;
|
||
</span></span><span class="code-line line-number" line="6"><span class="token string">}'</span>
|
||
</span><span class="code-line line-number" line="7"><span class="token number">5050</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{
|
||
total=0;
|
||
i=0;
|
||
do {total+=i;i++;} while(i<=100)
|
||
print total;
|
||
}'
|
||
5050
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="其他语句"><a class="anchor" aria-hidden="true" tabindex="-1" href="#其他语句"><span class="octicon octicon-link"></span></a>其他语句</h3>
|
||
<ul>
|
||
<li><strong>break</strong> 当 break 语句用于 while 或 for 语句时,导致退出程序循环。</li>
|
||
<li><strong>continue</strong> 当 continue 语句用于 while 或 for 语句时,使程序循环移动到下一个迭代。</li>
|
||
<li><strong>next</strong> 能能够导致读入下一个输入行,并返回到脚本的顶部。这可以避免对当前输入行执行其他的操作过程。</li>
|
||
<li><strong>exit</strong> 语句使主输入循环退出并将控制转移到END,如果END存在的话。如果没有定义END规则,或在END中应用exit语句,则终止脚本的执行。</li>
|
||
</ul>
|
||
<h2 id="数组应用"><a class="anchor" aria-hidden="true" tabindex="-1" href="#数组应用"><span class="octicon octicon-link"></span></a>数组应用</h2>
|
||
<p>数组是awk的灵魂,处理文本中最不能少的就是它的数组处理。因为数组索引(下标)可以是数字和字符串在awk中数组叫做关联数组(associative arrays)。awk 中的数组不必提前声明,也不必声明大小。数组元素用0或空字符串来初始化,这根据上下文而定。</p>
|
||
<h3 id="数组的定义"><a class="anchor" aria-hidden="true" tabindex="-1" href="#数组的定义"><span class="octicon octicon-link"></span></a>数组的定义</h3>
|
||
<p>数字做数组索引(下标):</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1">Array<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"sun"</span>
|
||
</span><span class="code-line line-number" line="2">Array<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"kai"</span>
|
||
</span></code><div onclick="copied(this)" data-code="Array[1]="sun"
|
||
Array[2]="kai"
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>字符串做数组索引(下标):</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1">Array<span class="token punctuation">[</span><span class="token string">"first"</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"www"</span>
|
||
</span><span class="code-line line-number" line="2">Array<span class="token punctuation">[</span><span class="token string">"last"</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"name"</span>
|
||
</span><span class="code-line line-number" line="3">Array<span class="token punctuation">[</span><span class="token string">"birth"</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"1987"</span>
|
||
</span></code><div onclick="copied(this)" data-code="Array["first"]="www"
|
||
Array["last"]="name"
|
||
Array["birth"]="1987"
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>使用中<code>print Array[1]</code>会打印出sun;使用<code>print Array[2]</code>会打印出kai;使用<code>print["birth"]</code>会得到1987。</p>
|
||
<p><strong>读取数组的值</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token punctuation">{</span> for<span class="token punctuation">(</span>item <span class="token keyword">in</span> array<span class="token punctuation">)</span> <span class="token punctuation">{</span>print array<span class="token punctuation">[</span>item<span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">#输出的顺序是随机的</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token punctuation">{</span> for<span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator"><=</span>len<span class="token punctuation">;</span>i++<span class="token punctuation">)</span> <span class="token punctuation">{</span>print array<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">#Len是数组的长度</span>
|
||
</span></code><div onclick="copied(this)" data-code="{ for(item in array) {print array[item]}; } #输出的顺序是随机的
|
||
{ for(i=1;i<=len;i++) {print array[i]}; } #Len是数组的长度
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="数组相关函数"><a class="anchor" aria-hidden="true" tabindex="-1" href="#数组相关函数"><span class="octicon octicon-link"></span></a>数组相关函数</h3>
|
||
<p><strong>得到数组长度:</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{info="it is a test";lens=split(info,tA," ");print length(tA),lens;}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">4</span> <span class="token number">4</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{info="it is a test";lens=split(info,tA," ");print length(tA),lens;}'
|
||
4 4
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>length返回字符串以及数组长度,split进行分割字符串为数组,也会返回分割得到数组长度。</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{info="it is a test";split(info,tA," ");print asort(tA);}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">4</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{info="it is a test";split(info,tA," ");print asort(tA);}'
|
||
4
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>asort对数组进行排序,返回数组长度。</p>
|
||
<p><strong>输出数组内容(无序,有序输出):</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{info="it is a test";split(info,tA," ");for(k in tA){print k,tA[k];}}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">4</span> <span class="token builtin class-name">test</span>
|
||
</span><span class="code-line line-number" line="3"><span class="token number">1</span> it
|
||
</span><span class="code-line line-number" line="4"><span class="token number">2</span> is
|
||
</span><span class="code-line line-number" line="5"><span class="token number">3</span> a
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{info="it is a test";split(info,tA," ");for(k in tA){print k,tA[k];}}'
|
||
4 test
|
||
1 it
|
||
2 is
|
||
3 a
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p><code>for…in</code>输出,因为数组是关联数组,默认是无序的。所以通过<code>for…in</code>得到是无序的数组。如果需要得到有序数组,需要通过下标获得。</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{info="it is a test";tlen=split(info,tA," ");for(k=1;k<=tlen;k++){print k,tA[k];}}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">1</span> it
|
||
</span><span class="code-line line-number" line="3"><span class="token number">2</span> is
|
||
</span><span class="code-line line-number" line="4"><span class="token number">3</span> a
|
||
</span><span class="code-line line-number" line="5"><span class="token number">4</span> <span class="token builtin class-name">test</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{info="it is a test";tlen=split(info,tA," ");for(k=1;k<=tlen;k++){print k,tA[k];}}'
|
||
1 it
|
||
2 is
|
||
3 a
|
||
4 test
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>注意:数组下标是从1开始,与C数组不一样。</p>
|
||
<p><strong>判断键值存在以及删除键值:</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token comment"># 错误的判断方法:</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token function">awk</span> <span class="token string">'BEGIN{tB["a"]="a1";tB["b"]="b1";if(tB["c"]!="1"){print "no found";};for(k in tB){print k,tB[k];}}'</span>
|
||
</span><span class="code-line line-number" line="3">no found
|
||
</span><span class="code-line line-number" line="4">a a1
|
||
</span><span class="code-line line-number" line="5">b b1
|
||
</span><span class="code-line line-number" line="6">c
|
||
</span></code><div onclick="copied(this)" data-code="# 错误的判断方法:
|
||
awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";if(tB["c"]!="1"){print "no found";};for(k in tB){print k,tB[k];}}'
|
||
no found
|
||
a a1
|
||
b b1
|
||
c
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>以上出现奇怪问题,<code>tB[“c”]</code>没有定义,但是循环时候,发现已经存在该键值,它的值为空,这里需要注意,awk数组是关联数组,只要通过数组引用它的key,就会自动创建改序列。</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token comment"># 正确判断方法:</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token function">awk</span> <span class="token string">'BEGIN{tB["a"]="a1";tB["b"]="b1";if( "c" in tB){print "ok";};for(k in tB){print k,tB[k];}}'</span>
|
||
</span><span class="code-line line-number" line="3">a a1
|
||
</span><span class="code-line line-number" line="4">b b1
|
||
</span></code><div onclick="copied(this)" data-code="# 正确判断方法:
|
||
awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";if( "c" in tB){print "ok";};for(k in tB){print k,tB[k];}}'
|
||
a a1
|
||
b b1
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p><code>if(key in array)</code>通过这种方法判断数组中是否包含<code>key</code>键值。</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token comment">#删除键值:</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token function">awk</span> <span class="token string">'BEGIN{tB["a"]="a1";tB["b"]="b1";delete tB["a"];for(k in tB){print k,tB[k];}}'</span>
|
||
</span><span class="code-line line-number" line="3">b b1
|
||
</span></code><div onclick="copied(this)" data-code="#删除键值:
|
||
awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";delete tB["a"];for(k in tB){print k,tB[k];}}'
|
||
b b1
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p><code>delete array[key]</code>可以删除,对应数组<code>key</code>的,序列值。</p>
|
||
<h3 id="二维多维数组使用"><a class="anchor" aria-hidden="true" tabindex="-1" href="#二维多维数组使用"><span class="octicon octicon-link"></span></a>二维、多维数组使用</h3>
|
||
<p>awk的多维数组在本质上是一维数组,更确切一点,awk在存储上并不支持多维数组。awk提供了逻辑上模拟二维数组的访问方式。例如,<code>array[2,4]=1</code>这样的访问是允许的。awk使用一个特殊的字符串<code>SUBSEP(\034)</code>作为分割字段,在上面的例子中,关联数组array存储的键值实际上是2\0344。</p>
|
||
<p>类似一维数组的成员测试,多维数组可以使用<code>if ( (i,j) in array)</code>这样的语法,但是下标必须放置在圆括号中。类似一维数组的循环访问,多维数组使用<code>for ( item in array )</code>这样的语法遍历数组。与一维数组不同的是,多维数组必须使用<code>split()</code>函数来访问单独的下标分量。</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{
|
||
</span></span><span class="code-line line-number" line="2"><span class="token string">for(i=1;i<=9;i++){
|
||
</span></span><span class="code-line line-number" line="3"><span class="token string"> for(j=1;j<=9;j++){
|
||
</span></span><span class="code-line line-number" line="4"><span class="token string"> tarr[i,j]=i*j; print i,"*",j,"=",tarr[i,j];
|
||
</span></span><span class="code-line line-number" line="5"><span class="token string"> }
|
||
</span></span><span class="code-line line-number" line="6"><span class="token string">}
|
||
</span></span><span class="code-line line-number" line="7"><span class="token string">}'</span>
|
||
</span><span class="code-line line-number" line="8"><span class="token number">1</span> * <span class="token number">1</span> <span class="token operator">=</span> <span class="token number">1</span>
|
||
</span><span class="code-line line-number" line="9"><span class="token number">1</span> * <span class="token number">2</span> <span class="token operator">=</span> <span class="token number">2</span>
|
||
</span><span class="code-line line-number" line="10"><span class="token number">1</span> * <span class="token number">3</span> <span class="token operator">=</span> <span class="token number">3</span>
|
||
</span><span class="code-line line-number" line="11"><span class="token number">1</span> * <span class="token number">4</span> <span class="token operator">=</span> <span class="token number">4</span>
|
||
</span><span class="code-line line-number" line="12"><span class="token number">1</span> * <span class="token number">5</span> <span class="token operator">=</span> <span class="token number">5</span>
|
||
</span><span class="code-line line-number" line="13"><span class="token number">1</span> * <span class="token number">6</span> <span class="token operator">=</span> <span class="token number">6</span>
|
||
</span><span class="code-line line-number" line="14"><span class="token punctuation">..</span>.
|
||
</span><span class="code-line line-number" line="15"><span class="token number">9</span> * <span class="token number">6</span> <span class="token operator">=</span> <span class="token number">54</span>
|
||
</span><span class="code-line line-number" line="16"><span class="token number">9</span> * <span class="token number">7</span> <span class="token operator">=</span> <span class="token number">63</span>
|
||
</span><span class="code-line line-number" line="17"><span class="token number">9</span> * <span class="token number">8</span> <span class="token operator">=</span> <span class="token number">72</span>
|
||
</span><span class="code-line line-number" line="18"><span class="token number">9</span> * <span class="token number">9</span> <span class="token operator">=</span> <span class="token number">81</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{
|
||
for(i=1;i<=9;i++){
|
||
for(j=1;j<=9;j++){
|
||
tarr[i,j]=i*j; print i,"*",j,"=",tarr[i,j];
|
||
}
|
||
}
|
||
}'
|
||
1 * 1 = 1
|
||
1 * 2 = 2
|
||
1 * 3 = 3
|
||
1 * 4 = 4
|
||
1 * 5 = 5
|
||
1 * 6 = 6
|
||
...
|
||
9 * 6 = 54
|
||
9 * 7 = 63
|
||
9 * 8 = 72
|
||
9 * 9 = 81
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>可以通过<code>array[k,k2]</code>引用获得数组内容。</p>
|
||
<p>另一种方法:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{
|
||
</span></span><span class="code-line line-number" line="2"><span class="token string">for(i=1;i<=9;i++){
|
||
</span></span><span class="code-line line-number" line="3"><span class="token string"> for(j=1;j<=9;j++){
|
||
</span></span><span class="code-line line-number" line="4"><span class="token string"> tarr[i,j]=i*j;
|
||
</span></span><span class="code-line line-number" line="5"><span class="token string"> }
|
||
</span></span><span class="code-line line-number" line="6"><span class="token string">}
|
||
</span></span><span class="code-line line-number" line="7"><span class="token string">for(m in tarr){
|
||
</span></span><span class="code-line line-number" line="8"><span class="token string"> split(m,tarr2,SUBSEP); print tarr2[1],"*",tarr2[2],"=",tarr[m];
|
||
</span></span><span class="code-line line-number" line="9"><span class="token string">}
|
||
</span></span><span class="code-line line-number" line="10"><span class="token string">}'</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{
|
||
for(i=1;i<=9;i++){
|
||
for(j=1;j<=9;j++){
|
||
tarr[i,j]=i*j;
|
||
}
|
||
}
|
||
for(m in tarr){
|
||
split(m,tarr2,SUBSEP); print tarr2[1],"*",tarr2[2],"=",tarr[m];
|
||
}
|
||
}'
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h2 id="内置函数"><a class="anchor" aria-hidden="true" tabindex="-1" href="#内置函数"><span class="octicon octicon-link"></span></a>内置函数</h2>
|
||
<p>awk内置函数,主要分以下3种类似:算数函数、字符串函数、其它一般函数、时间函数。</p>
|
||
<h3 id="算术函数"><a class="anchor" aria-hidden="true" tabindex="-1" href="#算术函数"><span class="octicon octicon-link"></span></a>算术函数</h3>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>格式</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>atan2( y, x )</td>
|
||
<td>返回 y/x 的反正切。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>cos( x )</td>
|
||
<td>返回 x 的余弦;x 是弧度。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>sin( x )</td>
|
||
<td>返回 x 的正弦;x 是弧度。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>exp( x )</td>
|
||
<td>返回 x 幂函数。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>log( x )</td>
|
||
<td>返回 x 的自然对数。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>sqrt( x )</td>
|
||
<td>返回 x 平方根。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>int( x )</td>
|
||
<td>返回 x 的截断至整数的值。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>rand( )</td>
|
||
<td>返回任意数字 n,其中 0 <= n < 1。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>srand( [expr] )</td>
|
||
<td>将 rand 函数的种子值设置为 Expr 参数的值,或如果省略 Expr 参数则使用某天的时间。返回先前的种子值。</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>举例说明:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{OFMT="%.3f";fs=sin(1);fe=exp(10);fl=log(10);fi=int(3.1415);print fs,fe,fl,fi;}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">0.841</span> <span class="token number">22026.466</span> <span class="token number">2.303</span> <span class="token number">3</span>
|
||
</span><span class="code-line line-number" line="3">
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{OFMT="%.3f";fs=sin(1);fe=exp(10);fl=log(10);fi=int(3.1415);print fs,fe,fl,fi;}'
|
||
0.841 22026.466 2.303 3
|
||
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>OFMT 设置输出数据格式是保留3位小数。</p>
|
||
<p>获得随机数:</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{srand();fr=int(100*rand());print fr;}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">78</span>
|
||
</span><span class="code-line line-number" line="3"><span class="token function">awk</span> <span class="token string">'BEGIN{srand();fr=int(100*rand());print fr;}'</span>
|
||
</span><span class="code-line line-number" line="4"><span class="token number">31</span>
|
||
</span><span class="code-line line-number" line="5"><span class="token function">awk</span> <span class="token string">'BEGIN{srand();fr=int(100*rand());print fr;}'</span>
|
||
</span><span class="code-line line-number" line="6"><span class="token number">41</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{srand();fr=int(100*rand());print fr;}'
|
||
78
|
||
awk 'BEGIN{srand();fr=int(100*rand());print fr;}'
|
||
31
|
||
awk 'BEGIN{srand();fr=int(100*rand());print fr;}'
|
||
41
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="字符串函数"><a class="anchor" aria-hidden="true" tabindex="-1" href="#字符串函数"><span class="octicon octicon-link"></span></a>字符串函数</h3>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>格式</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>gsub( Ere, Repl, [ In ] )</td>
|
||
<td>除了正则表达式所有具体值被替代这点,它和 sub 函数完全一样地执行。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>sub( Ere, Repl, [ In ] )</td>
|
||
<td>用 Repl 参数指定的字符串替换 In 参数指定的字符串中的由 Ere 参数指定的扩展正则表达式的第一个具体值。sub 函数返回替换的数量。出现在 Repl 参数指定的字符串中的 &(和符号)由 In 参数指定的与 Ere 参数的指定的扩展正则表达式匹配的字符串替换。如果未指定 In 参数,缺省值是整个记录($0 记录变量)。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>index( String1, String2 )</td>
|
||
<td>在由 String1 参数指定的字符串(其中有出现 String2 指定的参数)中,返回位置,从 1 开始编号。如果 String2 参数不在 String1 参数中出现,则返回 0(零)。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>length [(String)]</td>
|
||
<td>返回 String 参数指定的字符串的长度(字符形式)。如果未给出 String 参数,则返回整个记录的长度($0 记录变量)。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>blength [(String)]</td>
|
||
<td>返回 String 参数指定的字符串的长度(以字节为单位)。如果未给出 String 参数,则返回整个记录的长度($0 记录变量)。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>substr( String, M, [ N ] )</td>
|
||
<td>返回具有 N 参数指定的字符数量子串。子串从 String 参数指定的字符串取得,其字符以 M 参数指定的位置开始。M 参数指定为将 String 参数中的第一个字符作为编号 1。如果未指定 N 参数,则子串的长度将是 M 参数指定的位置到 String 参数的末尾 的长度。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>match( String, Ere )</td>
|
||
<td>在 String 参数指定的字符串(Ere 参数指定的扩展正则表达式出现在其中)中返回位置(字符形式),从 1 开始编号,或如果 Ere 参数不出现,则返回 0(零)。RSTART 特殊变量设置为返回值。RLENGTH 特殊变量设置为匹配的字符串的长度,或如果未找到任何匹配,则设置为 -1(负一)。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>split( String, A, [Ere] )</td>
|
||
<td>将 String 参数指定的参数分割为数组元素 A[1], A[2], . . ., A[n],并返回 n 变量的值。此分隔可以通过 Ere 参数指定的扩展正则表达式进行,或用当前字段分隔符(FS 特殊变量)来进行(如果没有给出 Ere 参数)。除非上下文指明特定的元素还应具有一个数字值,否则 A 数组中的元素用字符串值来创建。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>tolower( String )</td>
|
||
<td>返回 String 参数指定的字符串,字符串中每个大写字符将更改为小写。大写和小写的映射由当前语言环境的 LC_CTYPE 范畴定义。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>toupper( String )</td>
|
||
<td>返回 String 参数指定的字符串,字符串中每个小写字符将更改为大写。大写和小写的映射由当前语言环境的 LC_CTYPE 范畴定义。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>sprintf(Format, Expr, Expr, . . . )</td>
|
||
<td>根据 Format 参数指定的 printf 子例程格式字符串来格式化 Expr 参数指定的表达式并返回最后生成的字符串。</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>注:Ere都可以是正则表达式。</p>
|
||
<p><strong>gsub,sub使用</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{info="this is a test2010test!";gsub(/[0-9]+/,"!",info);print info}'</span>
|
||
</span><span class="code-line line-number" line="2">this is a test<span class="token operator">!</span>test<span class="token operator">!</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{info="this is a test2010test!";gsub(/[0-9]+/,"!",info);print info}'
|
||
this is a test!test!
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>在 info中查找满足正则表达式,<code>/[0-9]+/</code> 用<code>””</code>替换,并且替换后的值,赋值给info 未给info值,默认是<code>$0</code></p>
|
||
<p><strong>查找字符串(index使用)</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{info="this is a test2010test!";print index(info,"test")?"ok":"no found";}'</span>
|
||
</span><span class="code-line line-number" line="2">ok
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{info="this is a test2010test!";print index(info,"test")?"ok":"no found";}'
|
||
ok
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>未找到,返回0</p>
|
||
<p><strong>正则表达式匹配查找(match使用)</strong></p>
|
||
<pre><code class="code-highlight"><span class="code-line line-number" line="1">awk 'BEGIN{info="this is a test2010test!";print match(info,/[0-9]+/)?"ok":"no found";}'
|
||
</span><span class="code-line line-number" line="2">ok
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{info="this is a test2010test!";print match(info,/[0-9]+/)?"ok":"no found";}'
|
||
ok
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p><strong>截取字符串(substr使用)</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token punctuation">[</span>wangsl@centos5 ~<span class="token punctuation">]</span>$ <span class="token function">awk</span> <span class="token string">'BEGIN{info="this is a test2010test!";print substr(info,4,10);}'</span>
|
||
</span><span class="code-line line-number" line="2">s is a tes
|
||
</span></code><div onclick="copied(this)" data-code="[wangsl@centos5 ~]$ awk 'BEGIN{info="this is a test2010test!";print substr(info,4,10);}'
|
||
s is a tes
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>从第 4个 字符开始,截取10个长度字符串</p>
|
||
<p><strong>字符串分割(split使用)</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{info="this is a test";split(info,tA," ");print length(tA);for(k in tA){print k,tA[k];}}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">4</span>
|
||
</span><span class="code-line line-number" line="3"><span class="token number">4</span> <span class="token builtin class-name">test</span>
|
||
</span><span class="code-line line-number" line="4"><span class="token number">1</span> this
|
||
</span><span class="code-line line-number" line="5"><span class="token number">2</span> is
|
||
</span><span class="code-line line-number" line="6"><span class="token number">3</span> a
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{info="this is a test";split(info,tA," ");print length(tA);for(k in tA){print k,tA[k];}}'
|
||
4
|
||
4 test
|
||
1 this
|
||
2 is
|
||
3 a
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>分割info,动态创建数组tA,这里比较有意思,<code>awk for …in</code>循环,是一个无序的循环。 并不是从数组下标1…n ,因此使用时候需要注意。</p>
|
||
<p><strong>格式化字符串输出(sprintf使用)</strong></p>
|
||
<p>格式化字符串格式:</p>
|
||
<p>其中格式化字符串包括两部分内容:一部分是正常字符,这些字符将按原样输出; 另一部分是格式化规定字符,以<code>"%"</code>开始,后跟一个或几个规定字符,用来确定输出内容格式。</p>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>格式</th>
|
||
<th>描述</th>
|
||
<th>格式</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>%d</td>
|
||
<td>十进制有符号整数</td>
|
||
<td>%u</td>
|
||
<td>十进制无符号整数</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%f</td>
|
||
<td>浮点数</td>
|
||
<td>%s</td>
|
||
<td>字符串</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%c</td>
|
||
<td>单个字符</td>
|
||
<td>%p</td>
|
||
<td>指针的值</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%e</td>
|
||
<td>指数形式的浮点数</td>
|
||
<td>%x</td>
|
||
<td>%X 无符号以十六进制表示的整数</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%o</td>
|
||
<td>无符号以八进制表示的整数</td>
|
||
<td>%g</td>
|
||
<td>自动选择合适的表示法</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{n1=124.113;n2=-1.224;n3=1.2345; printf("%.2f,%.2u,%.2g,%X,%on",n1,n2,n3,n1,n1);}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">124.11</span>,18446744073709551615,1.2,7C,174
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{n1=124.113;n2=-1.224;n3=1.2345; printf("%.2f,%.2u,%.2g,%X,%on",n1,n2,n3,n1,n1);}'
|
||
124.11,18446744073709551615,1.2,7C,174
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<h3 id="一般函数"><a class="anchor" aria-hidden="true" tabindex="-1" href="#一般函数"><span class="octicon octicon-link"></span></a>一般函数</h3>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>格式</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>close( Expression )</td>
|
||
<td>用同一个带字符串值的 Expression 参数来关闭由 print 或 printf 语句打开的或调用 getline 函数打开的文件或管道。如果文件或管道成功关闭,则返回 0;其它情况下返回非零值。如果打算写一个文件,并稍后在同一个程序中读取文件,则 close 语句是必需的。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>system(command )</td>
|
||
<td>执行 Command 参数指定的命令,并返回退出状态。等同于 system 子例程。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>Expression <code>|</code> getline [ Variable ]</td>
|
||
<td>从来自 Expression 参数指定的命令的输出中通过管道传送的流中读取一个输入记录,并将该记录的值指定给 Variable 参数指定的变量。如果当前未打开将 Expression 参数的值作为其命令名称的流,则创建流。创建的流等同于调用 popen 子例程,此时 Command 参数取 Expression 参数的值且 Mode 参数设置为一个是 r 的值。只要流保留打开且 Expression 参数求得同一个字符串,则对 getline 函数的每次后续调用读取另一个记录。如果未指定 Variable 参数,则 $0 记录变量和 NF 特殊变量设置为从流读取的记录。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>getline [ Variable ] < Expression</td>
|
||
<td>从 Expression 参数指定的文件读取输入的下一个记录,并将 Variable 参数指定的变量设置为该记录的值。只要流保留打开且 Expression 参数对同一个字符串求值,则对 getline 函数的每次后续调用读取另一个记录。如果未指定 Variable 参数,则 $0 记录变量和 NF 特殊变量设置为从流读取的记录。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>getline [ Variable ]</td>
|
||
<td>将 Variable 参数指定的变量设置为从当前输入文件读取的下一个输入记录。如果未指定 Variable 参数,则 $0 记录变量设置为该记录的值,还将设置 NF、NR 和 FNR 特殊变量。</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p><strong>打开外部文件(close用法)</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{while("cat /etc/passwd"|getline){print $0;};close("/etc/passwd");}'</span>
|
||
</span><span class="code-line line-number" line="2">root:x:0:0:root:/root:/bin/bash
|
||
</span><span class="code-line line-number" line="3">bin:x:1:1:bin:/bin:/sbin/nologin
|
||
</span><span class="code-line line-number" line="4">daemon:x:2:2:daemon:/sbin:/sbin/nologin
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{while("cat /etc/passwd"|getline){print $0;};close("/etc/passwd");}'
|
||
root:x:0:0:root:/root:/bin/bash
|
||
bin:x:1:1:bin:/bin:/sbin/nologin
|
||
daemon:x:2:2:daemon:/sbin:/sbin/nologin
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p><strong>逐行读取外部文件(getline使用方法)</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{while(getline < "/etc/passwd"){print $0;};close("/etc/passwd");}'</span>
|
||
</span><span class="code-line line-number" line="2">root:x:0:0:root:/root:/bin/bash
|
||
</span><span class="code-line line-number" line="3">bin:x:1:1:bin:/bin:/sbin/nologin
|
||
</span><span class="code-line line-number" line="4">daemon:x:2:2:daemon:/sbin:/sbin/nologin
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{while(getline < "/etc/passwd"){print $0;};close("/etc/passwd");}'
|
||
root:x:0:0:root:/root:/bin/bash
|
||
bin:x:1:1:bin:/bin:/sbin/nologin
|
||
daemon:x:2:2:daemon:/sbin:/sbin/nologin
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{print "Enter your name:";getline name;print name;}'</span>
|
||
</span><span class="code-line line-number" line="2">Enter your name:
|
||
</span><span class="code-line line-number" line="3">chengmo
|
||
</span><span class="code-line line-number" line="4">chengmo
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{print "Enter your name:";getline name;print name;}'
|
||
Enter your name:
|
||
chengmo
|
||
chengmo
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p><strong>调用外部应用程序(system使用方法)</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{b=system("ls -al");print b;}'</span>
|
||
</span><span class="code-line line-number" line="2">total <span class="token number">42092</span>
|
||
</span><span class="code-line line-number" line="3">drwxr-xr-x <span class="token number">14</span> chengmo chengmo <span class="token number">4096</span> 09-30 <span class="token number">17</span>:47 <span class="token builtin class-name">.</span>
|
||
</span><span class="code-line line-number" line="4">drwxr-xr-x <span class="token number">95</span> root root <span class="token number">4096</span> <span class="token number">10</span>-08 <span class="token number">14</span>:01 <span class="token punctuation">..</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{b=system("ls -al");print b;}'
|
||
total 42092
|
||
drwxr-xr-x 14 chengmo chengmo 4096 09-30 17:47 .
|
||
drwxr-xr-x 95 root root 4096 10-08 14:01 ..
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>b返回值,是执行结果。</p>
|
||
<h3 id="时间函数"><a class="anchor" aria-hidden="true" tabindex="-1" href="#时间函数"><span class="octicon octicon-link"></span></a>时间函数</h3>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>格式</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>函数名</td>
|
||
<td>说明</td>
|
||
</tr>
|
||
<tr>
|
||
<td>mktime( YYYY MM dd HH MM ss[ DST])</td>
|
||
<td>生成时间格式</td>
|
||
</tr>
|
||
<tr>
|
||
<td>strftime([format [, timestamp]])</td>
|
||
<td>格式化时间输出,将时间戳转为时间字符串具体格式,见下表。</td>
|
||
</tr>
|
||
<tr>
|
||
<td>systime()</td>
|
||
<td>得到时间戳,返回从1970年1月1日开始到当前时间(不计闰年)的整秒数</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p><strong>建指定时间(mktime使用)</strong></p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{tstamp=mktime("2001 01 01 12 12 12");print strftime("%c",tstamp);}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">2001</span>年01月01日 星期一 <span class="token number">12</span>时12分12秒
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{tstamp=mktime("2001 01 01 12 12 12");print strftime("%c",tstamp);}'
|
||
2001年01月01日 星期一 12时12分12秒
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{tstamp1=mktime("2001 01 01 12 12 12");tstamp2=mktime("2001 02 01 0 0 0");print tstamp2-tstamp1;}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">2634468</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{tstamp1=mktime("2001 01 01 12 12 12");tstamp2=mktime("2001 02 01 0 0 0");print tstamp2-tstamp1;}'
|
||
2634468
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p>求2个时间段中间时间差,介绍了strftime使用方法</p>
|
||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line line-number" line="1"><span class="token function">awk</span> <span class="token string">'BEGIN{tstamp1=mktime("2001 01 01 12 12 12");tstamp2=systime();print tstamp2-tstamp1;}'</span>
|
||
</span><span class="code-line line-number" line="2"><span class="token number">308201392</span>
|
||
</span></code><div onclick="copied(this)" data-code="awk 'BEGIN{tstamp1=mktime("2001 01 01 12 12 12");tstamp2=systime();print tstamp2-tstamp1;}'
|
||
308201392
|
||
" class="copied"><svg class="octicon-copy" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg><svg class="octicon-check" aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" height="12" width="12"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg></div></pre>
|
||
<p><strong>strftime日期和时间格式说明符</strong></p>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>格式</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>%a</td>
|
||
<td>星期几的缩写(Sun)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%A</td>
|
||
<td>星期几的完整写法(Sunday)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%b</td>
|
||
<td>月名的缩写(Oct)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%B</td>
|
||
<td>月名的完整写法(October)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%c</td>
|
||
<td>本地日期和时间</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%d</td>
|
||
<td>十进制日期</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%D</td>
|
||
<td>日期 08/20/99</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%e</td>
|
||
<td>日期,如果只有一位会补上一个空格</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%H</td>
|
||
<td>用十进制表示24小时格式的小时</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%I</td>
|
||
<td>用十进制表示12小时格式的小时</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%j</td>
|
||
<td>从1月1日起一年中的第几天</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%m</td>
|
||
<td>十进制表示的月份</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%M</td>
|
||
<td>十进制表示的分钟</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%p</td>
|
||
<td>12小时表示法(AM/PM)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%S</td>
|
||
<td>十进制表示的秒</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%U</td>
|
||
<td>十进制表示的一年中的第几个星期(星期天作为一个星期的开始)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%w</td>
|
||
<td>十进制表示的星期几(星期天是0)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%W</td>
|
||
<td>十进制表示的一年中的第几个星期(星期一作为一个星期的开始)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%x</td>
|
||
<td>重新设置本地日期(08/20/99)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%X</td>
|
||
<td>重新设置本地时间(12:00:00)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%y</td>
|
||
<td>两位数字表示的年(99)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%Y</td>
|
||
<td>当前月份</td>
|
||
</tr>
|
||
<tr>
|
||
<td>%%</td>
|
||
<td>百分号(%)</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<style>markdown-style pre .copied {
|
||
display: flex;
|
||
position: absolute;
|
||
cursor: pointer;
|
||
color: #a5afbb;
|
||
top: 6px;
|
||
right: 6px;
|
||
border-radius: 5px;
|
||
background: #82828226;
|
||
padding: 6px;
|
||
font-size: 12px;
|
||
transition: all .3s;
|
||
}
|
||
markdown-style pre .copied:not(.active) {
|
||
visibility: hidden;
|
||
}
|
||
markdown-style pre:hover .copied {
|
||
visibility: visible;
|
||
}
|
||
markdown-style pre:hover .copied:hover {
|
||
background: #4caf50;
|
||
color: #fff;
|
||
}
|
||
markdown-style pre:hover .copied:active,
|
||
markdown-style pre .copied.active {
|
||
background: #2e9b33;
|
||
color: #fff;
|
||
}
|
||
markdown-style pre .copied .octicon-copy {
|
||
display: block;
|
||
}
|
||
markdown-style pre .copied .octicon-check {
|
||
display: none;
|
||
}
|
||
markdown-style pre .active .octicon-copy {
|
||
display: none;
|
||
}
|
||
markdown-style pre .active .octicon-check {
|
||
display: block;
|
||
}</style><script>/*! @uiw/copy-to-clipboard v1.0.12 | MIT (c) 2021 Kenny Wang | https://github.com/uiwjs/copy-to-clipboard.git */
|
||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).copyTextToClipboard=t()}(this,(function(){"use strict";return function(e,t){const o=document.createElement("textarea");o.value=e,o.setAttribute("readonly",""),o.style={position:"absolute",left:"-9999px"},document.body.appendChild(o);const n=document.getSelection().rangeCount>0&&document.getSelection().getRangeAt(0);o.select();let c=!1;try{c=!!document.execCommand("copy")}catch(e){c=!1}document.body.removeChild(o),n&&document.getSelection&&(document.getSelection().removeAllRanges(),document.getSelection().addRange(n)),t&&t(c)}}));
|
||
|
||
function copied(target, str) {
|
||
target.classList.add('active');
|
||
copyTextToClipboard(target.dataset.code, function() {
|
||
setTimeout(() => {
|
||
target.classList.remove('active');
|
||
}, 2000);
|
||
});
|
||
}</script></markdown-style>
|
||
|
||
</div>
|
||
<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ -->
|
||
<div class="footer ">
|
||
<a target="_blank" href="https://github.com/jaywcjlove/linux-command/new/master/command">添加命令</a> |
|
||
<a href="../hot.html">命令列表</a> |
|
||
<a href="https://github.com/jaywcjlove/oscnews" target="_blank">Chrome 插件</a> |
|
||
<a href="https://github.com/jaywcjlove/linux-command/releases" target="_blank">Alfred</a> |
|
||
<a href="https://jaywcjlove.github.io/linux-command/linux-command.docset.zip" target="_blank">Dash</a> |
|
||
<a href="https://github.com/roachsinai/krunner-linuxcommands" target="_blank">Krunner</a> |
|
||
<a href="http://jaywcjlove.gitee.io/linux-command/" target="_blank">开源中国Web版</a>
|
||
<br />
|
||
<a href="https://jaywcjlove.github.io/#/sponsor" target="_blank">打赏捐赠</a> |
|
||
<a href="https://github.com/jaywcjlove/linux-command" target="_blank">Github</a> |
|
||
<a href="https://jaywcjlove.github.io/linux-command/" target="_blank">短地址:https://git.io/linux</a>
|
||
<div>
|
||
收藏本站请使用 Ctrl+D 或者Command+d
|
||
<br />
|
||
共搜集到
|
||
<span id="commands_info">
|
||
611
|
||
</span> 个Linux命令,超过 <a href="../contributors.html">50+</a> 贡献者
|
||
</div>
|
||
<div>
|
||
<a target="_blank" href="../contributors.html#镜像网站">镜像网站</a>列表,
|
||
<a href="https://github.com/jaywcjlove/linux-command/issues/649" target="_blank">推荐</a>自己的镜像网站
|
||
</div>
|
||
</div>
|
||
<script type="text/javascript" src="../js/dt.js?v=1752678601389"></script>
|
||
<script type="text/javascript" src="../js/index.js?v=1752678601389"></script>
|
||
</body>
|
||
</html> |