<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Awk Command Archives - MASSIVE News</title>
	<atom:link href="https://massive.news/tag/awk-command/feed/" rel="self" type="application/rss+xml" />
	<link>https://massive.news/tag/awk-command/</link>
	<description>Progressive Mix of World News and Propaganda</description>
	<lastBuildDate>Fri, 27 Dec 2024 02:16:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://massive.news/wp-content/uploads/2024/08/m-150x150.jpg</url>
	<title>Awk Command Archives - MASSIVE News</title>
	<link>https://massive.news/tag/awk-command/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Use awk for Arithmetic in Loops in Linux</title>
		<link>https://massive.news/how-to-use-awk-for-arithmetic-in-loops-in-linux/</link>
		
		<dc:creator><![CDATA[Editor]]></dc:creator>
		<pubDate>Wed, 25 Dec 2024 04:21:38 +0000</pubDate>
				<category><![CDATA[World News]]></category>
		<category><![CDATA[Awk Command]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Operations]]></category>
		<category><![CDATA[X]]></category>
		<guid isPermaLink="false">https://massive.news/how-to-use-awk-for-arithmetic-in-loops-in-linux/</guid>

					<description><![CDATA[<p>The awk command is a powerful tool in Linux for processing and analyzing text files, which...</p>
<p>The post <a href="https://massive.news/how-to-use-awk-for-arithmetic-in-loops-in-linux/">How to Use awk for Arithmetic in Loops in Linux</a> appeared first on <a href="https://massive.news">MASSIVE News</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The awk command is a powerful tool in Linux for processing and analyzing text files, which is particularly useful when you need to perform arithmetic operations within loops.</p>
<p>This article will guide you through using <strong>awk</strong> for arithmetic operations in loops, using simple examples to make the concepts clear.</p>
<h2>What is awk?</h2>
<p><strong>awk</strong> is a scripting language designed for text processing and data extraction, which reads input line by line, splits each line into fields, and allows you to perform operations on those fields. It’s commonly used for tasks like pattern matching, arithmetic calculations, and generating formatted reports.</p>
<p>The basic syntax of <code>awk</code> is:</p>
<pre>awk 'BEGIN { initialization } { actions } END { finalization }' file
</pre>
<ul>
<li><code>BEGIN</code>: Code block executed before processing the input.</li>
<li><code>actions</code>: Code block executed for each line of the input.</li>
<li><code>END</code>: Code block executed after processing all lines.</li>
</ul>
<h2>Performing Arithmetic Operations in Loops</h2>
<p>Let’s explore how to use <code>awk</code> to perform arithmetic operations within loops with the following useful examples to demonstrate key concepts.</p>
<h3>Example 1: Calculating the Sum of Numbers</h3>
<p><center></p>
<div align="center" id="tecmint_incontent"></div>
<p></center></p>
<p>Suppose you have a file named <code>numbers.txt</code> containing the following numbers:</p>
<pre>5
10
15
20
</pre>
<p>You can calculate the sum of these numbers using <code>awk</code>:</p>
<pre>awk '{ sum += $1 } END { print "Total Sum:", sum }' numbers.txt
</pre>
<p><strong>Explanation</strong>:</p>
<ul>
<li><code>{ sum += $1 }</code>: For each line, the value of the first field <code>$1</code> is added to the variable <code>sum</code>.</li>
<li><code>END { print "Total Sum:", sum }</code>: After processing all lines, the total <code>sum</code> is printed.</li>
</ul>
<figure id="attachment_59065" aria-describedby="caption-attachment-59065" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" class="size-full wp-image-59065" src="https://massive.news/wp-content/uploads/2024/12/how-to-use-awk-for-arithmetic-in-loops-in-linux.png" alt="Number Sum Calculation" width="755" height="103"><figcaption id="caption-attachment-59065" class="wp-caption-text">Number Sum Calculation</figcaption></figure>
<h3>Example 2: Calculating the Average</h3>
<p>To calculate the average of the numbers:</p>
<pre>awk '{ sum += $1; count++ } END { print "Average:", sum / count }' numbers.txt
</pre>
<p><strong>Explanation</strong>:</p>
<ul>
<li><code>count++</code>: Increments the counter for each line.</li>
<li><code>sum / count</code>: Divides the total sum by the count to calculate the average.</li>
</ul>
<figure id="attachment_59067" aria-describedby="caption-attachment-59067" class="wp-caption aligncenter"><img decoding="async" class="size-full wp-image-59067" src="https://massive.news/wp-content/uploads/2024/12/how-to-use-awk-for-arithmetic-in-loops-in-linux-1.png" alt="Calculate the Average of a Set of Numbers" width="875" height="181"><figcaption id="caption-attachment-59067" class="wp-caption-text">Calculate the Average of a Set of Numbers</figcaption></figure>
<h3>Example 3: Multiplication Table</h3>
<p>You can use <code>awk</code> to generate a multiplication table for a given number. For example, to generate a table for 5:</p>
<pre>awk 'BEGIN { for (i = 1; i &lt;= 10; i++) print "5 x", i, "=", 5 * i }'
</pre>
<p><strong>Explanation</strong>:</p>
<ul>
<li><code>for (i = 1; i &lt;= 10; i++)</code>: A loop that runs from 1 to 10.</li>
<li><code>print "5 x", i, "=", 5 * i</code>: Prints the multiplication table.</li>
</ul>
<figure id="attachment_59068" aria-describedby="caption-attachment-59068" class="wp-caption aligncenter"><img decoding="async" class="size-full wp-image-59068" src="https://massive.news/wp-content/uploads/2024/12/how-to-use-awk-for-arithmetic-in-loops-in-linux-2.png" alt="Multiplication table with awk" width="829" height="323"><figcaption id="caption-attachment-59068" class="wp-caption-text">Multiplication table with awk</figcaption></figure>
<h3>Example 4: Factorial Calculation</h3>
<p>To calculate the factorial of a number (e.g., 5):</p>
<pre>awk 'BEGIN { n = 5; factorial = 1; for (i = 1; i &lt;= n; i++) factorial *= i; print "Factorial of", n, "is", factorial }'
</pre>
<p><strong>Explanation</strong>:</p>
<ul>
<li><code>n = 5</code>: The number for which the factorial is calculated.</li>
<li><code>factorial *= i</code>: Multiplies the current value of factorial by <code>i</code> in each iteration.</li>
</ul>
<figure id="attachment_59069" aria-describedby="caption-attachment-59069" class="wp-caption aligncenter"><img loading="lazy" decoding="async" class="size-full wp-image-59069" src="https://massive.news/wp-content/uploads/2024/12/how-to-use-awk-for-arithmetic-in-loops-in-linux-3.png" alt="Factorial Calculation Example" width="907" height="188"><figcaption id="caption-attachment-59069" class="wp-caption-text">Factorial Calculation Example</figcaption></figure>
<h3>Example 5: Summing Even Numbers</h3>
<p>To sum only the even numbers from a file:</p>
<pre>awk '{ if ($1 % 2 == 0) sum += $1 } END { print "Sum of Even Numbers:", sum }' numbers.txt
</pre>
<p><strong>Explanation</strong>:</p>
<ul>
<li><code>if ($1 % 2 == 0)</code>: Checks if the number is even.</li>
<li><code>sum += $1</code>: Adds the even number to the sum.</li>
</ul>
<figure id="attachment_59070" aria-describedby="caption-attachment-59070" class="wp-caption aligncenter"><img loading="lazy" decoding="async" class="size-full wp-image-59070" src="https://massive.news/wp-content/uploads/2024/12/how-to-use-awk-for-arithmetic-in-loops-in-linux-4.png" alt="Calculate the Sum of Even Numbers" width="916" height="204"><figcaption id="caption-attachment-59070" class="wp-caption-text">Calculate the Sum of Even Numbers</figcaption></figure>
<h5>Conclusion</h5>
<p>The <code>awk</code> command is a versatile tool for performing arithmetic operations in loops. By combining loops, conditions, and arithmetic operators, you can handle a wide range of tasks efficiently.</p>
<p>Practice these examples and experiment with your own scripts to unlock the full potential of <code>awk</code>!</p>
<p>The post <a href="https://massive.news/how-to-use-awk-for-arithmetic-in-loops-in-linux/">How to Use awk for Arithmetic in Loops in Linux</a> appeared first on <a href="https://massive.news">MASSIVE News</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Write Scripts Using Awk Programming &#8211; Part 13</title>
		<link>https://massive.news/how-to-write-scripts-using-awk-programming-part-13/</link>
		
		<dc:creator><![CDATA[Editor]]></dc:creator>
		<pubDate>Mon, 09 Sep 2024 05:40:22 +0000</pubDate>
				<category><![CDATA[World News]]></category>
		<category><![CDATA[Awk Command]]></category>
		<guid isPermaLink="false">https://massive.news/how-to-write-scripts-using-awk-programming-part-13/</guid>

					<description><![CDATA[<p>All along from the beginning of the Awk series up to Part 12, we have been...</p>
<p>The post <a href="https://massive.news/how-to-write-scripts-using-awk-programming-part-13/">How to Write Scripts Using Awk Programming &#8211; Part 13</a> appeared first on <a href="https://massive.news">MASSIVE News</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div><img decoding="async" src="https://massive.news/wp-content/uploads/2024/09/how-to-write-scripts-using-awk-programming-part-13.png" class="ff-og-image-inserted"></div>
<p>All along from the beginning of the Awk series up to Part 12, we have been writing small Awk commands and programs on the command line and in shell scripts respectively.</p>
<p>However, <strong>Awk</strong>, just like <strong>Shell</strong>, is also an interpreted language, therefore, with all that we have walked through from the start of this series, you can now write Awk executable scripts.</p>
<p>Similar to how we write a shell script, Awk scripts start with the line:</p>
<pre>#! /path/to/awk/utility -f 
</pre>
<p>For example on my system, the <strong>Awk</strong> utility is located in <strong>/usr/bin/awk</strong>, therefore, I would start an Awk script as follows:</p>
<pre>#! /usr/bin/awk -f 
</pre>
<p>Explaining the line above:</p>
<ul>
<li><code>#!</code> – referred to as <strong>Shebang</strong>, which specifies an interpreter for the instructions in a script</li>
<li><code>/usr/bin/awk</code> – is the interpreter</li>
<li><code>-f</code> – interpreter option, used to read a program file</li>
</ul>
<p><center></p>
<div align="center" id="tecmint_incontent"></div>
<p></center></p>
<p>That said, let us now dive into looking at some examples of Awk executable scripts, we can start with the simple script below. Use your favorite editor to open a new file as follows:</p>
<pre>vi script.awk
</pre>
<p>And paste the code below in the file:</p>
<pre>#!/usr/bin/awk -f 
BEGIN { printf "%sn","Writing my first Awk executable script!" }
</pre>
<p>Save the file and exit, then make the script executable by issuing the command below:</p>
<pre>chmod +x script.awk
</pre>
<p>Thereafter, run it:</p>
<pre>./script.awk
</pre>
<p><strong>Sample Output</strong>:</p>
<pre>Writing my first Awk executable script!
</pre>
<p>A critical programmer out there must be asking, “<strong>Where are the comments?</strong>”, yes, you can also include comments in your Awk script. Writing comments in your code is always a good programming practice.</p>
<p>It helps other programmers look through your code to understand what you are trying to achieve in each section of a script or program file.</p>
<p>Therefore, you can include comments in the script above as follows.</p>
<pre>#!/usr/bin/awk -f 

#This is how to write a comment in Awk
#using the BEGIN special pattern to print a sentence 

BEGIN { printf "%sn","Writing my first Awk executable script!" }
</pre>
<p>Next, we shall look at an example where we read input from a file. We want to search for a system user named <strong>aaronkilik</strong> in the account file, <strong>/etc/passwd</strong>, then print the username, user <strong>ID,</strong> and user <strong>GID</strong> as follows:</p>
<p>Below is the content of our script called <strong>second.awk</strong>.</p>
<pre>#! /usr/bin/awk -f 

#use BEGIN sepecial character to set FS built-in variable
BEGIN { FS=":" }

#search for username: aaronkilik and print account details 
/aaronkilik/ { print "Username :",$1,"User ID :",$3,"User GID :",$4 }
</pre>
<p>Save the file and exit, make the script executable, and execute it as below:</p>
<pre>chmod +x second.awk
./second.awk /etc/passwd
</pre>
<p><strong>Sample Output</strong>:</p>
<pre>Username : aaronkilik User ID : 1000 User GID : 1000
</pre>
<p>In the last example below, we shall use <strong>do while statement</strong> to print out numbers from <strong>0-10</strong>:</p>
<p>Below is the content of our script called <strong>do.awk</strong>.</p>
<pre>#! /usr/bin/awk -f 

#printing from 0-10 using a do while statement 
#do while statement 
BEGIN {
#initialize a counter
x=0

do {
    print x;
    x+=1;
}
while(x&lt;=10)
}
</pre>
<p>After saving the file, make the script executable as we have done before. Afterwards, run it:</p>
<pre>chmod +x do.awk
./do.awk
</pre>
<p><strong>Sample Output</strong>:</p>
<pre>0
1
2
3
4
5
6
7
8
9
10
</pre>
<h5>Summary</h5>
<p>We have come to the end of this interesting Awk series, I hope you have learned a lot from all 13 parts, as an introduction to Awk programming language.</p>
<p>As I mentioned from the beginning, Awk is a complete text processing language, for that reason, you can learn more other aspects of Awk programming language such as environmental variables, arrays, functions (built-in &amp; user-defined), and beyond.</p>
<p>There is yet additional parts of Awk programming to learn and master, so, below, I have provided some links to important online resources that you can use to expand your Awk programming skills, these are not necessarily all that you need, you can also look out for useful Awk programming books.</p>
<p><strong>Reference Links</strong>: The GNU Awk User’s Guide and AWK Language Programming</p>
<p>For any thoughts you wish to share or questions, use the comment form below. Remember to always stay connected to <strong>Tecmint</strong> for more exciting series.</p>
<p>For those seeking a comprehensive resource, we’ve compiled all the <strong>Awk</strong> series articles into a book, that includes 13 chapters and spans 41 pages, covering both basic and advanced Awk usage with practical examples.</p>
<table>
<tbody>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Buy</th>
</tr>
<tr>
<td>eBook: Introducing the Awk Getting Started Guide for Beginners</td>
<td>$8.99</td>
<td>[Buy Now]</td>
</tr>
</tbody>
</table>
<p>The post <a href="https://massive.news/how-to-write-scripts-using-awk-programming-part-13/">How to Write Scripts Using Awk Programming &#8211; Part 13</a> appeared first on <a href="https://massive.news">MASSIVE News</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Use Flow Control Statements in Awk &#8211; Part 12</title>
		<link>https://massive.news/how-to-use-flow-control-statements-in-awk-part-12/</link>
		
		<dc:creator><![CDATA[Editor]]></dc:creator>
		<pubDate>Thu, 22 Aug 2024 07:00:59 +0000</pubDate>
				<category><![CDATA[World News]]></category>
		<category><![CDATA[Awk Command]]></category>
		<guid isPermaLink="false">https://massive.news/how-to-use-flow-control-statements-in-awk-part-12/</guid>

					<description><![CDATA[<p>When you review all the Awk examples we have covered so far, right from the start...</p>
<p>The post <a href="https://massive.news/how-to-use-flow-control-statements-in-awk-part-12/">How to Use Flow Control Statements in Awk &#8211; Part 12</a> appeared first on <a href="https://massive.news">MASSIVE News</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div><img decoding="async" src="https://massive.news/wp-content/uploads/2024/08/how-to-use-flow-control-statements-in-awk-part-12.png" class="ff-og-image-inserted"></div>
<p>When you review all the Awk examples we have covered so far, right from the start of the Awk series, you will notice that all the commands in the various examples are executed sequentially, that is one after the other.</p>
<p>But in certain situations, we may want to run some text filtering operations based on some conditions, that is where the approach of flow control statements comes in.</p>
<p>There are various flow control statements in Awk programming and these include:</p>
<ul>
<li>if-else statement</li>
<li>for statement</li>
<li>while statement</li>
<li>do-while statement</li>
<li>break statement</li>
<li>continue statement</li>
<li>next statement</li>
<li>nextfile statement</li>
<li>exit statement</li>
</ul>
<p>However, for the scope of this series, we shall expound on: <code>if-else</code>, <code>for</code>, <code>while</code> and <code>do while</code> statements. Remember that we already walked through how to use next statement in Part 6 of this Awk series.</p>
<h2>1. The if-else Statement</h2>
<p>The expected syntax of the <code>if statement</code> is similar to that of the shell <strong>if statement</strong>:</p>
<pre>if  (condition1) {
     actions1
}
else {
      actions2
}
</pre>
<p><center></p>
<div align="center" id="tecmint_incontent"></div>
<p></center></p>
<p>In the above syntax, <code>condition1</code> and <code>condition2</code> are Awk expressions, and <code>actions1</code> and <code>actions2</code> are Awk commands executed when the respective conditions are satisfied.</p>
<p>When <code>condition1</code> is satisfied, meaning it’s true, then <code>actions1</code> is executed and the <code>if statement</code> exits, otherwise <code>actions2</code> is executed.</p>
<p>The <code>if statement</code> can also be expanded to a <code>if-else_if-else</code> statement as below:</p>
<pre>if (condition1){
     actions1
}
else if (conditions2){
      actions2
}
else{
     actions3
}
</pre>
<p>In this form, if <code>condition1</code> is true, then <code>actions1</code> is executed and the <code>if statement</code> exits, otherwise <code>condition2</code> is evaluated and if it is true, then <code>actions2</code> is executed and the <code>if statement</code> exits. However, when <code>condition2</code> is false then, <code>actions3</code> is executed and the <code>if statement</code> exits.</p>
<p>Here is a case in point of using <code>if statements</code>, we have a list of <strong>users</strong> and their <strong>ages</strong> stored in the file <strong>users.txt</strong>.</p>
<pre>Sarah L			35    	F
Aaron Kili		40    	M
John  Doo		20    	M
Kili  Seth		49    	M    
</pre>
<p>We want to print a statement indicating a user’s name and whether the user’s age is less or more than <strong>25</strong> years old.</p>
<p>We can write a short shell script to carry out our job above, here is the content of the script:</p>
<pre>#!/bin/bash
awk ' { 
        if ( $3 &lt;= 25 ){
           print "User",$1,$2,"is less than 25 years old." ;
        }
        else {
           print "User",$1,$2,"is more than 25 years old" ; 
}
}'    ~/users.txt
</pre>
<p>Then save the file and exit, make the script executable, and run it as follows:</p>
<pre>chmod +x test.sh
./test.sh
</pre>
<p><strong>Sample Output</strong>:</p>
<pre>User Sarah L is more than 25 years old
User Aaron Kili is more than 25 years old
User John Doo is less than 25 years old.
User Kili Seth is more than 25 years old
</pre>
<h2>2. The for Statement</h2>
<p>In case you want to execute some Awk commands in a loop, then the <code>for statement</code> offers you a suitable way to do that, with the syntax below:</p>
<pre>for ( counter-initialization; test-condition; counter-increment ){
      actions
}
</pre>
<p>Here, the approach is simply defined by the use of a counter to control the loop execution, first, you need to initialize the counter, then run it against a test condition, if it is true, execute the actions, and finally increment the counter. The loop terminates when the counter does not satisfy the condition.</p>
<p>The following Awk command shows how the <code>for statement</code> works, where we want to print the numbers <strong>0-10</strong>:</p>
<pre>awk 'BEGIN{ for(counter=0;counter&lt;=10;counter++){ print counter} }'
</pre>
<p><strong>Sample Output</strong>:</p>
<pre>0
1
2
3
4
5
6
7
8
9
10
</pre>
<h2>3. The while Statement</h2>
<p>The conventional syntax of the <code>while statement</code> is as follows:</p>
<pre>while ( condition ) {
          actions
}
</pre>
<p>The condition is an Awk expression and actions are lines of Awk commands executed when the condition is true.</p>
<p>Below is a script to illustrate the use of <strong>while statement</strong> to print the numbers <strong>0-10</strong>:</p>
<pre>#!/bin/bash
awk ' BEGIN{ counter=0 ;
         
        while(counter&lt;=10){
              print counter;
              counter+=1 ;
             
}
}  
</pre>
<p>Save the file and make the script executable, then run it:</p>
<pre>chmod +x test.sh
./test.sh
</pre>
<p><strong>Sample Output</strong>:</p>
<pre>0
1
2
3
4
5
6
7
8
9
10
</pre>
<h2>4. The do while Statement</h2>
<p>The <code>do-while</code> statement is a modification of the <code>while</code> statement with the following syntax:</p>
<pre>do {
     actions
}
 while (condition) 
</pre>
<p>The slight difference is that, under <code>do while</code>, the Awk commands are executed before the condition is evaluated. Using the very example under <code>while statement</code> above, we can illustrate the use of <code>do while</code> by altering the Awk command in the <strong>test.sh</strong> script as follows:</p>
<pre>#!/bin/bash

awk ' BEGIN{ counter=0 ;  
        do{
            print counter;  
            counter+=1 ;    
        }
        while (counter&lt;=10)   
} 
'
</pre>
<p>After modifying the script, save the file and exit. Then make the script executable and execute it as follows:</p>
<pre>chmod +x test.sh
./test.sh
</pre>
<p><strong>Sample Output</strong>:</p>
<pre>0
1
2
3
4
5
6
7
8
9
10
</pre>
<h5>Conclusion</h5>
<p>This is not a comprehensive guide regarding Awk flow control statements, as I had mentioned earlier on, there are several other <strong>flow control statements</strong> in Awk.</p>
<p>Nonetheless, this part of the Awk series should give you a clear fundamental idea of how the execution of Awk commands can be controlled based on certain conditions.</p>
<p>You can also expound more on the rest of the flow control statements to gain more understanding of the subject matter. Finally, in the next section of the Awk series, we shall move into writing Awk scripts.</p>
<p>For those seeking a comprehensive resource, we’ve compiled all the <strong>Awk</strong> series articles into a book, that includes 13 chapters and spans 41 pages, covering both basic and advanced Awk usage with practical examples.</p>
<table>
<tbody>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Buy</th>
</tr>
<tr>
<td>eBook: Introducing the Awk Getting Started Guide for Beginners</td>
<td>$8.99</td>
<td>[Buy Now]</td>
</tr>
</tbody>
</table>
<p>The post <a href="https://massive.news/how-to-use-flow-control-statements-in-awk-part-12/">How to Use Flow Control Statements in Awk &#8211; Part 12</a> appeared first on <a href="https://massive.news">MASSIVE News</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
