Skip to content

Commit

Permalink
updates for version 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
learnbyexample committed May 20, 2020
1 parent aca8a2d commit 2a69a4d
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 9 deletions.
6 changes: 6 additions & 0 deletions code_snippets/Adding_content_from_file.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## r for entire file

cat ip.txt

cat fav_colors.txt
Expand All @@ -18,12 +20,16 @@ printf '%b' "$items" | sed -e '2 {r /dev/stdin' -e 'd}' ip.txt

sed -e '/^red/r ip.txt' -e '/yellow/,//d' fav_colors.txt

## Using e and cat command

sed '/red/e cat ip.txt' fav_colors.txt

text='good\tone\nfood\tpun'

echo "$text" | sed '1e cat /dev/stdin' ip.txt

## R for line by line

sed '/red/R ip.txt' fav_colors.txt

seq 4 | sed 'R /dev/stdin' fav_colors.txt
Expand Down
52 changes: 50 additions & 2 deletions code_snippets/BRE_ERE_Regular_Expressions.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## Line Anchors

printf 'spared no one\npar\nspar\n' | sed -n '/^sp/p'

printf 'spared no one\npar\nspar\n' | sed -n '/ar$/p'
Expand All @@ -8,6 +10,8 @@ printf 'spared no one\npar\nspar\n' | sed 's/^/* /'

printf 'spared no one\npar\nspar\n' | sed '/ /! s/$/./'

## Word Anchors

cat word_anchors.txt

sed -n '/\bpar/p' word_anchors.txt
Expand All @@ -26,6 +30,8 @@ echo 'copper' | sed 's/\b/:/g'

echo 'copper' | sed 's/\B/:/g'

## Alternation

sed -n '/two\|sub/p' word_anchors.txt

sed -nE '/two|sub/p' word_anchors.txt
Expand All @@ -42,6 +48,10 @@ echo 'spared party parent' | sed -E 's/spa|spared/**/g'

echo 'spared party parent' | sed -E 's/spared|spa/**/g'

echo 'spared party parent' | perl -pe 's/spa|spared/**/'

## Grouping

printf 'red\nreform\nread\narrest\n' | sed -nE '/reform|rest/p'

printf 'red\nreform\nread\narrest\n' | sed -nE '/re(form|st)/p'
Expand All @@ -52,6 +62,8 @@ printf 'sub par\nspare\npart time\n' | sed -nE '/\b(par|part)\b/p'

printf 'sub par\nspare\npart time\n' | sed -nE '/\bpar(|t)\b/p'

## Matching the metacharacters

echo 'a^2 + b^2 - C*3' | sed -n '/b^2/p'

echo '$a = $b + $c' | sed -n '/$b/p'
Expand All @@ -62,6 +74,8 @@ printf '(a/b) + c\n3 + (a/b) - c\n' | sed -n '/^(a\/b)/p'

printf '(a/b) + c\n3 + (a/b) - c\n' | sed -nE '/^\(a\/b\)/p'

## Using different delimiters

echo '/home/learnbyexample/reports' | sed 's/\/home\/learnbyexample\//~\//'

echo '/home/learnbyexample/reports' | sed 's#/home/learnbyexample/#~/#'
Expand All @@ -74,12 +88,16 @@ printf '/foo/bar/1\n/foo/baz/1\n'

printf '/foo/bar/1\n/foo/baz/1\n' | sed -n '\;/foo/bar/;p'

## The dot meta character

echo 'tac tin cot abc:tyz excited' | sed 's/c.t/-/g'

printf '42\t35\n' | sed 's/.3.//'

printf 'abc\nxyz\n' | sed 'N; s/c.x/ /'

## Quantifiers

echo 'fed fold fe:d feeder' | sed -E 's/\bfe.?d\b/X/g'

printf 'sub par\nspare\npart time\n' | sed -nE '/\bpart?\b/p'
Expand Down Expand Up @@ -114,6 +132,8 @@ echo 'two cats and a dog' | sed -E 's/cat.*dog|dog.*cat/pets/'

echo 'two dogs and a cat' | sed -E 's/cat.*dog|dog.*cat/pets/'

## Longest match wins

echo 'foot' | sed -E 's/f.?o/X/'

echo 'car bat cod map scat dot abacus' | sed 's/.*/X/'
Expand All @@ -130,6 +150,8 @@ echo 'car bat cod map scat dot abacus' | sed 's/b.*at/-/'

echo 'car bat cod map scat dot abacus' | sed 's/a.*m*/-/'

## Character classes

printf 'cute\ncat\ncot\ncoat\ncost\nscuttle\n' | sed -n '/c[ou]t/p'

printf 'meeting\ncute\nboat\nat\nfoot\n' | sed -nE '/.[aeo]+t/p'
Expand Down Expand Up @@ -182,6 +204,8 @@ echo 'int a[5]' | sed -n '/[x[y.]/p'

echo 'f*(a^b) - 3*(a+b)/(a-b)' | sed 's/a[+^]b/c/g'

## Escape sequences

printf 'foo\tbar\tbaz\n' | sed 's/\t/ /g'

echo 'a b c' | sed 's/ /\t/g'
Expand All @@ -198,6 +222,8 @@ echo 'hello world' | sed 's/.*/"&"/'

echo 'hello world' | sed 's/.*/"\x26"/'

## Backreferences

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

echo '\[\] and \\w and \[a-zA-Z0-9\_\]' | sed -E 's/(\\?)\\/\1/g'
Expand All @@ -214,13 +240,35 @@ echo 'hello world' | sed 's/.*/Hi. &. Have a nice day/'

echo 'one,2,3.14,42' | sed -E 's/^(([^,]+,){2})([^,]+)/\1"\3"/'

sed -nE '/^(\w*(\w)\2\w*){2}$/p' words.txt | head -n5
s='tryst,fun,glyph,pity,why,group'

sed -nE '/^\w*(\w)\1\w*(\w)\2\w*$/p' words.txt | head -n5
echo "$s" | sed -E 's/\b\w+\b|(\b[gp]\w*y\w*\b)/\1/g'

echo "$s" | sed -E 's/(\b[gp]\w*y\w*\b)|\b\w+\b/\1/g'

echo 'foo and bar' | sed 's/and/[&]/'

echo 'foo and bar' | sed 's/and/[\&]/'

echo 'foo and bar' | sed 's/and/\\/'

## Known Bugs

sed -nE '/^(\w*(\w)\2\w*){2}$/p' words.txt | head -n5

sed -nE '/^\w*(\w)\1\w*(\w)\2\w*$/p' words.txt | head -n5

echo 'cocoa' | sed -nE '/(\bco){2}/p'

echo 'cocoa' | sed -nE '/\bco\bco/p'

echo 'it line with it here sit too' | sed -E 's/with(.*\bit\b){2}/XYZ/'

echo 'it line with it here sit too' | sed -E 's/with.*\bit\b.*\bit\b/XYZ/'

echo 'it line with it here sit too' | sed -E 's/with(.*\<it\>){2}/XYZ/'

echo 'it line with it here it too' | sed -E 's/with(.*\<it\>){2}/XYZ/'

echo 'it line with it here it too sit' | sed -E 's/with(.*\<it\>){2}/XYZ/'

4 changes: 4 additions & 0 deletions code_snippets/Control_structures.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## if-then-else

cat nums.txt

sed '/^-/{s///; b}; s/^/-/' nums.txt
Expand All @@ -8,6 +10,8 @@ printf 'good\nbad\n' | sed 's/o/-/g; T; s/d/*/g'

printf 'good\nbad\nneed\n' | sed 's/o/-/g; s/a/%/g; T; a ----'

## loop

echo '12345 hello42' | sed -E ':a s/^(\**)[0-9]/\1*/; ta'

echo '123x45 hello42' | sed -E ':a s/^(\**)[0-9]/\1*/; ta'
Expand Down
16 changes: 16 additions & 0 deletions code_snippets/Flags.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
## Case insensitive matching

printf 'Cat\ncOnCaT\nscatter\ncot\n' | sed -n '/cat/p'

printf 'Cat\ncOnCaT\nscatter\ncot\n' | sed -n '/cat/Ip'

printf 'Cat\ncOnCaT\nscatter\ncot\n' | sed 's/cat/dog/I'

## Changing case in replacement section

echo 'hello there. how are you?' | sed 's/\b\w/\u&/g'

echo 'HELLO THERE. HOW ARE YOU?' | sed 's/\b\w/\l&/g'
Expand All @@ -22,10 +26,14 @@ echo 'HeLLo:bYe gOoD:beTTEr' | sed -E 's/[a-z]+/\L\u&/Ig'

echo 'HeLLo:bYe gOoD:beTTEr' | sed -E 's/[a-z]+/\U\l&/Ig'

## Global replace

printf '1,2,3,4\na,b,c,d\n' | sed 's/,/-/'

printf '1,2,3,4\na,b,c,d\n' | sed 's/,/-/g'

## Replace specific occurrences

echo 'foo:123:bar:baz' | sed 's/:/-/'

echo 'foo:123:bar:baz' | sed -E 's/[^:]+/"&"/'
Expand Down Expand Up @@ -54,10 +62,14 @@ echo '456:foo:123:bar:789:baz' | sed 's/:/[]/3; s/:/[]/2'

echo '456:foo:123:bar:789:baz' | sed 's/:/[]/5; s/:/[]/3; s/:/[]/2'

## Print flag

echo 'hi there. have a nice day' | sed -n 's/xyz/XYZ/p'

echo 'hi there. have a nice day' | sed -n 's/\bh/H/pg'

## Write to a file

seq 20 | sed -n 's/3/three/w 3.txt'

cat 3.txt
Expand All @@ -76,6 +88,8 @@ sed -i 's/three/3/w /dev/stdout' 3.txt

cat 3.txt

## Executing external commands

printf 'Date:\nreplace this line\n'

printf 'Date:\nreplace this line\n' | sed 's/^replace.*/date/e'
Expand All @@ -92,6 +106,8 @@ printf 'date\ndate -I\n' | sed '2e'

printf 'show\nexample\n' | sed '/am/e seq 2'

## Multiline mode

printf 'Hi there\nHave a Nice Day\n' | sed 'N; s/H.*e/X/'

printf 'Hi there\nHave a Nice Day\n' | sed 'N; s/H.*e/X/gm'
Expand Down
10 changes: 10 additions & 0 deletions code_snippets/Inplace_file_editing.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## With backup

cat colors.txt

sed -i.bkp 's/blue/green/' colors.txt
Expand All @@ -6,12 +8,16 @@ cat colors.txt

cat colors.txt.bkp

## Without backup

cat fruits.txt

sed -i 's/an/AN/g' fruits.txt

cat fruits.txt

## Multiple files

cat f1.txt

cat f2.txt
Expand All @@ -24,12 +30,16 @@ cat f1.txt

cat f2.txt

## Prefix backup name

ls *colors*

sed -i'bkp.*' 's/green/yellow/' colors.txt

ls *colors*

## Place backups in different directory

mkdir backups

sed -i'backups/*' 's/good/nice/' f1.txt f2.txt
Expand Down
14 changes: 11 additions & 3 deletions code_snippets/Introduction.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
wget https://ftp.gnu.org/gnu/sed/sed-4.7.tar.xz
## Installation

tar -Jxf sed-4.7.tar.xz
wget https://ftp.gnu.org/gnu/sed/sed-4.8.tar.xz

cd sed-4.7/
tar -Jxf sed-4.8.tar.xz

cd sed-4.8/

./configure

Expand All @@ -14,16 +16,22 @@ type -a sed

sed --version | head -n1

## Documentation and options overview

man sed

sed --help

## Editing standard input

printf '1,2,3,4\na,b,c,d\n'

printf '1,2,3,4\na,b,c,d\n' | sed 's/,/-/'

printf '1,2,3,4\na,b,c,d\n' | sed 's/,/-/g'

## Editing file input

cat greeting.txt

sed 's/day/weekend/' greeting.txt
Expand Down
6 changes: 6 additions & 0 deletions code_snippets/Processing_lines_bounded_by_distinct_markers.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## Uniform markers

cat uniform.txt

sed -n '/start/,/end/p' uniform.txt
Expand All @@ -16,12 +18,16 @@ sed '/start/,/end/{/start/!d}' uniform.txt

sed '/start/,/end/{/end/!d}' uniform.txt

## Extracting first or last group

sed -n '/start/,/end/{p; /end/q}' uniform.txt

sed -n '/start/,/end/{//!p; /end/q}' uniform.txt

tac uniform.txt | sed -n '/end/,/start/{p; /start/q}' | tac

## Broken groups

cat log.txt

sed -n '/warning/,/error/p' log.txt
Expand Down
Loading

0 comments on commit 2a69a4d

Please sign in to comment.