Skip to content

Commit dd73a2c

Browse files
updates for version 2.1
1 parent 15d582c commit dd73a2c

File tree

10 files changed

+96
-92
lines changed

10 files changed

+96
-92
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# CLI text processing with GNU sed
22

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

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

7-
The book also includes exercises to test your understanding, which are presented together as a single file in this repo — [Exercises.md](./exercises/Exercises.md)
7+
The book also includes exercises to test your understanding, which are presented together as a single file in this repo — [Exercises.md](./exercises/Exercises.md).
88

99
For solutions to the exercises, see [Exercise_solutions.md](./exercises/Exercise_solutions.md).
1010

@@ -20,15 +20,15 @@ See [Version_changes.md](./Version_changes.md) to keep track of changes made to
2020
* https://learnbyexample.gumroad.com/l/gnu_sed
2121
* https://leanpub.com/gnu_sed
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+
* **Linux CLI Text Processing** bundle from https://leanpub.com/b/linux-cli-text-processing or https://learnbyexample.gumroad.com/l/linux-cli-text-processing
25+
* **Magical one-liners** bundle from https://leanpub.com/b/oneliners or https://learnbyexample.gumroad.com/l/oneliners
26+
* **Awesome Regex** bundle from https://leanpub.com/b/regex or https://learnbyexample.gumroad.com/l/regex
2727
* See https://learnbyexample.github.io/books/ for a list of other books
2828

2929
For a preview of the book, see [sample chapters](./sample_chapters/gnu_sed_sample.pdf).
3030

31-
The book can also be [viewed as a single markdown file in this repo](./gnu_sed.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.
31+
The book can also be [viewed as a single markdown file in this repo](./gnu_sed.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.
3232

3333
For the web version of the book, visit https://learnbyexample.github.io/learn_gnused/
3434

Version_changes.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
<br>
22

3+
### 2.1
4+
5+
* Corrected typos, updated exercises, descriptions and external links
6+
* Updated snapshot image for the interactive TUI app
7+
8+
<br>
9+
310
### 2.0
411

512
* Command version updated to **GNU sed 4.9**

code_snippets/BRE_ERE_Regular_Expressions.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,15 @@ printf 'tryst\nfun\nglyph\npity\nwhy\n' | sed '/[aeiou]/d'
194194

195195
## Escape sequence sets
196196

197-
echo 'load;err_msg--\nant,r2..not' | sed -E 's/\W+/-/g'
197+
printf '%s\n' 'load;err_msg--\nant,r2..not' | sed -E 's/\W+/-/g'
198198

199199
printf 'hi \v\f there.\thave \ra nice\t\tday\n' | sed -E 's/\s+/ /g'
200200

201-
echo 'w=y\x+9*3' | sed 's/[\w=]//g'
201+
printf '%s\n' 'w=y\x+9*3' | sed 's/[\w=]//g'
202202

203-
echo '42\d123' | sed -E 's/\d+/-/g'
203+
printf '%s\n' '42\d123' | sed -E 's/\d+/-/g'
204204

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

207207
## Named character sets
208208

@@ -252,7 +252,7 @@ echo 'hello world' | sed 's/.*/"\x26"/'
252252

253253
echo 'effort flee facade oddball rat tool' | sed -E 's/\w*(\w)\1\w*/X/g'
254254

255-
echo '\[\] and \\w and \[a-zA-Z0-9\_\]' | sed -E 's/(\\?)\\/\1/g'
255+
printf '%s\n' '\[\] and \\w and \[a-zA-Z0-9\_\]' | sed -E 's/(\\?)\\/\1/g'
256256

257257
echo 'aa a a a 42 f_1 f_1 f_13.14' | sed -E 's/\b(\w+)( \1)+\b/\1/g'
258258

code_snippets/Shell_substitutions.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ s='{[(\ta^b/d).*+?^$|]}'
4040

4141
s=$(printf '%s' "$s" | sed 's#[{[()^$*?+.\|/]#\\&#g')
4242

43-
echo 'f*{[(\ta^b/d).*+?^$|]} - 3' | sed -E 's/'"$s"'/'"$r"'/g'
43+
printf '%s\n' 'f*{[(\ta^b/d).*+?^$|]} - 3' | sed -E 's/'"$s"'/'"$r"'/g'
4444

4545
s='{[(\ta^b/d).*+?^$|]}'
4646

4747
s=$(printf '%s' "$s" | sed 's#[[^$*.\/]#\\&#g')
4848

49-
echo 'f*{[(\ta^b/d).*+?^$|]} - 3' | sed 's/'"$s"'/'"$r"'/g'
49+
printf '%s\n' 'f*{[(\ta^b/d).*+?^$|]} - 3' | sed 's/'"$s"'/'"$r"'/g'
5050

5151
## Command substitution
5252

exercises/Exercise_solutions.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ eqn2 = pressure*3+8-14256
428428
* `*` is same as `{0,}`
429429
* `+` is same as `{1,}`
430430

431-
**10)** True or False? In ERE, `(a*|b*)` is same as `(a|b)*`.
431+
**10)** In ERE, `(a*|b*)` is same as `(a|b)*` — True or False?
432432

433433
False. Because `(a*|b*)` will match only sequences like `a`, `aaa`, `bb`, `bbbbbbbb`. But `(a|b)*` can match a mixed sequence like `ababbba` too.
434434

@@ -508,7 +508,7 @@ $ echo 'apple42:fig100' | sed -E 's/^a\w+([0-9]+:fig)?/[&]/'
508508
[apple42:fig]100
509509
```
510510
511-
For patterns matching from the same starting location, longest match wins in case of BRE/ERE flavors. So, `\w+` will give up characters to allow `([0-9]+:fig)?` to also match in the second case. In other flavors like PCRE, `apple42` will be matched both the cases.
511+
For patterns matching from the same starting location, longest match wins in case of BRE/ERE flavors. So, `\w+` will give up characters to allow `([0-9]+:fig)?` to also match in the second case. In other flavors like PCRE, `apple42` will be matched for both the cases.
512512
513513
**17)** For the input file `patterns.txt`, display lines starting with `4` or `-` or `u` or `sub` or `care`.
514514
@@ -697,7 +697,7 @@ hi there
697697
bye
698698
```
699699
700-
**8)** For the input file `patterns.txt`, replace the last but second `ar` with `X`. Display only the modified lines.
700+
**8)** For the input file `patterns.txt`, filter lines containing three or more occurrences of `ar`. For such lines, replace the third from last occurrence of `ar` with `X`.
701701
702702
```bash
703703
$ sed -nE 's/(.*)ar((.*ar){2})/\1X\2/p' patterns.txt
@@ -1065,7 +1065,7 @@ $ sed -n '/^%%Code: python%%$/,/^%%end%%$/{//!p; /^%%end%%$/q}' code.txt
10651065
3
10661066
```
10671067
1068-
**4)** For the input file `broken.txt`, display lines between the markers `top` and `bottom`. The first `sed` command shown below doesn't work because lines till the end of file are printed when the second address isn't found.
1068+
**4)** The input file `broken.txt` starts with a line containing `top` followed by some content before a line containing `bottom` is found. Blocks of lines bounded by these two markers repeats except for the last block as it is missing the `bottom` marker. The first `sed` command shown below doesn't work because it is matching till the end of file due to the missing marker. Correct this command to get the expected output shown below.
10691069
10701070
```bash
10711071
$ cat broken.txt

exercises/Exercises.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ eqn2 = pressure*3+8-14256
415415
* `*` is same as
416416
* `+` is same as
417417

418-
**10)** True or False? In ERE, `(a*|b*)` is same as `(a|b)*`.
418+
**10)** In ERE, `(a*|b*)` is same as `(a|b)*` — True or False?
419419

420420
**11)** For the input file `patterns.txt`, construct two different REGEXPs to get the outputs as shown below. Display only the modified lines.
421421

@@ -658,7 +658,7 @@ hi there
658658
bye
659659
```
660660
661-
**8)** For the input file `patterns.txt`, replace the last but second `ar` with `X`. Display only the modified lines.
661+
**8)** For the input file `patterns.txt`, filter lines containing three or more occurrences of `ar`. For such lines, replace the third from last occurrence of `ar` with `X`.
662662
663663
```bash
664664
$ sed ##### add your solution here
@@ -1019,7 +1019,7 @@ $ sed ##### add your solution here
10191019
3
10201020
```
10211021
1022-
**4)** For the input file `broken.txt`, display lines between the markers `top` and `bottom`. The first `sed` command shown below doesn't work because lines till the end of file are printed when the second address isn't found.
1022+
**4)** The input file `broken.txt` starts with a line containing `top` followed by some content before a line containing `bottom` is found. Blocks of lines bounded by these two markers repeats except for the last block as it is missing the `bottom` marker. The first `sed` command shown below doesn't work because it is matching till the end of file due to the missing marker. Correct this command to get the expected output shown below.
10231023
10241024
```bash
10251025
$ cat broken.txt

0 commit comments

Comments
 (0)