Skip to content

Commit 549bf76

Browse files
updates for version 1.5
1 parent e883495 commit 549bf76

13 files changed

Lines changed: 402 additions & 95 deletions

‎code_snippets/BRE_ERE_Regular_Expressions.sh‎

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Line Anchors
2+
13
printf 'spared no one\npar\nspar\ndare' | grep '^pa'
24

35
printf 'spared no one\npar\nspar\ndare' | grep 'ar$'
@@ -6,6 +8,8 @@ printf 'spared no one\npar\nspar\ndare' | grep '^par$'
68

79
printf 'spared no one\npar\nspar\ndare' | grep -x 'par'
810

11+
## Word Anchors
12+
913
cat word_anchors.txt
1014

1115
grep '\bpar' word_anchors.txt
@@ -22,6 +26,8 @@ grep '\Bpar' word_anchors.txt
2226

2327
grep 'par\B' word_anchors.txt
2428

29+
## Alternation
30+
2531
printf 'I like cats\nI like parrots\nI like dogs' | grep 'cat\|dog'
2632

2733
printf 'I like cats\nI like parrots\nI like dogs' | grep -E 'cat|dog'
@@ -36,13 +42,17 @@ grep --color=auto -E '^|pare' word_anchors.txt
3642

3743
grep --color=auto -E 'sub|put|tar|$' word_anchors.txt
3844

39-
printf 'spared PARTY PaReNt' | grep -ioE 'par|pare|spare'
45+
echo 'car spared spar' | grep -oE 'are|spared'
46+
47+
echo 'car spared spar' | grep -oE 'spared|are'
4048

41-
printf 'spared PARTY PaReNt' | grep -ioE 'spare|pare|par'
49+
echo 'pool party 2' | grep -oE 'party|par'
4250

43-
printf 'spared PARTY PaReNt' | grep -ioE 'spa|pared'
51+
echo 'pool party 2' | grep -oE 'par|party'
4452

45-
printf 'spared PARTY PaReNt' | grep -ioE 'pared|spa'
53+
echo 'pool party 2' | grep -oP 'par|party'
54+
55+
## Grouping
4656

4757
printf 'red\nreform\nread\narrest' | grep -E 'reform|rest'
4858

@@ -54,6 +64,8 @@ printf 'sub par\nspare\npart time' | grep -E '\b(par|part)\b'
5464

5565
printf 'sub par\nspare\npart time' | grep -E '\bpar(|t)\b'
5666

67+
## Escaping metacharacters
68+
5769
echo 'a^2 + b^2 - C*3' | grep 'b^2'
5870

5971
echo '$a = $b + $c' | grep '$b'
@@ -64,7 +76,17 @@ echo '$a = $b + $c' | grep -oF '$' | wc -l
6476

6577
printf '(a/b) + c\n3 + (a/b) - c' | grep '^(a/b)'
6678

67-
printf '(a/b) + c\n3 + (a/b) - c' | grep -E '^\(a/b\)'
79+
printf '(a/b) + c\n3 + (a/b) - c' | grep -E '^\(a/b)'
80+
81+
## Matching characters like tabs
82+
83+
echo 'attempt' | grep -o 'a\tt'
84+
85+
printf 'go\tto\ngo to' | grep $'go\tto'
86+
87+
printf 'go\tto\ngo to' | grep $'go\x20to'
88+
89+
## The dot meta character
6890

6991
echo 'tac tin cot abc:tuv excite' | grep -o 'c.t'
7092

@@ -74,6 +96,8 @@ printf '42\t33\n' | grep -o '2.3'
7496

7597
grep -xE 'c..(t|l)y' words.txt
7698

99+
## Quantifiers
100+
77101
printf 'fed\nfod\nfe:d\nfeed' | grep -wE 'fe.?d'
78102

79103
printf 'sub par\nspare\npart time' | grep -wE 'part?'
@@ -108,10 +132,16 @@ echo 'a cat and a dog' | grep -E 'cat.*dog|dog.*cat'
108132

109133
echo 'dog and cat' | grep -E 'cat.*dog|dog.*cat'
110134

135+
## Longest match wins
136+
111137
echo 'foot' | grep -oE 'f.?o'
112138

113139
echo 'car bat cod map scat dot abacus' | grep -o '.*'
114140

141+
echo 'foo123312baz' | grep -oE 'o(1|2|3)+(12baz)?'
142+
143+
echo 'foo123312baz' | grep -oP 'o(1|2|3)+(12baz)?'
144+
115145
echo 'car bat cod map scat dot abacus' | grep -o '.*m'
116146

117147
echo 'car bat cod map scat dot abacus' | grep -o 'c.*t'
@@ -120,6 +150,8 @@ echo 'car bat cod map scat dot abacus' | grep -o 'c.*at'
120150

121151
echo 'car bat cod map scat dot abacus' | grep -o 'b.*m*'
122152

153+
## Character classes
154+
123155
printf 'cute\ncat\ncot\ncoat\ncost\nscuttle' | grep 'c[ou]t'
124156

125157
printf 'meeting\ncute\nboat\nsite\nfoot' | grep -E '[aeo]+t'
@@ -180,6 +212,8 @@ echo 'f*(a^b) - 3*(a+b)/(a-b)' | grep -o 'a[+^]b'
180212

181213
echo '5ba\babc2' | grep -o '[a\b]*'
182214

215+
## Backreferences
216+
183217
grep -xE '([a-z]{3})..\1' words.txt
184218

185219
grep -xE '([a-d]..)\1' words.txt
@@ -188,9 +222,31 @@ echo 'effort flee facade oddball rat tool' | grep -owE '\w*(\w)\1\w*'
188222

189223
printf 'spot the the error\nno issues here' | grep -wE '(\w+)\W+\1'
190224

225+
## Known Bugs
226+
191227
grep -m5 -xiE '([a-z]*([a-z])\2[a-z]*){2}' words.txt
192228

193229
grep -m5 -xiE '[a-z]*([a-z])\1[a-z]*([a-z])\2[a-z]*' words.txt
194230

195231
grep -m5 -xiP '([a-z]*([a-z])\2[a-z]*){2}' words.txt
196232

233+
echo 'cocoa' | grep -E '(\bco){2}'
234+
235+
echo 'cocoa' | grep -E '\bco\bco'
236+
237+
echo 'cocoa' | grep -P '(\bco){2}'
238+
239+
echo 'it line with it here sit too' | grep -oE 'with(.*\bit\b){2}'
240+
241+
echo 'it line with it here sit too' | grep -oE 'with.*\bit\b.*\bit\b'
242+
243+
echo 'it line with it here sit too' | grep -oP 'with(.*\bit\b){2}'
244+
245+
echo 'it line with it here sit too' | grep -oE 'with(.*\<it\>){2}'
246+
247+
echo 'it line with it here it too' | grep -oE 'with(.*\<it\>){2}'
248+
249+
echo 'it line with it here it too sit' | grep -oE 'with(.*\<it\>){2}'
250+
251+
echo 'it line with it here it too sit' | grep -oP 'with(.*\bit\b){2}'
252+

‎code_snippets/Context_matching.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
cat context.txt
22

3+
## -A
4+
35
grep -A2 'blue' context.txt
46

57
grep -x -A2 'blue' context.txt
68

9+
## -B
10+
711
grep -B2 'bread' context.txt
812

913
grep -B3 'ruby' context.txt
1014

15+
## -C
16+
1117
grep -C1 'sky' context.txt
1218

1319
grep -A1 -B2 'sky' context.txt
1420

1521
grep -C2 'kotlin' context.txt
1622

23+
## Contiguous matches
24+
1725
grep -n -C1 'flower' context.txt
1826

1927
grep -n -A4 'blue' context.txt
2028

29+
## Customizing separators
30+
2131
seq 29 | grep --group-separator='*****' -A1 '3'
2232

2333
grep -A0 --group-separator='*-----------*-----------*' 'in' context.txt

‎code_snippets/Frequently_used_options.sh‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
printf '42\n' | file -
2-
3-
printf '42\r\n' | file -
1+
## Simple string search
42

53
cat programming_quotes.txt
64

@@ -10,6 +8,12 @@ grep 'e th' programming_quotes.txt
108

119
printf 'avocado\nmango\nguava' | grep 'v'
1210

11+
printf '42\n' | file -
12+
13+
printf '42\r\n' | file -
14+
15+
## Fixed string search
16+
1317
echo 'int a[5]' | grep 'a[5]'
1418

1519
echo 'int a[5]' | grep 'a['
@@ -20,14 +24,20 @@ echo 'int a[5]' | grep -F 'a[5]'
2024

2125
echo 'int a[5]' | fgrep 'a[5]'
2226

27+
## Case insensitive search
28+
2329
grep -i 'jam' programming_quotes.txt
2430

2531
printf 'Cat\ncOnCaT\nscatter\ncut' | grep -i 'cat'
2632

33+
## Invert matching lines
34+
2735
seq 5 | grep -v '3'
2836

2937
printf 'goal\nrate\neat\npit' | grep -v 'at'
3038

39+
## Line number and count
40+
3141
grep -n 'not' programming_quotes.txt
3242

3343
printf 'great\nnumber\numpteen' | grep -n 'r'
@@ -40,10 +50,14 @@ seq 15 | grep -c '1' programming_quotes.txt -
4050

4151
cat <(seq 15) programming_quotes.txt | grep -c '1'
4252

53+
## Limiting output lines
54+
4355
grep -m3 'in' programming_quotes.txt
4456

4557
seq 1000 | grep -m4 '2'
4658

59+
## Multiple search strings
60+
4761
grep -e '1' -e 'two' programming_quotes.txt
4862

4963
printf 'two\n1\n' > search_strings.txt
@@ -56,6 +70,8 @@ grep -f search_strings.txt -e 'twice' programming_quotes.txt
5670

5771
grep 'in' programming_quotes.txt | grep 'not'
5872

73+
## Filename instead of matching lines
74+
5975
grep -l 'are' programming_quotes.txt search_strings.txt
6076

6177
grep -l 'xyz' programming_quotes.txt search_strings.txt
@@ -66,6 +82,8 @@ grep -L 'xyz' programming_quotes.txt search_strings.txt
6682

6783
grep -L 'are' programming_quotes.txt search_strings.txt
6884

85+
## Filename prefix for matching lines
86+
6987
grep '1' programming_quotes.txt
7088

7189
seq 1000 | grep -h -m3 '1' - programming_quotes.txt
@@ -78,6 +96,8 @@ grep -Hn '1' *
7896

7997
vim -q <(grep -Hn '1' *)
8098

99+
## Colored output
100+
81101
grep --color=auto -m3 'in' programming_quotes.txt
82102

83103
grep --color=auto -n -e '1' -e 'worth' *.txt
@@ -90,6 +110,8 @@ alias grep='grep --color=auto'
90110

91111
grep --color=always 'not' programming_quotes.txt | less -R
92112

113+
## Match whole word or line
114+
93115
printf 'par value\nheir apparent\n' | grep 'par'
94116

95117
printf 'par value\nheir apparent\n' | grep -w 'par'
@@ -104,6 +126,8 @@ grep -x '1' *.txt
104126

105127
grep -cx '' programming_quotes.txt
106128

129+
## Comparing lines between files
130+
107131
printf 'teal\nlight blue\nbrown\nyellow\n' > colors_1
108132

109133
printf 'blue\nblack\ndark green\nyellow\n' > colors_2
@@ -114,6 +138,8 @@ grep -Fvxf colors_1 colors_2
114138

115139
grep -Fvxf colors_2 colors_1
116140

141+
## Extract only matching portion
142+
117143
grep -o -e 'twice' -e 'hard' programming_quotes.txt
118144

119145
grep -c 'in' programming_quotes.txt

‎code_snippets/Gotchas_and_Tricks.sh‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Shell quoting
2+
13
echo 'a cat and a dog' | grep and a
24

35
echo 'a cat and a dog' | grep 'and a'
@@ -18,6 +20,8 @@ echo '\S*\Q'"$expr"'\E\S*'
1820

1921
echo 'f*(2-a/b) - 3*(a^b)-42' | grep -oP '\S*\Q'"$expr"'\E\S*'
2022

23+
## Patterns starting with hyphen
24+
2125
printf '-2+3=1\n'
2226

2327
echo '5*3-2=13' | grep '-2'
@@ -32,6 +36,8 @@ printf 'boat\nsite\nfoot' | grep '[aeo]+t'
3236

3337
printf 'boat\nsite\nfoot' | grep '[aeo]+t' -E
3438

39+
## Word boundary differences
40+
3541
echo '*$' | grep '\b\$\b'
3642

3743
echo '*$' | grep -w '\$'
@@ -42,24 +48,40 @@ echo 'I have 12, he has 2!' | grep -o '\<..\>'
4248

4349
echo 'I have 12, he has 2!' | grep -ow '..'
4450

45-
mkdir gotchas_tricks && cd $_
51+
## Faster execution for ASCII input
52+
53+
time grep -xE '([a-d][r-z]){3}' words.txt > f1
54+
55+
time LC_ALL=C grep -xE '([a-d][r-z]){3}' words.txt > f2
56+
57+
diff -s f1 f2
58+
59+
time grep -xE '([a-z]..)\1' words.txt > f1
60+
61+
time LC_ALL=C grep -xE '([a-z]..)\1' words.txt > f2
62+
63+
rm f[12]
64+
65+
/bin/grep --version | head -n1
4666

47-
wget https://www.gutenberg.org/files/60/60.txt -O scarlet_pimpernel.txt
67+
time /bin/grep -wE '([a-d][r-z]){3}' words.txt > f1
4868

49-
file scarlet_pimpernel.txt
69+
grep --version | head -n1
5070

51-
time grep -wE '([a-d][r-z]){3}' scarlet_pimpernel.txt > f1
71+
time grep -wE '([a-d][r-z]){3}' words.txt > f2
5272

53-
time LC_ALL=C grep -wE '([a-d][r-z]){3}' scarlet_pimpernel.txt > f2
73+
rm f[12]
5474

55-
cd ../bre_ere/
75+
## Speed benefits with PCRE
5676

5777
time LC_ALL=C grep -xE '([a-z]..)\1' words.txt > f1
5878

5979
time LC_ALL=C grep -xP '([a-z]..)\1' words.txt > f2
6080

6181
rm f[12]
6282

83+
## Parallel execution
84+
6385
wget https://github.com/torvalds/linux/archive/v4.19.tar.gz
6486

6587
tar -zxf v4.19.tar.gz
@@ -78,15 +100,5 @@ diff -sq <(sort ../f1) <(sort ../f2)
78100

79101
diff -sq <(sort ../f1) <(sort ../f3)
80102

81-
cd ..
82-
83-
/bin/grep --version | head -n1
84-
85-
time /bin/grep -wE '([a-d][r-z]){3}' scarlet_pimpernel.txt > f1
86-
87-
grep --version | head -n1
88-
89-
time grep -wE '([a-d][r-z]){3}' scarlet_pimpernel.txt > f2
90-
91-
rm f[1-3]
103+
rm ../f[1-3]
92104

0 commit comments

Comments
 (0)