Skip to content

Commit 2fcdc20

Browse files
updates for version 2.5
1 parent 96b8442 commit 2fcdc20

16 files changed

+347
-150
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 Sundeep Agarwal
3+
Copyright (c) 2025 Sundeep Agarwal
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CLI text processing with GNU awk
22

3-
Example based guide to mastering GNU awk. Visit https://youtu.be/KIa_EaYwGDI for a short video about the book.
3+
Learn GNU awk with hundreds of examples and exercises. Visit https://youtu.be/KIa_EaYwGDI for a short video about the book.
44

55
<p align="center"><img src="./images/gawk_ls.png" alt="CLI text processing with GNU awk ebook cover image" /></p>
66

@@ -20,15 +20,14 @@ See [Version_changes.md](./Version_changes.md) to keep track of changes made to
2020
* https://learnbyexample.gumroad.com/l/gnu_awk
2121
* https://leanpub.com/gnu_awk
2222
* You can also get the book as part of these bundles:
23-
* **All books bundle** bundle from https://learnbyexample.gumroad.com/l/all-books
24-
* Includes all my programming books
25-
* **Magical one-liners** bundle from https://learnbyexample.gumroad.com/l/oneliners or https://leanpub.com/b/oneliners
26-
* **Awesome Regex** bundle from https://learnbyexample.gumroad.com/l/regex or https://leanpub.com/b/regex
23+
* **All books bundle** bundle from https://leanpub.com/b/learnbyexample-all-books or https://learnbyexample.gumroad.com/l/all-books
24+
* **Magical one-liners** bundle from https://leanpub.com/b/oneliners or https://learnbyexample.gumroad.com/l/oneliners
25+
* **Awesome Regex** bundle from https://leanpub.com/b/regex or https://learnbyexample.gumroad.com/l/regex
2726
* See https://learnbyexample.github.io/books/ for a list of other books
2827

2928
For a preview of the book, see [sample chapters](./sample_chapters/gnu_awk_sample.pdf).
3029

31-
The book can also be [viewed as a single markdown file in this repo](./gnu_awk.md). See my blogpost on [generating pdfs from markdown using pandoc](https://learnbyexample.github.io/customizing-pandoc/) if you are interested in the ebook creation process.
30+
The book can also be [viewed as a single markdown file in this repo](./gnu_awk.md). See my blogpost on [generating pdf/epub from markdown using pandoc](https://learnbyexample.github.io/customizing-pandoc/) if you are interested in the ebook creation process.
3231

3332
For the web version of the book, visit https://learnbyexample.github.io/learn_gnuawk/
3433

@@ -95,11 +94,12 @@ You can reach me via:
9594
* [Warning](https://commons.wikimedia.org/wiki/File:Warning_icon.svg) and [Info](https://commons.wikimedia.org/wiki/File:Info_icon_002.svg) icons by [Amada44](https://commons.wikimedia.org/wiki/User:Amada44) under public domain
9695
* [arifmahmudrana](https://github.com/arifmahmudrana) for spotting an ambiguous explanation
9796
* [Pound-Hash](https://github.com/Pound-Hash) for critical feedback
97+
* [FabijanC](https://github.com/FabijanC) and [alexwheezy](https://github.com/alexwheezy) for spotting ambiguous descriptions, typos and issues with commands
9898
* [mdBook](https://github.com/rust-lang/mdBook) — for web version of the book
9999
* [mdBook-pagetoc](https://github.com/JorelAli/mdBook-pagetoc) — for adding table of contents for each chapter
100100
* [minify-html](https://github.com/wilsonzlin/minify-html) — for minifying html files
101101

102-
Special thanks to all my friends and online acquaintances for their help, support and encouragement, especially during these difficult times.
102+
Special thanks to all my friends and online acquaintances for their help, support and encouragement, especially during difficult times.
103103

104104
<br>
105105

Version_changes.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<br>
22

3+
### 2.5
4+
5+
* Command version updated to **GNU awk 5.3.1**
6+
* Added details for the `--csv` option and the `\u` escape sequence
7+
* Corrected typos, updated exercises, descriptions and external links
8+
* Updated Acknowledgements section
9+
10+
<br>
11+
312
### 2.0
413

514
* Command version updated to **GNU awk 5.2.2**

code_snippets/Builtin_functions.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ awk 'index($0, "a+b")==1' eqns.txt
105105

106106
awk -v s="a+b" 'index($0, s)==length()-length(s)+1' eqns.txt
107107

108-
echo 'a\b\c\d' | awk -v s='a\b' 'index($0, s)'
108+
printf '%s\n' 'a\b\c\d' | awk -v s='a\b' 'index($0, s)'
109109

110-
echo 'a\b\c\d' | awk -v s='a\\b' 'index($0, s)'
110+
printf '%s\n' 'a\b\c\d' | awk -v s='a\\b' 'index($0, s)'
111111

112-
echo 'a\b\c\d' | s='a\b' awk 'index($0, ENVIRON["s"])'
112+
printf '%s\n' 'a\b\c\d' | s='a\b' awk 'index($0, ENVIRON["s"])'
113113

114114
## system
115115

code_snippets/Control_Structures.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ seq 6 | awk '{ORS = NR%3 ? "-" : RS} 1'
88

99
awk '/^b/{print; print($NF>0 ? "------" : "======")}' table.txt
1010

11-
## loops
11+
## Loops
1212

1313
awk 'BEGIN{for(i=2; i<7; i+=2) print i}'
1414

code_snippets/Field_separators.sh

+20-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ echo 'Sample123string42with777numbers' | awk -F'[0-9]+' '{print $2}'
5656

5757
echo 'Sample123string42with777numbers' | awk -F'[a-zA-Z]+' '{print $2}'
5858

59-
echo 'load;err_msg--\ant,r2..not' | awk -F'\\W+' '{print $3}'
59+
printf '%s\n' 'load;err_msg--\ant,r2..not' | awk -F'\\W+' '{print $3}'
6060

6161
echo 'hi.bye.hello' | awk -F. '{print $2}'
6262

@@ -122,17 +122,33 @@ s='items: "apple" and "mango"'
122122

123123
echo "$s" | awk -v FPAT='"[^"]+"' '{print $1}'
124124

125+
echo 'Read Eat Sleep' | awk -v FPAT='e' '{print NF}'
126+
127+
echo 'Read Eat Sleep' | awk -v IGNORECASE=1 -v FPAT='e' '{print NF}'
128+
129+
echo 'Read Eat Sleep' | awk -v IGNORECASE=1 -v FPAT='[e]' '{print NF}'
130+
131+
## CSV processing with FPAT
132+
125133
s='eagle,"fox,42",bee,frog'
126134

127135
echo "$s" | awk -F, '{print $2}'
128136

129137
echo "$s" | awk -v FPAT='"[^"]*"|[^,]*' '{print $2}'
130138

131-
echo 'Read Eat Sleep' | awk -v FPAT='e' '{print NF}'
139+
## CSV processing with `--csv`
132140

133-
echo 'Read Eat Sleep' | awk -v IGNORECASE=1 -v FPAT='e' '{print NF}'
141+
s='"toy,eagle\"s","fox,42",bee,frog'
134142

135-
echo 'Read Eat Sleep' | awk -v IGNORECASE=1 -v FPAT='[e]' '{print NF}'
143+
printf '%b' "$s" | awk -v FPAT='"[^"]*"|[^,]*' '{print $2}'
144+
145+
printf '%b' "$s" | awk -k '{print $2}'
146+
147+
printf '%b' "$s" | awk -k -v OFS=: '{$1=$1} 1'
148+
149+
cat newline.csv
150+
151+
awk -k 'NR==1{print $2}' newline.csv
136152

137153
## FIELDWIDTHS
138154

code_snippets/Installation_and_Documentation.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## Installation
22

3-
wget https://ftp.gnu.org/gnu/gawk/gawk-5.2.2.tar.xz
3+
wget https://ftp.gnu.org/gnu/gawk/gawk-5.3.1.tar.xz
44

5-
tar -Jxf gawk-5.2.2.tar.xz
5+
tar -Jxf gawk-5.3.1.tar.xz
66

7-
cd gawk-5.2.2/
7+
cd gawk-5.3.1/
88

99
./configure
1010

code_snippets/Regular_Expressions.sh

+13-9
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ printf 'a^2 + b^2 - C*3\nd = c^2' | awk '/b\^2/'
8282

8383
echo '(a*b) + c' | awk '{gsub(/\(|)/, "")} 1'
8484

85-
echo '\learn\by\example' | awk '{gsub(/\\/, "/")} 1'
85+
printf '%s\n' '\learn\by\example' | awk '{gsub(/\\/, "/")} 1'
8686

8787
## Using string literal as a regexp
8888

@@ -102,9 +102,9 @@ awk 'gsub("\\<par\\>", "X")' anchors.txt
102102

103103
awk 'gsub(/\<par\>/, "X")' anchors.txt
104104

105-
echo '\learn\by\example' | awk '{gsub("\\\\", "/")} 1'
105+
printf '%s\n' '\learn\by\example' | awk '{gsub("\\\\", "/")} 1'
106106

107-
echo '\learn\by\example' | awk '{gsub(/\\/, "/")} 1'
107+
printf '%s\n' '\learn\by\example' | awk '{gsub(/\\/, "/")} 1'
108108

109109
## The dot meta character
110110

@@ -202,15 +202,15 @@ echo 'I like "mango" and "guava"' | awk '{gsub(/"[^"]+"/, "X")} 1'
202202

203203
printf 'tryst\nfun\nglyph\npity\nwhy\n' | awk '!/[aeiou]/'
204204

205-
echo 'load;err_msg--\/ant,r2..not' | awk '{gsub(/\W+/, "|")} 1'
205+
printf '%s\n' 'load;err_msg--\/ant,r2..not' | awk '{gsub(/\W+/, "|")} 1'
206206

207207
printf 'hi \v\f there.\thave \ra nice\t\tday\n' | awk '{gsub(/\s+/, " ")} 1'
208208

209-
echo 'w=y\x+9*3' | awk '{gsub(/[\w=]/, "")} 1'
209+
printf '%s\n' 'w=y\x+9*3' | awk '{gsub(/[\w=]/, "")} 1'
210210

211-
echo '42\d123' | awk '{gsub(/\d+/, "-")} 1'
211+
printf '%s\n' '42\d123' | awk '{gsub(/\d+/, "-")} 1'
212212

213-
echo '42\d123' | perl -pe 's/\d+/-/g'
213+
printf '%s\n' '42\d123' | perl -pe 's/\d+/-/g'
214214

215215
## Named character sets
216216

@@ -260,7 +260,11 @@ echo 'hello world' | awk '{sub(/.*/, "[\x26]")} 1'
260260

261261
echo 'read' | awk '{sub(/\d/, "l")} 1'
262262

263-
## Replace specific occurrence
263+
awk 'BEGIN{print "\u3b1\u3bb\u3b5\u3c0\u3bf\u3cd"}'
264+
265+
awk 'BEGIN{print "cag\u308" "ed"}'
266+
267+
## Replace a specific occurrence
264268

265269
s='apple:banana:cherry:fig:mango'
266270

@@ -286,7 +290,7 @@ echo "$s" | awk '{print gensub(/(.*),((.*,){2})/, "\\1[]\\2", 1)}'
286290

287291
s='effort flee facade oddball rat tool'
288292

289-
echo "$s" | awk '{gsub(/\w*(\w)\1\w*/, "X")} 1'
293+
echo "$s" | awk '{print gensub(/\w*(\w)\1\w*/, "X", "g")}'
290294

291295
echo "$s" | sed -E 's/\w*(\w)\1\w*/X/g'
292296

example_files/newline.csv

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apple,"1
2+
2
3+
3",good
4+
fig,guava,"32
5+
54",nice

0 commit comments

Comments
 (0)