awk.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  5. <head>
  6. <!-- 2017-11-11 sáb 12:25 -->
  7. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  8. <meta name="viewport" content="width=device-width, initial-scale=1" />
  9. <title>AWK</title>
  10. <meta name="generator" content="Org mode" />
  11. <style type="text/css">
  12. <!--/*--><![CDATA[/*><!--*/
  13. .title { text-align: center;
  14. margin-bottom: .2em; }
  15. .subtitle { text-align: center;
  16. font-size: medium;
  17. font-weight: bold;
  18. margin-top:0; }
  19. .todo { font-family: monospace; color: red; }
  20. .done { font-family: monospace; color: green; }
  21. .priority { font-family: monospace; color: orange; }
  22. .tag { background-color: #eee; font-family: monospace;
  23. padding: 2px; font-size: 80%; font-weight: normal; }
  24. .timestamp { color: #bebebe; }
  25. .timestamp-kwd { color: #5f9ea0; }
  26. .org-right { margin-left: auto; margin-right: 0px; text-align: right; }
  27. .org-left { margin-left: 0px; margin-right: auto; text-align: left; }
  28. .org-center { margin-left: auto; margin-right: auto; text-align: center; }
  29. .underline { text-decoration: underline; }
  30. #postamble p, #preamble p { font-size: 90%; margin: .2em; }
  31. p.verse { margin-left: 3%; }
  32. pre {
  33. border: 1px solid #ccc;
  34. box-shadow: 3px 3px 3px #eee;
  35. padding: 8pt;
  36. font-family: monospace;
  37. overflow: auto;
  38. margin: 1.2em;
  39. }
  40. pre.src {
  41. position: relative;
  42. overflow: visible;
  43. padding-top: 1.2em;
  44. }
  45. pre.src:before {
  46. display: none;
  47. position: absolute;
  48. background-color: white;
  49. top: -10px;
  50. right: 10px;
  51. padding: 3px;
  52. border: 1px solid black;
  53. }
  54. pre.src:hover:before { display: inline;}
  55. /* Languages per Org manual */
  56. pre.src-asymptote:before { content: 'Asymptote'; }
  57. pre.src-awk:before { content: 'Awk'; }
  58. pre.src-C:before { content: 'C'; }
  59. /* pre.src-C++ doesn't work in CSS */
  60. pre.src-clojure:before { content: 'Clojure'; }
  61. pre.src-css:before { content: 'CSS'; }
  62. pre.src-D:before { content: 'D'; }
  63. pre.src-ditaa:before { content: 'ditaa'; }
  64. pre.src-dot:before { content: 'Graphviz'; }
  65. pre.src-calc:before { content: 'Emacs Calc'; }
  66. pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
  67. pre.src-fortran:before { content: 'Fortran'; }
  68. pre.src-gnuplot:before { content: 'gnuplot'; }
  69. pre.src-haskell:before { content: 'Haskell'; }
  70. pre.src-hledger:before { content: 'hledger'; }
  71. pre.src-java:before { content: 'Java'; }
  72. pre.src-js:before { content: 'Javascript'; }
  73. pre.src-latex:before { content: 'LaTeX'; }
  74. pre.src-ledger:before { content: 'Ledger'; }
  75. pre.src-lisp:before { content: 'Lisp'; }
  76. pre.src-lilypond:before { content: 'Lilypond'; }
  77. pre.src-lua:before { content: 'Lua'; }
  78. pre.src-matlab:before { content: 'MATLAB'; }
  79. pre.src-mscgen:before { content: 'Mscgen'; }
  80. pre.src-ocaml:before { content: 'Objective Caml'; }
  81. pre.src-octave:before { content: 'Octave'; }
  82. pre.src-org:before { content: 'Org mode'; }
  83. pre.src-oz:before { content: 'OZ'; }
  84. pre.src-plantuml:before { content: 'Plantuml'; }
  85. pre.src-processing:before { content: 'Processing.js'; }
  86. pre.src-python:before { content: 'Python'; }
  87. pre.src-R:before { content: 'R'; }
  88. pre.src-ruby:before { content: 'Ruby'; }
  89. pre.src-sass:before { content: 'Sass'; }
  90. pre.src-scheme:before { content: 'Scheme'; }
  91. pre.src-screen:before { content: 'Gnu Screen'; }
  92. pre.src-sed:before { content: 'Sed'; }
  93. pre.src-sh:before { content: 'shell'; }
  94. pre.src-sql:before { content: 'SQL'; }
  95. pre.src-sqlite:before { content: 'SQLite'; }
  96. /* additional languages in org.el's org-babel-load-languages alist */
  97. pre.src-forth:before { content: 'Forth'; }
  98. pre.src-io:before { content: 'IO'; }
  99. pre.src-J:before { content: 'J'; }
  100. pre.src-makefile:before { content: 'Makefile'; }
  101. pre.src-maxima:before { content: 'Maxima'; }
  102. pre.src-perl:before { content: 'Perl'; }
  103. pre.src-picolisp:before { content: 'Pico Lisp'; }
  104. pre.src-scala:before { content: 'Scala'; }
  105. pre.src-shell:before { content: 'Shell Script'; }
  106. pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
  107. /* additional language identifiers per "defun org-babel-execute"
  108. in ob-*.el */
  109. pre.src-cpp:before { content: 'C++'; }
  110. pre.src-abc:before { content: 'ABC'; }
  111. pre.src-coq:before { content: 'Coq'; }
  112. pre.src-groovy:before { content: 'Groovy'; }
  113. /* additional language identifiers from org-babel-shell-names in
  114. ob-shell.el: ob-shell is the only babel language using a lambda to put
  115. the execution function name together. */
  116. pre.src-bash:before { content: 'bash'; }
  117. pre.src-csh:before { content: 'csh'; }
  118. pre.src-ash:before { content: 'ash'; }
  119. pre.src-dash:before { content: 'dash'; }
  120. pre.src-ksh:before { content: 'ksh'; }
  121. pre.src-mksh:before { content: 'mksh'; }
  122. pre.src-posh:before { content: 'posh'; }
  123. /* Additional Emacs modes also supported by the LaTeX listings package */
  124. pre.src-ada:before { content: 'Ada'; }
  125. pre.src-asm:before { content: 'Assembler'; }
  126. pre.src-caml:before { content: 'Caml'; }
  127. pre.src-delphi:before { content: 'Delphi'; }
  128. pre.src-html:before { content: 'HTML'; }
  129. pre.src-idl:before { content: 'IDL'; }
  130. pre.src-mercury:before { content: 'Mercury'; }
  131. pre.src-metapost:before { content: 'MetaPost'; }
  132. pre.src-modula-2:before { content: 'Modula-2'; }
  133. pre.src-pascal:before { content: 'Pascal'; }
  134. pre.src-ps:before { content: 'PostScript'; }
  135. pre.src-prolog:before { content: 'Prolog'; }
  136. pre.src-simula:before { content: 'Simula'; }
  137. pre.src-tcl:before { content: 'tcl'; }
  138. pre.src-tex:before { content: 'TeX'; }
  139. pre.src-plain-tex:before { content: 'Plain TeX'; }
  140. pre.src-verilog:before { content: 'Verilog'; }
  141. pre.src-vhdl:before { content: 'VHDL'; }
  142. pre.src-xml:before { content: 'XML'; }
  143. pre.src-nxml:before { content: 'XML'; }
  144. /* add a generic configuration mode; LaTeX export needs an additional
  145. (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
  146. pre.src-conf:before { content: 'Configuration File'; }
  147. table { border-collapse:collapse; }
  148. caption.t-above { caption-side: top; }
  149. caption.t-bottom { caption-side: bottom; }
  150. td, th { vertical-align:top; }
  151. th.org-right { text-align: center; }
  152. th.org-left { text-align: center; }
  153. th.org-center { text-align: center; }
  154. td.org-right { text-align: right; }
  155. td.org-left { text-align: left; }
  156. td.org-center { text-align: center; }
  157. dt { font-weight: bold; }
  158. .footpara { display: inline; }
  159. .footdef { margin-bottom: 1em; }
  160. .figure { padding: 1em; }
  161. .figure p { text-align: center; }
  162. .inlinetask {
  163. padding: 10px;
  164. border: 2px solid gray;
  165. margin: 10px;
  166. background: #ffffcc;
  167. }
  168. #org-div-home-and-up
  169. { text-align: right; font-size: 70%; white-space: nowrap; }
  170. textarea { overflow-x: auto; }
  171. .linenr { font-size: smaller }
  172. .code-highlighted { background-color: #ffff00; }
  173. .org-info-js_info-navigation { border-style: none; }
  174. #org-info-js_console-label
  175. { font-size: 10px; font-weight: bold; white-space: nowrap; }
  176. .org-info-js_search-highlight
  177. { background-color: #ffff00; color: #000000; font-weight: bold; }
  178. .org-svg { width: 90%; }
  179. /*]]>*/-->
  180. </style>
  181. <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/htmlize.css"/>
  182. <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/readtheorg.css"/>
  183. <script type="text/javascript">
  184. /*
  185. @licstart The following is the entire license notice for the
  186. JavaScript code in this tag.
  187. Copyright (C) 2012-2017 Free Software Foundation, Inc.
  188. The JavaScript code in this tag is free software: you can
  189. redistribute it and/or modify it under the terms of the GNU
  190. General Public License (GNU GPL) as published by the Free Software
  191. Foundation, either version 3 of the License, or (at your option)
  192. any later version. The code is distributed WITHOUT ANY WARRANTY;
  193. without even the implied warranty of MERCHANTABILITY or FITNESS
  194. FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  195. As additional permission under GNU GPL version 3 section 7, you
  196. may distribute non-source (e.g., minimized or compacted) forms of
  197. that code without the copy of the GNU GPL normally required by
  198. section 4, provided you include this license notice and a URL
  199. through which recipients can access the Corresponding Source.
  200. @licend The above is the entire license notice
  201. for the JavaScript code in this tag.
  202. */
  203. <!--/*--><![CDATA[/*><!--*/
  204. function CodeHighlightOn(elem, id)
  205. {
  206. var target = document.getElementById(id);
  207. if(null != target) {
  208. elem.cacheClassElem = elem.className;
  209. elem.cacheClassTarget = target.className;
  210. target.className = "code-highlighted";
  211. elem.className = "code-highlighted";
  212. }
  213. }
  214. function CodeHighlightOff(elem, id)
  215. {
  216. var target = document.getElementById(id);
  217. if(elem.cacheClassElem)
  218. elem.className = elem.cacheClassElem;
  219. if(elem.cacheClassTarget)
  220. target.className = elem.cacheClassTarget;
  221. }
  222. /*]]>*///-->
  223. </script>
  224. </head>
  225. <body>
  226. <div id="content">
  227. <h1 class="title">AWK</h1>
  228. <div id="table-of-contents">
  229. <h2>Table of Contents</h2>
  230. <div id="text-table-of-contents">
  231. <ul>
  232. <li><a href="#org1da1eff">1. AWK and GAWK</a>
  233. <ul>
  234. <li><a href="#orgb5fc0b9">1.1. Simple Description of AWK:</a>
  235. <ul>
  236. <li><a href="#orgf0d32ba">1.1.1. Typical uses of awk:</a></li>
  237. </ul>
  238. </li>
  239. <li><a href="#org03371c5">1.2. Basic example</a></li>
  240. <li><a href="#org87bb050">1.3. Better examples</a></li>
  241. <li><a href="#orgaf95990">1.4. What else can be done</a></li>
  242. <li><a href="#orga44b236">1.5. Resources</a></li>
  243. </ul>
  244. </li>
  245. </ul>
  246. </div>
  247. </div>
  248. <div id="outline-container-org1da1eff" class="outline-2">
  249. <h2 id="org1da1eff"><span class="section-number-2">1</span> AWK and GAWK</h2>
  250. <div class="outline-text-2" id="text-1">
  251. <p>
  252. <i>Dependencies:</i> In order to learn awk this can be necessary for you to learn before:
  253. </p>
  254. <ul class="org-ul">
  255. <li>Unix Pipes</li>
  256. <li>Shell Basics</li>
  257. <li>Data Streams</li>
  258. <li>Unix-like Operative Systems</li>
  259. </ul>
  260. <ul class="org-ul">
  261. <li>Before we start learning grep please create the following file named <code>animals.txt</code></li>
  262. </ul>
  263. <pre class="example">
  264. Animals Quantity
  265. Dogs 32
  266. Cats 17
  267. Birds 25
  268. Cows 7
  269. Ducks 9
  270. Pigs 12
  271. </pre>
  272. </div>
  273. <div id="outline-container-orgb5fc0b9" class="outline-3">
  274. <h3 id="orgb5fc0b9"><span class="section-number-3">1.1</span> Simple Description of AWK:</h3>
  275. <div class="outline-text-3" id="text-1-1">
  276. <p>
  277. AWK is a programming language in UNIX, it is designed for text processing, but can be used for data extraction and reporting generation. As a programming language, the syntax of Awk would seem similar to the languages C, Python and Bash, among others.
  278. </p>
  279. <p>
  280. (<b>Note</b> In this case GNU awk or GAWK will be covered, which is the <a href="https://www.gnu.org/philosophy/free-sw.html">GNU Project's</a> implementation of AWK. <b>End Note</b>)
  281. </p>
  282. </div>
  283. <div id="outline-container-orgf0d32ba" class="outline-4">
  284. <h4 id="orgf0d32ba"><span class="section-number-4">1.1.1</span> Typical uses of awk:</h4>
  285. <div class="outline-text-4" id="text-1-1-1">
  286. <ul class="org-ul">
  287. <li>Text processing.</li>
  288. <li>Data extraction.</li>
  289. <li>Formatted text reports.</li>
  290. <li>Arithmetic operations.</li>
  291. <li>String operations.</li>
  292. <li>Many more!.</li>
  293. </ul>
  294. </div>
  295. </div>
  296. </div>
  297. <div id="outline-container-org03371c5" class="outline-3">
  298. <h3 id="org03371c5"><span class="section-number-3">1.2</span> Basic example</h3>
  299. <div class="outline-text-3" id="text-1-2">
  300. <p>
  301. The first program we will write is very simple, it will just print "hello world!":
  302. </p>
  303. <pre class="example">
  304. $ echo "hello world!" | awk {'print'}
  305. hello world
  306. </pre>
  307. <p>
  308. <b>What just happened?</b>
  309. <i>awk is a pattern matcher, in this case it got an input "hello world" and a pattern 'print', and awk applies pattern to each line of the input. That is why we see 'hello world' as a result.</i>
  310. </p>
  311. <p>
  312. As we can see awk is a language that process an input file, in a nutshell this is what awk does:
  313. </p>
  314. <ol class="org-ol">
  315. <li>Gets input, a pattern to look for, and a rule to apply.</li>
  316. <li>Reads input.</li>
  317. <li>Looks first line of input looking for pattern.</li>
  318. <li>If a pattern is matched, then rule is applied.</li>
  319. <li>Moves to next line until End of File.</li>
  320. </ol>
  321. <p>
  322. Of course, awk can get multiple patterns and rules, but all of them are applied sequentially, we can also change awk behaviour so it looks at patterns and not at lines.
  323. </p>
  324. </div>
  325. </div>
  326. <div id="outline-container-org87bb050" class="outline-3">
  327. <h3 id="org87bb050"><span class="section-number-3">1.3</span> Better examples</h3>
  328. <div class="outline-text-3" id="text-1-3">
  329. <p>
  330. With awk we can print the content of a file with:
  331. </p>
  332. <pre class="example">
  333. $ awk '{print $0}' fruits.txt
  334. Animals Quantity
  335. Dogs 32
  336. Cats 17
  337. Birds 25
  338. Cows 7
  339. Ducks 9
  340. Pigs 12
  341. </pre>
  342. <p>
  343. We can also print only the first field of text with $1
  344. </p>
  345. <pre class="example">
  346. $ awk '{print $1}' fruits.txt
  347. Animals
  348. Dogs
  349. Cats
  350. Birds
  351. Cows
  352. Ducks
  353. Pigs
  354. </pre>
  355. <p>
  356. We can also print only the second field of text with $2
  357. </p>
  358. <pre class="example">
  359. $ awk '{print $2}' fruits.txt
  360. Quantity
  361. 32
  362. 17
  363. 25
  364. 7
  365. 9
  366. 12
  367. </pre>
  368. <p>
  369. <b>What just happened?</b>
  370. The "$0", "$1" and "$2" have a meaning similar to a shell script. Instead of the zero, first and second argument, they mean the entire current line, the first and second field of the input line, respectively.
  371. </p>
  372. </div>
  373. </div>
  374. <div id="outline-container-orgaf95990" class="outline-3">
  375. <h3 id="orgaf95990"><span class="section-number-3">1.4</span> What else can be done</h3>
  376. <div class="outline-text-3" id="text-1-4">
  377. <p>
  378. Cool stuff that you can do now with this new knowledge.
  379. </p>
  380. </div>
  381. </div>
  382. <div id="outline-container-orga44b236" class="outline-3">
  383. <h3 id="orga44b236"><span class="section-number-3">1.5</span> Resources</h3>
  384. <div class="outline-text-3" id="text-1-5">
  385. <p>
  386. <a href="https://www.gnu.org/software/gawk/manual/">Gawk: Effective AWK Programming</a>
  387. <a href="https://learnbyexample.gitbooks.io/command-line-text-processing/content/gnu_awk.html">Learn by Example - AWK</a>
  388. <a href="https://www.ibm.com/developerworks/library/l-awk1/">AWK by example - IBM</a>
  389. <a href="http://www.grymoire.com/Unix/Awk.html">AWK - Grymoire</a>
  390. <a href="https://www.tutorialspoint.com/awk/">AWK Tutorial - Tutorialspoint</a>
  391. <a href="http://www.softpanorama.org/Tools/Awk/hello_world_in_awk.shtml">Hello World in AWK</a>
  392. </p>
  393. </div>
  394. </div>
  395. </div>
  396. </div>
  397. <div id="postamble" class="status">
  398. <p class="date">Created: 2017-11-11 sáb 12:25</p>
  399. <p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
  400. </div>
  401. </body>
  402. </html>