Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Awk utility in Fedora

#1
Awk utility in Fedora

<div><p>Fedora provides <em>awk</em> as part of its default installation, including all its editions, including the immutable ones like Silverblue. But you may be asking, what is <em>awk</em> and why would you need it?</p>
<p><em>Awk</em> is a data driven programming language that acts when it matches a pattern. On Fedora, and most other distributions, GNU <em>awk</em> or <em>gawk</em> is used. Read on for more about this language and how to use it.</p>
<p> <span id="more-27576"></span> </p>
<h2>A brief history of awk</h2>
<p><em>Awk</em> began at Bell Labs in 1977. Its name is an acronym from the initials of the designers: Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan. </p>
<blockquote class="wp-block-quote">
<p>The specification for <em>awk</em> in the POSIX Command Language and Utilities standard further clarified the language. Both the <em>gawk</em> designers and the original <em>awk</em> designers at Bell Laboratories provided feedback for the POSIX specification. </p>
<p><cite>From <a href="https://www.gnu.org/software/gawk/manual/gawk.html#Foreword3">The GNU Awk User’s Guide</a></cite></p></blockquote>
<p>For a more in-depth look at how <em>awk/gawk</em> ended up being as powerful and useful as it is, follow the link above. Numerous individuals have contributed to the current state of <em>gawk</em>. Among those are:</p>
<ul>
<li>Arnold Robbins and David Trueman, the creators of <em>gawk</em></li>
<li>Michael Brennan, the creator of <em>mawk</em>, which later was merged with <em>gawk</em></li>
<li>Jurgen Kahrs, who added networking capabilities to <em>gawk</em> in 1997</li>
<li>John Hague, who rewrote the <em>gawk</em> internals and added an <em>awk</em>-level debugger in 2011</li>
</ul>
<h2>Using awk</h2>
<p>The following sections show various ways of using <em>awk</em> in Fedora.</p>
<h3>At the command line</h3>
<p>The simples way to invoke <em>awk</em> is at the command line. You can search a text file for a particular pattern, and if found, print out the line(s) of the file that match the pattern anywhere. As an example, use <em>cat</em> to take a look at the command history file in your home director:</p>
<pre class="wp-block-preformatted">$ cat ~/.bash_history</pre>
<p> There are probably many lines scrolling by right now. </p>
<p><em>Awk</em> helps with this type of file quite easily. Instead of printing the entire file out to the terminal like <em>cat</em>, you can use <em>awk</em> to find something of specific interest. For this example, type the following at the command line if you’re running a standard Fedora edition:</p>
<pre class="wp-block-preformatted">$ awk '/dnf/' ~/.bash_history<br /></pre>
<p> If you’re running Silverblue, try this instead:</p>
<pre class="wp-block-preformatted">$ awk '/rpm-ostree/' ~/.bash_history</pre>
<p>In both cases, more data likely appears than what you really want. That’s no problem for <em>awk</em> since it can accept regular expressions. Using the previous example, you can change the pattern to more closely match search requirements of wanting to know about installs only. Try changing the search pattern to one of these:</p>
<pre class="wp-block-preformatted">$ awk '/rpm-ostree install/' ~/.bash_history<br />$ awk '/dnf isntall/' ~/.bash_history<br /></pre>
<p>All the entries of your bash command line history appear that have the pattern specified at any position along the line. Awk works on one line of a data file at a time. It matches pattern, then performs an action, then moves to next line until the end of file (EOF) is reached. </p>
<h3>From an <em>awk</em> program</h3>
<p>Using awk at the command line as above is not much different than piping output to <em>grep</em>, like this:</p>
<pre class="wp-block-preformatted">$ cat .bash_history | grep 'dnf install'</pre>
<p>The end result of printing to standard output (<em>stdout</em>) is the same with both methods. </p>
<p>Awk is a programming language, and the command <em>awk</em> is an interpreter of that language. The real power and flexibility of <em>awk</em> is you can make programs with it, and combine them with shell scripts to create even more powerful programs. For more feature rich development with <em>awk</em>, you can also incorporate C or C++ code using <a href="https://www.gnu.org/software/gawk/manual/gawk.html#Dynamic-Extensions">Dynamic-Extensions</a>. </p>
<p>Next, to show the power of <em>awk</em>, let’s make a couple of program files to print the header and draw five numbers for the first row of a bingo card. To do this we’ll create two awk program files. </p>
<p>The first file prints out the header of the bingo card. For this example it is called <em>bingo-title.awk</em>. Use your favorite editor to save this text as that file name: </p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">BEGIN { <br />
&nbsp; &nbsp; print &quot;B\tI\tN\tG\tO&quot; <br />
}</div></div> </pre>
<p>Now the title program is ready. You could try it out with this command:</p>
<pre class="wp-block-preformatted">$ awk -f bingo-title.awk</pre>
<p>The program prints the word BINGO, with a tab space (<em>\t</em>) between the characters. For the number selection, let’s use one of awk’s builtin numeric functions called <em>rand()</em> and use two of the control statements, <em>for</em> and <em>switch.</em> (Except the editor changed my program, so no switch statement used this time).</p>
<p>The title of the second awk program is <em>bingo-num.awk</em>. Enter the following into your favorite editor and save with that file name:</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">@include &quot;bingo-title.awk&quot;<br />
BEGIN {<br />
&nbsp; &nbsp; for (i = 1; i &lt; = 5; i++) {<br />
&nbsp; &nbsp; b = int(rand() * 15) + (15*(i-1))<br />
&nbsp; &nbsp; printf &quot;%s\t&quot;, b<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; print<br />
}</div></div> </code></pre>
<p>The <em>@include</em> statement in the file tells the interpreter to process the included file first. In this case the interpreter processs the <em>bingo-title.awk</em> file so the title prints out first.</p>
<h3>Running the test program</h3>
<p>Now enter the command to pick a row of bingo numbers:</p>
<pre class="wp-block-preformatted">$ awk -f bingo-num.awk</pre>
<p>Output appears similar to the following. Note that the <em>rand()</em> function in <em>awk</em> is not ideal for truly random numbers. It’s used here only as for example purposes.</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">$ awk -f bingo-num.awk <br />
B &nbsp; I &nbsp; N &nbsp; G &nbsp; O<br />
13&nbsp; 23&nbsp; 34&nbsp; 53&nbsp; 71</div></div> </pre>
<p>In the example, we created two programs with only beginning sections that used actions to manipulate data generated from within the awk program. In order to satisfy the rules of Bingo, more work is needed to achieve the desirable results. The reader is encouraged to fix the programs so they can reliably pick bingo numbers, maybe look at the awk function <em>srand()</em> for answers on how that could be done.</p>
<h2>Final examples</h2>
<p><em>Awk</em> can be useful even for mundane daily search tasks that you encounter, like listing all <em>flatpak’s</em> on the <em>Flathub</em> repository from <em>org.gnome</em> (providing you have the Flathub repository setup). The command to do that would be:</p>
<pre class="wp-block-preformatted">$ flatpak remote-ls flathub --system | awk /org.gnome/</pre>
<p>A listing appears that shows all output from <em>remote-ls</em> that matches the <em>org.gnome</em> pattern. To see flatpaks already installed from org.gnome, enter this command:</p>
<pre class="wp-block-preformatted">$ flatpak list --system | awk /org.gnome/</pre>
<p>Awk is a powerful and flexible programming language that fills a niche with text file manipulation exceedingly well. </p>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016