Skip to content

Commit d305e94

Browse files
updates for version 2.5
1 parent d4c661b commit d305e94

20 files changed

+176
-97
lines changed

Exercise_solutions.html

Lines changed: 40 additions & 15 deletions
Large diffs are not rendered by default.

awk-introduction.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
# filter lines NOT containing 'e'
4646
$ printf 'gate\napple\nwhat\nkite\n' | awk '!/e/'
4747
what
48-
</code></pre><p>By default, <code>awk</code> automatically loops over the input content line by line. You can then use programming instructions to process those lines. As <code>awk</code> is often used from the command line, many shortcuts are available to reduce the amount of typing needed.<p>In the above examples, a regular expression (defined by the pattern between a pair of forward slashes) has been used to filter the input. Regular expressions (regexp) will be covered in detail in the <a href=./regular-expressions.html#regular-expressions>next chapter</a>. String values without any special regexp characters are used in this chapter. The full syntax is <code>string ~ /regexp/</code> to check if the given string matches the regexp and <code>string !~ /regexp/</code> to check if doesn't match. When the string isn't specified, the test is performed against a special variable <code>$0</code>, which has the contents of the input line. The correct term would be input <strong>record</strong>, but that's a discussion for a <a href=./record-separators.html#record-separators>later chapter</a>.<p>Also, in the above examples, only the filtering condition was given. By default, when the condition evaluates to <code>true</code>, the contents of <code>$0</code> is printed. Thus:<ul><li><code>awk '/regexp/'</code> is a shortcut for <code>awk '$0 ~ /regexp/{print $0}'</code><li><code>awk '!/regexp/'</code> is a shortcut for <code>awk '$0 !~ /regexp/{print $0}'</code></ul><pre><code class=language-bash># same as: awk '/at/'
48+
</code></pre><p>By default, <code>awk</code> automatically loops over the input content line by line. You can then use programming instructions to process those lines. As <code>awk</code> is often used from the command line, many shortcuts are available to reduce the amount of typing needed.<p>In the above examples, a regular expression (defined by the pattern between a pair of forward slashes) has been used to filter the input. Regular expressions (regexp) will be covered in detail in the <a href=./regular-expressions.html#regular-expressions>next chapter</a>. String values without any special regexp characters are used in this chapter. The full syntax is <code>string ~ /regexp/</code> to check if the given string matches the regexp and <code>string !~ /regexp/</code> to invert the condition. When the string isn't specified, the test is performed against a special variable <code>$0</code>, which has the contents of the input line. The correct term would be input <strong>record</strong>, but that's a discussion for a <a href=./record-separators.html#record-separators>later chapter</a>.<p>Also, in the above examples, only the filtering condition was given. By default, when the condition evaluates to <code>true</code>, the contents of <code>$0</code> is printed. Thus:<ul><li><code>awk '/regexp/'</code> is a shortcut for <code>awk '$0 ~ /regexp/{print $0}'</code><li><code>awk '!/regexp/'</code> is a shortcut for <code>awk '$0 !~ /regexp/{print $0}'</code></ul><pre><code class=language-bash># same as: awk '/at/'
4949
$ printf 'gate\napple\nwhat\nkite\n' | awk '$0 ~ /at/{print $0}'
5050
gate
5151
what
@@ -121,7 +121,7 @@
121121
# strings placed next to each other are concatenated
122122
$ awk 'BEGIN{s1="con"; s2="cat"; print s1 s2}'
123123
concat
124-
</code></pre><p>If an uninitialized variable is used, it will act as an empty string in string context and <code>0</code> in numeric context. You can force a string to behave as a number by simply using it in an expression with numeric values. You can also use unary <code>+</code> or <code>-</code> operators. If the string doesn't start with a valid number (ignoring any starting whitespaces), it will be treated as <code>0</code>. Similarly, concatenating a string to a number will automatically change the number to string. See <a href=https://www.gnu.org/software/gawk/manual/gawk.html#Strings-And-Numbers>gawk manual: How awk Converts Between Strings and Numbers</a> for more details.<pre><code class=language-bash># same as: awk 'BEGIN{sum=0} {sum += $NF} END{print sum}'
124+
</code></pre><p>If an uninitialized variable is used, it will act as an empty string in string context and <code>0</code> in numeric context. You can force a string to behave as a number by simply using it in an expression with numeric values. You can also use unary <code>+</code> or <code>-</code> operators. If the string doesn't start with a valid number (ignoring any starting whitespaces), it will be treated as <code>0</code>. Similarly, concatenating a string to a number will automatically change the number to a string. See <a href=https://www.gnu.org/software/gawk/manual/gawk.html#Strings-And-Numbers>gawk manual: How awk Converts Between Strings and Numbers</a> for more details.<pre><code class=language-bash># same as: awk 'BEGIN{sum=0} {sum += $NF} END{print sum}'
125125
$ awk '{sum += $NF} END{print sum}' table.txt
126126
38.14
127127

built-in-functions.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121

122122
$ echo "$s" | awk '{patsplit($0, a, /"[^"]*"|[^,]*/); print a[2]}'
123123
"fox,42"
124-
</code></pre><h2 id=substr><a class=header href=#substr>substr</a></h2><p>The <code>substr</code> function helps to extract a specified number of characters from an input string based on indexing. The argument order is:<ul><li>First argument is the input string<li>Second argument is the starting position<li>Third argument is the number of characters to extract</ul><p>The index starts from <code>1</code>. If the third argument is not specified, by default all characters until the end of the string is extracted. If the second argument is greater than the length of the string or if the third argument is less than or equal to <code>0</code>, then an empty string is returned. The second argument will be converted <code>1</code> if a number less than one is specified.<pre><code class=language-bash>$ echo 'abcdefghij' | awk '{print substr($0, 1, 5)}'
124+
</code></pre><h2 id=substr><a class=header href=#substr>substr</a></h2><p>The <code>substr</code> function helps to extract a specified number of characters from an input string based on indexing. The argument order is:<ul><li>First argument is the input string<li>Second argument is the starting position<li>Third argument is the number of characters to extract</ul><p>The index starts from <code>1</code>. If the third argument is not specified, by default all characters until the end of the string is extracted. If the second argument is greater than the length of the string or if the third argument is less than or equal to <code>0</code>, then an empty string is returned. The second argument will be converted to <code>1</code> if a number less than one is specified.<pre><code class=language-bash>$ echo 'abcdefghij' | awk '{print substr($0, 1, 5)}'
125125
abcde
126126
$ echo 'abcdefghij' | awk '{print substr($0, 4, 3)}'
127127
def
@@ -182,10 +182,10 @@
182182
# end of string
183183
$ awk -v s="a+b" 'index($0, s)==length()-length(s)+1' eqns.txt
184184
i*(t+9-g)/8,4-a+b
185-
</code></pre><p>Recall that the <code>-v</code> option gets parsed by <code>awk</code>'s string processing rules. So, if you need to pass a literal string without falling in backslash hell, use <code>ENVIRON</code> instead.<pre><code class=language-bash>$ echo 'a\b\c\d' | awk -v s='a\b' 'index($0, s)'
186-
$ echo 'a\b\c\d' | awk -v s='a\\b' 'index($0, s)'
185+
</code></pre><p>Recall that the <code>-v</code> option gets parsed by <code>awk</code>'s string processing rules. So, if you need to pass a literal string without falling in backslash hell, use <code>ENVIRON</code> instead.<pre><code class=language-bash>$ printf '%s\n' 'a\b\c\d' | awk -v s='a\b' 'index($0, s)'
186+
$ printf '%s\n' 'a\b\c\d' | awk -v s='a\\b' 'index($0, s)'
187187
a\b\c\d
188-
$ echo 'a\b\c\d' | s='a\b' awk 'index($0, ENVIRON["s"])'
188+
$ printf '%s\n' 'a\b\c\d' | s='a\b' awk 'index($0, ENVIRON["s"])'
189189
a\b\c\d
190190
</code></pre><h2 id=system><a class=header href=#system>system</a></h2><p>External commands can be issued using the <code>system</code> function. Any output generated by the external command would be as usual on <code>stdout</code> unless redirected while calling the command.<pre><code class=language-bash>$ awk 'BEGIN{system("echo Hello World")}'
191191
Hello World
@@ -318,7 +318,7 @@
318318
Er,56,79,92
319319
Ort,68,72,66
320320
Blue,67,46,99
321-
</code></pre><p><strong>2)</strong> For the input file <code>nums3.txt</code>, calculate the square root of numbers and display the results in two different formats as shown below. First, with four digits after the fractional point and then in the scientific notation, again with four digits after the fractional point. Assume that the input has only a single column of positive numbers.<pre><code class=language-bash>$ cat nums3.txt
321+
</code></pre><p><strong>2)</strong> For the input file <code>nums3.txt</code>, calculate the square root of numbers and display the results in two different formats as shown below. First, with four digits after the fractional point and then in the scientific notation, again with four digits after the fractional point. Assume that the input has only a single column of positive numbers.<pre><code class=language-bash>$ cat nums3.txt
322322
3.14
323323
4201
324324
777
@@ -359,9 +359,9 @@
359359

360360
$ s='\&/'
361361
# should be no output for this input
362-
$ echo 'f\&z\&2.14' | ##### add your solution here
362+
$ printf '%s\n' 'f\&z\&2.14' | ##### add your solution here
363363
# but this one has a match
364-
$ echo 'f\&z\&/2.14' | ##### add your solution here
364+
$ printf '%s\n' 'f\&z\&/2.14' | ##### add your solution here
365365
\&/2.14
366366
</code></pre><p><strong>6)</strong> Extract all positive integers preceded by <code>-</code> and followed by <code>:</code> or <code>;</code>. Display the matching portions separated by a newline character.<pre><code class=language-bash>$ s='42 apple-5; fig3; x-83, y-20:-34; f12'
367367
$ echo "$s" | awk ##### add your solution here

buy.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
2929
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
3030
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
31-
});</script><div class=content id=content><main><div class=sidetoc><nav class=pagetoc></nav></div><h1 id=buy-pdfepub-versions><a class=header href=#buy-pdfepub-versions>Buy PDF/EPUB versions</a></h1><h2 id=purchase-links><a class=header href=#purchase-links>Purchase links</a></h2><p>You can buy the pdf/epub versions of the book using these links:<ul><li><a href=https://learnbyexample.gumroad.com/l/gnu_awk>https://learnbyexample.gumroad.com/l/gnu_awk</a><li><a href=https://leanpub.com/gnu_awk>https://leanpub.com/gnu_awk</a></ul><h2 id=bundles><a class=header href=#bundles>Bundles</a></h2><p>You can also get the book as part of these bundles:<ul><li><strong>Magical one-liners</strong> <ul><li><a href=https://learnbyexample.gumroad.com/l/oneliners>https://learnbyexample.gumroad.com/l/oneliners</a><li><a href=https://leanpub.com/b/oneliners>https://leanpub.com/b/oneliners</a></ul><li><strong>Awesome Regex</strong> <ul><li><a href=https://learnbyexample.gumroad.com/l/regex>https://learnbyexample.gumroad.com/l/regex</a><li><a href=https://leanpub.com/b/regex>https://leanpub.com/b/regex</a></ul><li><strong>All books bundle</strong> <ul><li><a href=https://learnbyexample.gumroad.com/l/all-books>https://learnbyexample.gumroad.com/l/all-books</a><li>Includes all my programming books</ul></ul><h2 id=testimonials><a class=header href=#testimonials>Testimonials</a></h2><blockquote><p>Step up your cli fu with this fabulous intro & deep dive into awk. I learned a ton of tricks!<p><a href=https://twitter.com/killchain/status/1246820137455452163>feedback on twitter</a></blockquote><blockquote><p>I consider myself pretty experienced at shell-fu and capable of doing most things I set out to achieve in either bash scripts or fearless one-liners. However, my awk is rudimentary at best, I think mostly because it's such an unforgiving environment to experiment in.<p>These books you've written are great for a bit of first principles insight and then quickly building up to functional usage. I will have no hesitation in referring colleagues to them!<p><a href=https://news.ycombinator.com/item?id=31930840>feedback on Hacker News</a></blockquote><h2 id=book-list><a class=header href=#book-list>Book list</a></h2><p>Here's a list of programming books I've written:<ul><li><a href=https://learnbyexample.github.io/py_regular_expressions/>Understanding Python re(gex)?</a><li><a href=https://learnbyexample.github.io/learn_js_regexp/>JavaScript RegExp</a><li><a href=https://learnbyexample.github.io/Ruby_Regexp/>Ruby Regexp</a><li><a href=https://learnbyexample.github.io/learn_gnugrep_ripgrep/>CLI text processing with GNU grep and ripgrep</a><li><a href=https://learnbyexample.github.io/learn_gnused/>CLI text processing with GNU sed</a><li><a href=https://learnbyexample.github.io/learn_gnuawk/>CLI text processing with GNU awk</a><li><a href=https://learnbyexample.github.io/learn_ruby_oneliners/>Ruby one-liners cookbook</a><li><a href=https://learnbyexample.github.io/learn_perl_oneliners/>Perl one-liners cookbook</a><li><a href=https://learnbyexample.github.io/100_page_python_intro/>100 Page Python Intro</a><li><a href=https://learnbyexample.github.io/practice_python_projects/>Practice Python Projects</a><li><a href=https://learnbyexample.github.io/cli_text_processing_coreutils/>Command line text processing with GNU Coreutils</a><li><a href=https://learnbyexample.github.io/cli_text_processing_rust/>Command line text processing with Rust tools</a><li><a href=https://learnbyexample.github.io/vim_reference/>Vim reference guide</a><li><a href=https://learnbyexample.github.io/cli-computing/>Computing from the Command Line</a></ul></main><nav aria-label="Page navigation"class=nav-wrapper><a aria-label="Previous chapter"class="mobile-nav-chapters previous"title="Previous chapter"aria-keyshortcuts=Left href=cover.html rel=prev> <i class="fa fa-angle-left"></i> </a><a aria-label="Next chapter"class="mobile-nav-chapters next"title="Next chapter"aria-keyshortcuts=Right href=preface.html rel=next> <i class="fa fa-angle-right"></i> </a><div style="clear: both"></div></nav></div></div><nav aria-label="Page navigation"class=nav-wide-wrapper><a aria-label="Previous chapter"class="nav-chapters previous"title="Previous chapter"aria-keyshortcuts=Left href=cover.html rel=prev> <i class="fa fa-angle-left"></i> </a><a aria-label="Next chapter"class="nav-chapters next"title="Next chapter"aria-keyshortcuts=Right href=preface.html rel=next> <i class="fa fa-angle-right"></i> </a></nav></div><script>window.playground_copyable = true;</script><script charset=utf-8 src=elasticlunr.min.js></script><script charset=utf-8 src=mark.min.js></script><script charset=utf-8 src=searcher.js></script><script charset=utf-8 src=clipboard.min.js></script><script charset=utf-8 src=highlight.js></script><script charset=utf-8 src=book.js></script><script src=sidebar.js></script>
31+
});</script><div class=content id=content><main><div class=sidetoc><nav class=pagetoc></nav></div><h1 id=buy-pdfepub-versions><a class=header href=#buy-pdfepub-versions>Buy PDF/EPUB versions</a></h1><h2 id=purchase-links><a class=header href=#purchase-links>Purchase links</a></h2><p>You can buy the pdf/epub versions of the book using these links:<ul><li><a href=https://leanpub.com/gnu_awk>https://leanpub.com/gnu_awk</a><li><a href=https://learnbyexample.gumroad.com/l/gnu_awk>https://learnbyexample.gumroad.com/l/gnu_awk</a></ul><h2 id=bundles><a class=header href=#bundles>Bundles</a></h2><p>You can also get the book as part of these bundles:<ul><li><strong>All books bundle</strong> <ul><li><a href=https://leanpub.com/b/learnbyexample-all-books>https://leanpub.com/b/learnbyexample-all-books</a><li><a href=https://learnbyexample.gumroad.com/l/all-books>https://learnbyexample.gumroad.com/l/all-books</a></ul><li><strong>Linux CLI Text Processing</strong> <ul><li><a href=https://leanpub.com/b/linux-cli-text-processing>https://leanpub.com/b/linux-cli-text-processing</a><li><a href=https://learnbyexample.gumroad.com/l/linux-cli-text-processing>https://learnbyexample.gumroad.com/l/linux-cli-text-processing</a></ul><li><strong>Magical one-liners</strong> <ul><li><a href=https://leanpub.com/b/oneliners>https://leanpub.com/b/oneliners</a><li><a href=https://learnbyexample.gumroad.com/l/oneliners>https://learnbyexample.gumroad.com/l/oneliners</a></ul><li><strong>Awesome Regex</strong> <ul><li><a href=https://leanpub.com/b/regex>https://leanpub.com/b/regex</a><li><a href=https://learnbyexample.gumroad.com/l/regex>https://learnbyexample.gumroad.com/l/regex</a></ul></ul><h2 id=testimonials><a class=header href=#testimonials>Testimonials</a></h2><blockquote><p>Step up your cli fu with this fabulous intro & deep dive into awk. I learned a ton of tricks!<p><a href=https://twitter.com/killchain/status/1246820137455452163>feedback on twitter</a></blockquote><blockquote><p>I consider myself pretty experienced at shell-fu and capable of doing most things I set out to achieve in either bash scripts or fearless one-liners. However, my awk is rudimentary at best, I think mostly because it's such an unforgiving environment to experiment in.<p>These books you've written are great for a bit of first principles insight and then quickly building up to functional usage. I will have no hesitation in referring colleagues to them!<p><a href=https://news.ycombinator.com/item?id=31930840>feedback on Hacker News</a></blockquote><h2 id=book-list><a class=header href=#book-list>Book list</a></h2><p>Here's a list of programming books I've written:<ul><li><a href=https://learnbyexample.github.io/py_regular_expressions/>Understanding Python re(gex)?</a><li><a href=https://learnbyexample.github.io/learn_js_regexp/>Understanding JavaScript RegExp</a><li><a href=https://learnbyexample.github.io/Ruby_Regexp/>Understanding Ruby Regexp</a><li><a href=https://learnbyexample.github.io/learn_gnugrep_ripgrep/>CLI text processing with GNU grep and ripgrep</a><li><a href=https://learnbyexample.github.io/learn_gnused/>CLI text processing with GNU sed</a><li><a href=https://learnbyexample.github.io/learn_gnuawk/>CLI text processing with GNU awk</a><li><a href=https://learnbyexample.github.io/learn_ruby_oneliners/>Ruby One-Liners Guide</a><li><a href=https://learnbyexample.github.io/learn_perl_oneliners/>Perl One-Liners Guide</a><li><a href=https://learnbyexample.github.io/100_page_python_intro/>100 Page Python Intro</a><li><a href=https://learnbyexample.github.io/practice_python_projects/>Practice Python Projects</a><li><a href=https://learnbyexample.github.io/cli_text_processing_coreutils/>CLI text processing with GNU Coreutils</a><li><a href=https://learnbyexample.github.io/cli_text_processing_rust/>Command line text processing with Rust tools</a><li><a href=https://learnbyexample.github.io/vim_reference/>Vim Reference Guide</a><li><a href=https://learnbyexample.github.io/cli-computing/>Linux Command Line Computing</a></ul></main><nav aria-label="Page navigation"class=nav-wrapper><a aria-label="Previous chapter"class="mobile-nav-chapters previous"title="Previous chapter"aria-keyshortcuts=Left href=cover.html rel=prev> <i class="fa fa-angle-left"></i> </a><a aria-label="Next chapter"class="mobile-nav-chapters next"title="Next chapter"aria-keyshortcuts=Right href=preface.html rel=next> <i class="fa fa-angle-right"></i> </a><div style="clear: both"></div></nav></div></div><nav aria-label="Page navigation"class=nav-wide-wrapper><a aria-label="Previous chapter"class="nav-chapters previous"title="Previous chapter"aria-keyshortcuts=Left href=cover.html rel=prev> <i class="fa fa-angle-left"></i> </a><a aria-label="Next chapter"class="nav-chapters next"title="Next chapter"aria-keyshortcuts=Right href=preface.html rel=next> <i class="fa fa-angle-right"></i> </a></nav></div><script>window.playground_copyable = true;</script><script charset=utf-8 src=elasticlunr.min.js></script><script charset=utf-8 src=mark.min.js></script><script charset=utf-8 src=searcher.js></script><script charset=utf-8 src=clipboard.min.js></script><script charset=utf-8 src=highlight.js></script><script charset=utf-8 src=book.js></script><script src=sidebar.js></script>

0 commit comments

Comments
 (0)