<?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 Archives - Tech Chronicles</title>
	<atom:link href="http://kostacipo.stream/tag/awk/feed/" rel="self" type="application/rss+xml" />
	<link>http://kostacipo.stream/tag/awk/</link>
	<description>Ramblings of a Tech Dude</description>
	<lastBuildDate>Mon, 09 Dec 2019 11:06:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>

<image>
	<url>https://kostacipo.stream/wp-content/uploads/2019/12/cropped-profile-32x32.jpg</url>
	<title>awk Archives - Tech Chronicles</title>
	<link>http://kostacipo.stream/tag/awk/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Getting started with awk, a powerful text-parsing tool</title>
		<link>http://kostacipo.stream/getting-started-with-awk-a-powerful-text-parsing-tool/</link>
					<comments>http://kostacipo.stream/getting-started-with-awk-a-powerful-text-parsing-tool/#respond</comments>
		
		<dc:creator><![CDATA[Majordomo]]></dc:creator>
		<pubDate>Mon, 09 Dec 2019 11:02:13 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tools]]></category>
		<guid isPermaLink="false">http://www.kostacipo.stream/?p=1436</guid>

					<description><![CDATA[<p>&#160; Awk is a powerful text-parsing tool for Unix and Unix-like systems, but because it has programmed functions that you can use to perform common parsing tasks, it&#8217;s also considered a programming language. You probably won&#8217;t be developing your next GUI application with awk, and it likely won&#8217;t take the place of your default scripting [&#8230;]</p>
<p>The post <a href="http://kostacipo.stream/getting-started-with-awk-a-powerful-text-parsing-tool/">Getting started with awk, a powerful text-parsing tool</a> appeared first on <a href="http://kostacipo.stream">Tech Chronicles</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>&nbsp;</p>
<p id="introduction-to-awk">Awk is a powerful text-parsing tool for Unix and Unix-like systems, but because it has programmed functions that you can use to perform common parsing tasks, it&#8217;s also considered a programming language. You probably won&#8217;t be developing your next GUI application with awk, and it likely won&#8217;t take the place of your default scripting language, but it&#8217;s a powerful utility for specific tasks.</p>
<p>What those tasks may be is surprisingly diverse. The best way to discover which of your problems might be best solved by awk is to learn awk; you&#8217;ll be surprised at how awk can help you get more done but with a lot less effort.</p>
<p>Awk&#8217;s basic syntax is:</p>
<pre><span class="geshifilter"><code class="bash geshifilter-bash"><span class="kw2">awk</span> <span class="br0">[</span>options<span class="br0">]</span> <span class="st_h">'pattern {action}'</span> <span class="kw2">file</span></code></span></pre>
<p>To get started, create this sample file and save it as <strong>colours.txt</strong></p>
<div class="geshifilter">
<div class="text geshifilter-text">name &nbsp; &nbsp; &nbsp; color &nbsp;amount<br />apple &nbsp; &nbsp; &nbsp;red &nbsp; &nbsp;4<br />banana &nbsp; &nbsp; yellow 6<br />strawberry red &nbsp; &nbsp;3<br />grape &nbsp; &nbsp; &nbsp;purple 10<br />apple &nbsp; &nbsp; &nbsp;green &nbsp;8<br />plum &nbsp; &nbsp; &nbsp; purple 2<br />kiwi &nbsp; &nbsp; &nbsp; brown &nbsp;4<br />potato &nbsp; &nbsp; brown &nbsp;9<br />pineapple &nbsp;yellow 5</p>
</div>
</div>
<p>This data is separated into columns by one or&nbsp;more spaces. It&#8217;s common for data that you are analyzing to be organized in some way. It may not always be columns separated by whitespace, or even a comma or semicolon, but especially in log files or data dumps, there&#8217;s generally a predictable pattern.&nbsp;You can use patterns of data to help awk extract and process the data that you want to focus on.</p>
<h2 id="printing-a-column">Printing a column</h2>
<p>In awk, the <strong>print</strong> function displays whatever you specify. There are many predefined variables you can use, but some of the most common are integers designating columns in a text file. Try it out:</p>
<div class="geshifilter">
<div class="bash geshifilter-bash">$ <span class="kw2">awk</span> <span class="st_h">&#8216;{print $2;}&#8217;</span> colours.txt<br />color<br /><span class="kw2">red</span><br />yellow<br /><span class="kw2">red</span><br />purple<br />green<br />purple<br />brown<br />brown<br />yellow</div>
</div>
<p>&nbsp;</p>
<p>In this case, awk displays the second column, denoted by <strong>$2</strong>. This is relatively intuitive, so you can probably guess that <strong>print $1</strong> displays the first column, and <strong>print $3</strong> displays the third, and so on.</p>
<p>To display <em>all</em> columns, use <strong>$0</strong>.</p>
<p>The number after the dollar sign (<strong>$</strong>) is an <em>expression</em>, so <strong>$2</strong> and <strong>$(1+1)</strong> mean the same thing.</p>
<h2 id="conditionally-selecting-columns">Conditionally selecting columns</h2>
<p>The example file you&#8217;re using is very structured. It has a row that serves as a header, and the columns relate directly to one another. By defining <em>conditional</em> requirements, you can qualify what you want awk to return when looking at this data. For instance, to view items in column 2 that match &#8220;yellow&#8221; and print the contents of column 1:</p>
<div class="geshifilter">
<div class="bash geshifilter-bash"><span class="kw2">awk</span> <span class="st_h">&#8216;$2==&#8221;yellow&#8221;{print $1}&#8217;</span> colours.txt<br />banana<br />pineapple</div>
</div>
<p>&nbsp;</p>
<p>Regular expressions work as well. This conditional looks at <strong>$2</strong> for approximate matches to the letter <strong>p</strong> followed by any number of (one or more) characters, which are in turn followed by the letter <strong>p</strong>:</p>
<div class="geshifilter">
<div class="bash geshifilter-bash">$ <span class="kw2">awk</span> <span class="st_h">&#8216;$2 ~ /p.+p/ {print $0}&#8217;</span> colours.txt<br />grape &nbsp; purple &nbsp;<span class="nu0">10</span><br />plum &nbsp; &nbsp;purple &nbsp;<span class="nu0">2</span></div>
</div>
<p>&nbsp;</p>
<p>Numbers are interpreted naturally by awk. For instance, to print any row with a third column containing an integer greater than 5:</p>
<div class="geshifilter">
<div class="bash geshifilter-bash"><span class="kw2">awk</span> <span class="st_h">&#8216;$3&gt;5 {print $1, $2}&#8217;</span> colours.txt<br />name &nbsp; &nbsp;color<br />banana &nbsp;yellow<br />grape &nbsp; purple<br />apple &nbsp; green<br />potato &nbsp;brown</div>
</div>
<h2 id="field-separator">Field separator</h2>
<p>By default, awk uses whitespace as the field separator. Not all text files use whitespace to define fields, though. For example, create a file called <strong>colours.csv</strong> with this content:</p>
<div class="geshifilter">
<div class="text geshifilter-text">name,color,amount<br />apple,red,4<br />banana,yellow,6<br />strawberry,red,3<br />grape,purple,10<br />apple,green,8<br />plum,purple,2<br />kiwi,brown,4<br />potato,brown,9<br />pineapple,yellow,5</div>
</div>
<p>&nbsp;</p>
<p>Awk can treat the data in exactly the same way, as long as you specify which character it should use as the field separator in your command. Use the <strong>&#8211;field-separator</strong> (or just <strong>-F</strong> for short) option to define the delimiter:</p>
<div class="geshifilter">
<div class="bash geshifilter-bash">$ <span class="kw2">awk</span> <span class="re5">-F</span><span class="st0">&#8220;,&#8221;</span> <span class="st_h">&#8216;$2==&#8221;yellow&#8221; {print $1}&#8217;</span> file1.csv<br />banana<br />pineapple</div>
</div>
<h2 id="saving-output">Saving output</h2>
<p>Using output redirection, you can write your results to a file. For example:</p>
<pre><span class="geshifilter"><code class="bash geshifilter-bash"><span class="co4">$ </span><span class="kw2">awk</span> -F, <span class="st_h">'$3&gt;5 {print $1, $2} colours.csv &gt; output.txt</span></code></span></pre>
<p>This creates a file with the contents of your awk query.</p>
<p>You can also split a file into multiple files&nbsp;grouped by column data. For example, if you want to split colours.txt into multiple files according to what color appears in each row, you can cause awk to redirect <em>per query</em> by including the redirection in your awk statement:</p>
<pre><span class="geshifilter"><code class="bash geshifilter-bash"><span class="co4">$ </span><span class="kw2">awk</span> <span class="st_h">'{print &gt; $2".txt"}'</span> colours.txt</code></span></pre>
<p>This produces files named <strong>yellow.txt</strong>, <strong>red.txt</strong>, and so on.</p>
<p>The post <a href="http://kostacipo.stream/getting-started-with-awk-a-powerful-text-parsing-tool/">Getting started with awk, a powerful text-parsing tool</a> appeared first on <a href="http://kostacipo.stream">Tech Chronicles</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://kostacipo.stream/getting-started-with-awk-a-powerful-text-parsing-tool/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
