Skip to content

Commit 8527f05

Browse files
added 29th cli tip
1 parent 361ba8f commit 8527f05

File tree

10 files changed

+77
-8
lines changed

10 files changed

+77
-8
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ I'm addicted to reading fantasy/sci-fi books, so I have a [blog](https://learnby
6868

6969
## Tips
7070

71+
* [CLI tip 29: define fields using FPAT in GNU awk](https://learnbyexample.github.io/tips/cli-tip-29/)
7172
* [Python tip 29: negative lookarounds](https://learnbyexample.github.io/tips/python-tip-29/)
7273
* [Vim tip 27: regexp anchors](https://learnbyexample.github.io/tips/vim-tip-27/)
7374
* [CLI tip 28: substitute specific occurrence with GNU sed](https://learnbyexample.github.io/tips/cli-tip-28/)

Diff for: atom.xml

+42-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,49 @@
55
<link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/>
66
<link href="https://learnbyexample.github.io"/>
77
<generator uri="https://www.getzola.org/">Zola</generator>
8-
<updated>2023-06-13T00:00:00+00:00</updated>
8+
<updated>2023-06-20T00:00:00+00:00</updated>
99
<id>https://learnbyexample.github.io/atom.xml</id>
10+
<entry xml:lang="en">
11+
<title>CLI tip 29: define fields using FPAT in GNU awk</title>
12+
<published>2023-06-20T00:00:00+00:00</published>
13+
<updated>2023-06-20T00:00:00+00:00</updated>
14+
<link rel="alternate" href="https://learnbyexample.github.io/tips/cli-tip-29/" type="text/html"/>
15+
<id>https://learnbyexample.github.io/tips/cli-tip-29/</id>
16+
<content type="html">&lt;p&gt;In &lt;code&gt;awk&lt;&#x2F;code&gt;, the &lt;code&gt;FS&lt;&#x2F;code&gt; variable allows you to define the input field &lt;em&gt;separator&lt;&#x2F;em&gt;. In contrast, &lt;code&gt;FPAT&lt;&#x2F;code&gt; (field pattern) allows you to define what should the fields be made up of.&lt;&#x2F;p&gt;
17+
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;$ s=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;Sample123string42with777numbers&amp;#39;
18+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# one or more consecutive digits
19+
&lt;&#x2F;span&gt;&lt;span&gt;$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;$s&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; awk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;v &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c23f31;&quot;&gt;FPAT&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;[0-9]+&amp;#39; &amp;#39;{print $2}&amp;#39;
20+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;42
21+
&lt;&#x2F;span&gt;&lt;span&gt;
22+
&lt;&#x2F;span&gt;&lt;span&gt;$ s=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;coat Bin food tar12 best Apple fig_42&amp;#39;
23+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# whole words made up of lowercase alphabets and digits only
24+
&lt;&#x2F;span&gt;&lt;span&gt;$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;$s&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; awk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;v &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c23f31;&quot;&gt;FPAT&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#aeb52b;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;lt;[a-z0-9]+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#aeb52b;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;v &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c23f31;&quot;&gt;OFS&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;{$1=$1} 1&amp;#39;
25+
&lt;&#x2F;span&gt;&lt;span&gt;coat,food,tar12,best
26+
&lt;&#x2F;span&gt;&lt;span&gt;
27+
&lt;&#x2F;span&gt;&lt;span&gt;$ s=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;items: &amp;quot;apple&amp;quot; and &amp;quot;mango&amp;quot;&amp;#39;
28+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# get the first double quoted item
29+
&lt;&#x2F;span&gt;&lt;span&gt;$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;$s&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; awk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;v &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c23f31;&quot;&gt;FPAT&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;&amp;quot;[^&amp;quot;]+&amp;quot;&amp;#39; &amp;#39;{print $1}&amp;#39;
30+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;apple&amp;quot;
31+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
32+
&lt;p&gt;&lt;code&gt;FPAT&lt;&#x2F;code&gt; is often used for CSV input where fields can contain embedded delimiter characters. For example, a field content &lt;code&gt;&amp;quot;fox,42&amp;quot;&lt;&#x2F;code&gt; when &lt;code&gt;,&lt;&#x2F;code&gt; is the delimiter.&lt;&#x2F;p&gt;
33+
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;$ s=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;eagle,&amp;quot;fox,42&amp;quot;,bee,frog&amp;#39;
34+
&lt;&#x2F;span&gt;&lt;span&gt;
35+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# simply using , as separator isn&amp;#39;t sufficient
36+
&lt;&#x2F;span&gt;&lt;span&gt;$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;$s&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; awk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;{print $2}&amp;#39;
37+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;fox
38+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
39+
&lt;p&gt;For such simpler CSV input, &lt;code&gt;FPAT&lt;&#x2F;code&gt; helps to define fields as starting and ending with double quotes or containing non-comma characters.&lt;&#x2F;p&gt;
40+
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# * is used instead of + to allow empty fields
41+
&lt;&#x2F;span&gt;&lt;span&gt;$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;$s&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; awk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;v &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c23f31;&quot;&gt;FPAT&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;&amp;quot;[^&amp;quot;]*&amp;quot;|[^,]*&amp;#39; &amp;#39;{print $2}&amp;#39;
42+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;fox,42&amp;quot;
43+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
44+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;warning.svg&quot; alt=&quot;warning&quot; &#x2F;&gt; The above will not work for all kinds of CSV files, for example if fields contain escaped double quotes, newline characters, etc. See &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;q&#x2F;45420535&#x2F;4082052&quot;&gt;stackoverflow: What&#x27;s the most robust way to efficiently parse CSV using awk?&lt;&#x2F;a&gt; for such cases. You could also use other programming languages such as Perl, Python, Ruby, etc which come with standard CSV parsing libraries or have easy access to third party solutions. There are also specialized command line tools such as &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;BurntSushi&#x2F;xsv&quot;&gt;xsv&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
45+
&lt;p&gt;&lt;strong&gt;Video demo&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
46+
&lt;p align=&quot;center&quot;&gt;&lt;iframe width=&quot;560&quot; height=&quot;315&quot; loading=&quot;lazy&quot; src=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;embed&#x2F;1ZQni88a99w&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;&#x2F;iframe&gt;&lt;&#x2F;p&gt;
47+
&lt;br&gt;
48+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;info.svg&quot; alt=&quot;info&quot; &#x2F;&gt; See also my &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;learnbyexample&#x2F;learn_gnuawk&quot;&gt;GNU awk&lt;&#x2F;a&gt; ebook.&lt;&#x2F;p&gt;
49+
</content>
50+
</entry>
1051
<entry xml:lang="en">
1152
<title>Python tip 29: negative lookarounds</title>
1253
<published>2023-06-13T00:00:00+00:00</published>

Diff for: sitemap.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@
428428
</url>
429429
<url>
430430
<loc>https://learnbyexample.github.io/tips/</loc>
431-
<lastmod>2023-06-13</lastmod>
431+
<lastmod>2023-06-20</lastmod>
432432
</url>
433433
<url>
434434
<loc>https://learnbyexample.github.io/tips/cli-tip-1/</loc>
@@ -514,6 +514,10 @@
514514
<loc>https://learnbyexample.github.io/tips/cli-tip-28/</loc>
515515
<lastmod>2023-05-30</lastmod>
516516
</url>
517+
<url>
518+
<loc>https://learnbyexample.github.io/tips/cli-tip-29/</loc>
519+
<lastmod>2023-06-20</lastmod>
520+
</url>
517521
<url>
518522
<loc>https://learnbyexample.github.io/tips/cli-tip-3/</loc>
519523
<lastmod>2022-06-14</lastmod>

0 commit comments

Comments
 (0)