123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
- <head>
- <!-- 2017-11-11 sáb 12:25 -->
- <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <title>AWK</title>
- <meta name="generator" content="Org mode" />
- <style type="text/css">
- <!--/*--><![CDATA[/*><!--*/
- .title { text-align: center;
- margin-bottom: .2em; }
- .subtitle { text-align: center;
- font-size: medium;
- font-weight: bold;
- margin-top:0; }
- .todo { font-family: monospace; color: red; }
- .done { font-family: monospace; color: green; }
- .priority { font-family: monospace; color: orange; }
- .tag { background-color: #eee; font-family: monospace;
- padding: 2px; font-size: 80%; font-weight: normal; }
- .timestamp { color: #bebebe; }
- .timestamp-kwd { color: #5f9ea0; }
- .org-right { margin-left: auto; margin-right: 0px; text-align: right; }
- .org-left { margin-left: 0px; margin-right: auto; text-align: left; }
- .org-center { margin-left: auto; margin-right: auto; text-align: center; }
- .underline { text-decoration: underline; }
- #postamble p, #preamble p { font-size: 90%; margin: .2em; }
- p.verse { margin-left: 3%; }
- pre {
- border: 1px solid #ccc;
- box-shadow: 3px 3px 3px #eee;
- padding: 8pt;
- font-family: monospace;
- overflow: auto;
- margin: 1.2em;
- }
- pre.src {
- position: relative;
- overflow: visible;
- padding-top: 1.2em;
- }
- pre.src:before {
- display: none;
- position: absolute;
- background-color: white;
- top: -10px;
- right: 10px;
- padding: 3px;
- border: 1px solid black;
- }
- pre.src:hover:before { display: inline;}
- /* Languages per Org manual */
- pre.src-asymptote:before { content: 'Asymptote'; }
- pre.src-awk:before { content: 'Awk'; }
- pre.src-C:before { content: 'C'; }
- /* pre.src-C++ doesn't work in CSS */
- pre.src-clojure:before { content: 'Clojure'; }
- pre.src-css:before { content: 'CSS'; }
- pre.src-D:before { content: 'D'; }
- pre.src-ditaa:before { content: 'ditaa'; }
- pre.src-dot:before { content: 'Graphviz'; }
- pre.src-calc:before { content: 'Emacs Calc'; }
- pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
- pre.src-fortran:before { content: 'Fortran'; }
- pre.src-gnuplot:before { content: 'gnuplot'; }
- pre.src-haskell:before { content: 'Haskell'; }
- pre.src-hledger:before { content: 'hledger'; }
- pre.src-java:before { content: 'Java'; }
- pre.src-js:before { content: 'Javascript'; }
- pre.src-latex:before { content: 'LaTeX'; }
- pre.src-ledger:before { content: 'Ledger'; }
- pre.src-lisp:before { content: 'Lisp'; }
- pre.src-lilypond:before { content: 'Lilypond'; }
- pre.src-lua:before { content: 'Lua'; }
- pre.src-matlab:before { content: 'MATLAB'; }
- pre.src-mscgen:before { content: 'Mscgen'; }
- pre.src-ocaml:before { content: 'Objective Caml'; }
- pre.src-octave:before { content: 'Octave'; }
- pre.src-org:before { content: 'Org mode'; }
- pre.src-oz:before { content: 'OZ'; }
- pre.src-plantuml:before { content: 'Plantuml'; }
- pre.src-processing:before { content: 'Processing.js'; }
- pre.src-python:before { content: 'Python'; }
- pre.src-R:before { content: 'R'; }
- pre.src-ruby:before { content: 'Ruby'; }
- pre.src-sass:before { content: 'Sass'; }
- pre.src-scheme:before { content: 'Scheme'; }
- pre.src-screen:before { content: 'Gnu Screen'; }
- pre.src-sed:before { content: 'Sed'; }
- pre.src-sh:before { content: 'shell'; }
- pre.src-sql:before { content: 'SQL'; }
- pre.src-sqlite:before { content: 'SQLite'; }
- /* additional languages in org.el's org-babel-load-languages alist */
- pre.src-forth:before { content: 'Forth'; }
- pre.src-io:before { content: 'IO'; }
- pre.src-J:before { content: 'J'; }
- pre.src-makefile:before { content: 'Makefile'; }
- pre.src-maxima:before { content: 'Maxima'; }
- pre.src-perl:before { content: 'Perl'; }
- pre.src-picolisp:before { content: 'Pico Lisp'; }
- pre.src-scala:before { content: 'Scala'; }
- pre.src-shell:before { content: 'Shell Script'; }
- pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
- /* additional language identifiers per "defun org-babel-execute"
- in ob-*.el */
- pre.src-cpp:before { content: 'C++'; }
- pre.src-abc:before { content: 'ABC'; }
- pre.src-coq:before { content: 'Coq'; }
- pre.src-groovy:before { content: 'Groovy'; }
- /* additional language identifiers from org-babel-shell-names in
- ob-shell.el: ob-shell is the only babel language using a lambda to put
- the execution function name together. */
- pre.src-bash:before { content: 'bash'; }
- pre.src-csh:before { content: 'csh'; }
- pre.src-ash:before { content: 'ash'; }
- pre.src-dash:before { content: 'dash'; }
- pre.src-ksh:before { content: 'ksh'; }
- pre.src-mksh:before { content: 'mksh'; }
- pre.src-posh:before { content: 'posh'; }
- /* Additional Emacs modes also supported by the LaTeX listings package */
- pre.src-ada:before { content: 'Ada'; }
- pre.src-asm:before { content: 'Assembler'; }
- pre.src-caml:before { content: 'Caml'; }
- pre.src-delphi:before { content: 'Delphi'; }
- pre.src-html:before { content: 'HTML'; }
- pre.src-idl:before { content: 'IDL'; }
- pre.src-mercury:before { content: 'Mercury'; }
- pre.src-metapost:before { content: 'MetaPost'; }
- pre.src-modula-2:before { content: 'Modula-2'; }
- pre.src-pascal:before { content: 'Pascal'; }
- pre.src-ps:before { content: 'PostScript'; }
- pre.src-prolog:before { content: 'Prolog'; }
- pre.src-simula:before { content: 'Simula'; }
- pre.src-tcl:before { content: 'tcl'; }
- pre.src-tex:before { content: 'TeX'; }
- pre.src-plain-tex:before { content: 'Plain TeX'; }
- pre.src-verilog:before { content: 'Verilog'; }
- pre.src-vhdl:before { content: 'VHDL'; }
- pre.src-xml:before { content: 'XML'; }
- pre.src-nxml:before { content: 'XML'; }
- /* add a generic configuration mode; LaTeX export needs an additional
- (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
- pre.src-conf:before { content: 'Configuration File'; }
- table { border-collapse:collapse; }
- caption.t-above { caption-side: top; }
- caption.t-bottom { caption-side: bottom; }
- td, th { vertical-align:top; }
- th.org-right { text-align: center; }
- th.org-left { text-align: center; }
- th.org-center { text-align: center; }
- td.org-right { text-align: right; }
- td.org-left { text-align: left; }
- td.org-center { text-align: center; }
- dt { font-weight: bold; }
- .footpara { display: inline; }
- .footdef { margin-bottom: 1em; }
- .figure { padding: 1em; }
- .figure p { text-align: center; }
- .inlinetask {
- padding: 10px;
- border: 2px solid gray;
- margin: 10px;
- background: #ffffcc;
- }
- #org-div-home-and-up
- { text-align: right; font-size: 70%; white-space: nowrap; }
- textarea { overflow-x: auto; }
- .linenr { font-size: smaller }
- .code-highlighted { background-color: #ffff00; }
- .org-info-js_info-navigation { border-style: none; }
- #org-info-js_console-label
- { font-size: 10px; font-weight: bold; white-space: nowrap; }
- .org-info-js_search-highlight
- { background-color: #ffff00; color: #000000; font-weight: bold; }
- .org-svg { width: 90%; }
- /*]]>*/-->
- </style>
- <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/htmlize.css"/>
- <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/readtheorg.css"/>
- <script type="text/javascript">
- /*
- @licstart The following is the entire license notice for the
- JavaScript code in this tag.
- Copyright (C) 2012-2017 Free Software Foundation, Inc.
- The JavaScript code in this tag is free software: you can
- redistribute it and/or modify it under the terms of the GNU
- General Public License (GNU GPL) as published by the Free Software
- Foundation, either version 3 of the License, or (at your option)
- any later version. The code is distributed WITHOUT ANY WARRANTY;
- without even the implied warranty of MERCHANTABILITY or FITNESS
- FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
- As additional permission under GNU GPL version 3 section 7, you
- may distribute non-source (e.g., minimized or compacted) forms of
- that code without the copy of the GNU GPL normally required by
- section 4, provided you include this license notice and a URL
- through which recipients can access the Corresponding Source.
- @licend The above is the entire license notice
- for the JavaScript code in this tag.
- */
- <!--/*--><![CDATA[/*><!--*/
- function CodeHighlightOn(elem, id)
- {
- var target = document.getElementById(id);
- if(null != target) {
- elem.cacheClassElem = elem.className;
- elem.cacheClassTarget = target.className;
- target.className = "code-highlighted";
- elem.className = "code-highlighted";
- }
- }
- function CodeHighlightOff(elem, id)
- {
- var target = document.getElementById(id);
- if(elem.cacheClassElem)
- elem.className = elem.cacheClassElem;
- if(elem.cacheClassTarget)
- target.className = elem.cacheClassTarget;
- }
- /*]]>*///-->
- </script>
- </head>
- <body>
- <div id="content">
- <h1 class="title">AWK</h1>
- <div id="table-of-contents">
- <h2>Table of Contents</h2>
- <div id="text-table-of-contents">
- <ul>
- <li><a href="#org1da1eff">1. AWK and GAWK</a>
- <ul>
- <li><a href="#orgb5fc0b9">1.1. Simple Description of AWK:</a>
- <ul>
- <li><a href="#orgf0d32ba">1.1.1. Typical uses of awk:</a></li>
- </ul>
- </li>
- <li><a href="#org03371c5">1.2. Basic example</a></li>
- <li><a href="#org87bb050">1.3. Better examples</a></li>
- <li><a href="#orgaf95990">1.4. What else can be done</a></li>
- <li><a href="#orga44b236">1.5. Resources</a></li>
- </ul>
- </li>
- </ul>
- </div>
- </div>
- <div id="outline-container-org1da1eff" class="outline-2">
- <h2 id="org1da1eff"><span class="section-number-2">1</span> AWK and GAWK</h2>
- <div class="outline-text-2" id="text-1">
- <p>
- <i>Dependencies:</i> In order to learn awk this can be necessary for you to learn before:
- </p>
- <ul class="org-ul">
- <li>Unix Pipes</li>
- <li>Shell Basics</li>
- <li>Data Streams</li>
- <li>Unix-like Operative Systems</li>
- </ul>
- <ul class="org-ul">
- <li>Before we start learning grep please create the following file named <code>animals.txt</code></li>
- </ul>
- <pre class="example">
- Animals Quantity
- Dogs 32
- Cats 17
- Birds 25
- Cows 7
- Ducks 9
- Pigs 12
- </pre>
- </div>
- <div id="outline-container-orgb5fc0b9" class="outline-3">
- <h3 id="orgb5fc0b9"><span class="section-number-3">1.1</span> Simple Description of AWK:</h3>
- <div class="outline-text-3" id="text-1-1">
- <p>
- 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.
- </p>
- <p>
- (<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>)
- </p>
- </div>
- <div id="outline-container-orgf0d32ba" class="outline-4">
- <h4 id="orgf0d32ba"><span class="section-number-4">1.1.1</span> Typical uses of awk:</h4>
- <div class="outline-text-4" id="text-1-1-1">
- <ul class="org-ul">
- <li>Text processing.</li>
- <li>Data extraction.</li>
- <li>Formatted text reports.</li>
- <li>Arithmetic operations.</li>
- <li>String operations.</li>
- <li>Many more!.</li>
- </ul>
- </div>
- </div>
- </div>
- <div id="outline-container-org03371c5" class="outline-3">
- <h3 id="org03371c5"><span class="section-number-3">1.2</span> Basic example</h3>
- <div class="outline-text-3" id="text-1-2">
- <p>
- The first program we will write is very simple, it will just print "hello world!":
- </p>
- <pre class="example">
- $ echo "hello world!" | awk {'print'}
- hello world
- </pre>
- <p>
- <b>What just happened?</b>
- <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>
- </p>
- <p>
- As we can see awk is a language that process an input file, in a nutshell this is what awk does:
- </p>
- <ol class="org-ol">
- <li>Gets input, a pattern to look for, and a rule to apply.</li>
- <li>Reads input.</li>
- <li>Looks first line of input looking for pattern.</li>
- <li>If a pattern is matched, then rule is applied.</li>
- <li>Moves to next line until End of File.</li>
- </ol>
- <p>
- 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.
- </p>
- </div>
- </div>
- <div id="outline-container-org87bb050" class="outline-3">
- <h3 id="org87bb050"><span class="section-number-3">1.3</span> Better examples</h3>
- <div class="outline-text-3" id="text-1-3">
- <p>
- With awk we can print the content of a file with:
- </p>
- <pre class="example">
- $ awk '{print $0}' fruits.txt
- Animals Quantity
- Dogs 32
- Cats 17
- Birds 25
- Cows 7
- Ducks 9
- Pigs 12
- </pre>
- <p>
- We can also print only the first field of text with $1
- </p>
- <pre class="example">
- $ awk '{print $1}' fruits.txt
- Animals
- Dogs
- Cats
- Birds
- Cows
- Ducks
- Pigs
- </pre>
- <p>
- We can also print only the second field of text with $2
- </p>
- <pre class="example">
- $ awk '{print $2}' fruits.txt
- Quantity
- 32
- 17
- 25
- 7
- 9
- 12
- </pre>
- <p>
- <b>What just happened?</b>
- 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.
- </p>
- </div>
- </div>
- <div id="outline-container-orgaf95990" class="outline-3">
- <h3 id="orgaf95990"><span class="section-number-3">1.4</span> What else can be done</h3>
- <div class="outline-text-3" id="text-1-4">
- <p>
- Cool stuff that you can do now with this new knowledge.
- </p>
- </div>
- </div>
- <div id="outline-container-orga44b236" class="outline-3">
- <h3 id="orga44b236"><span class="section-number-3">1.5</span> Resources</h3>
- <div class="outline-text-3" id="text-1-5">
- <p>
- <a href="https://www.gnu.org/software/gawk/manual/">Gawk: Effective AWK Programming</a>
- <a href="https://learnbyexample.gitbooks.io/command-line-text-processing/content/gnu_awk.html">Learn by Example - AWK</a>
- <a href="https://www.ibm.com/developerworks/library/l-awk1/">AWK by example - IBM</a>
- <a href="http://www.grymoire.com/Unix/Awk.html">AWK - Grymoire</a>
- <a href="https://www.tutorialspoint.com/awk/">AWK Tutorial - Tutorialspoint</a>
- <a href="http://www.softpanorama.org/Tools/Awk/hello_world_in_awk.shtml">Hello World in AWK</a>
- </p>
- </div>
- </div>
- </div>
- </div>
- <div id="postamble" class="status">
- <p class="date">Created: 2017-11-11 sáb 12:25</p>
- <p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
- </div>
- </body>
- </html>
|