Cílem cvičení je procvičení regulárních výrazů a příkazu grep.
grep
primárně vypíše řádky, které odpovídají (matching) RE (Regular Expression):
grep
pro BRE (Basic RE)egrep
(grep -E
) pro ERE (Extended RE)fgrep
(grep -F
) pro obyčejné řetězce (Fixed string)-o
pro výpis „only matching“ části řádku.
fray1:~alias grep='/usr/gnu/bin/grep'
fray1:~getent passwd | cut -d: -f5 >names
fray1:~grep Jan names
fray1:~grep -i jan names
fray1:~grep -vi jan names
fray1:~grep -wi jan names
fray1:~grep -wi jan names | wc -l
fray1:~grep -cwi jan names
fray1:~grep -nwi jan names
fray1:~grep -lwi jan names /etc/passwd
fray1:~ls /home/* 2>/dev/null | grep novak
fray1:~tr ':' '\n' <<<"$PATH" | grep -v bin
fray1:~info ls 2>/dev/null | grep -i list
fray1:~grep -l start /etc/* 2>/dev/null
fray1:~grep -c example /usr/man/man1/grep.1
fray1:~grep Petr names | grep Jan
fray1:~grep Petr names | grep -v student
fray1:~grep -v zam names | grep -v student
fray1:~grep -v zam names | grep -v student | grep -v host
fray1:~... | grep -v host | grep -v dokt
fray1:~... | grep -v dokt | grep -v User
fray1:~... | grep -v User | grep -v UID
fray1:~... | grep -v UID | grep -v Admin
fray1:~grep -v zam names | grep -v student | grep -v host \
| grep -v dokt | grep -v User | grep -v UID | grep -v Admin
fray1:~grep Admin names
fray1:~grep UID names
fray1:~grep -e Admin names
fray1:~grep -e Admin -e UID names
fray1:~printf '%s\n' Admin UID User >re
fray1:~grep -f re names
fray1:~grep --color start /etc/init.d/sendmail
fray1:~grep --color -o start /etc/init.d/sendmail
fray1:~grep --color -A3 start /etc/init.d/sendmail
fray1:~grep --color -B3 start /etc/init.d/sendmail
fray1:~grep --color -C3 start /etc/init.d/sendmail
fray1:~alias grep='/usr/gnu/bin/grep --color'
[...]
nebo libovoný znak zapsaný jako .
fray1:~PATH=/usr/gnu/bin:$PATH
fray1:~alias grep='grep --color'
fray1:~alias grep='/usr/gnu/bin/grep --color'
fray1:~cd /home/courses/BIPS1/public/re
fray1:.../regrep word words
fray1:.../regrep ^word words
fray1:.../regrep word$ words
fray1:.../regrep '\<ll' words
fray1:.../regrep 'who\>' words
fray1:.../regrep '\<who\>' words
fray1:.../regrep -w 'who' words
fray1:.../regrep '^[Ww]ord' words
fray1:.../regrep '^[XYZ]' words
fray1:.../regrep '^[X-Z]' words
fray1:.../reLC_ALL=C grep '^[X-Z]' words
fray1:.../regrep '^[[:upper:]]' words
fray1:.../regrep '^[^[:alpha:]]' words
fray1:.../regrep '.' words
fray1:.../regrep -c '.' words
fray1:.../rewc -l <words
fray1:.../regrep '^$' words
fray1:.../regrep -v '.' words
fray1:.../regrep '^.$' words
fray1:.../regrep '\.' words
fray1:.../regrep '^[xyz].$' words
*
— libovolněkrát, nebo vůbec (0,1,2,...)\+
— jednou nebo vícekrát [ERE] (1,2,...)\{n,m\}
— n
až m
-krát, m
lze vynechat\?
— volitelně (0,1) [ERE]
fray1:.../regrep 'xx' words
fray1:.../regrep 'x.x' words
fray1:.../regrep 'x..x' words
fray1:.../regrep 'x...x' words
fray1:.../regrep 'x.*x' words
fray1:.../regrep 'x..*x' words
fray1:.../regrep -E 'x.+x' words
fray1:.../reegrep 'x.+x' words
fray1:.../regrep 'x.\+x' words
fray1:.../regrep '^.\{2,3\}$' words
fray1:.../reegrep '^.{2,3}$' words
fray1:.../regrep '[[:upper:]]\{2,3\}' words
fray1:.../regrep '[[:upper:]]\{5,\}' words
fray1:.../regrep '[[:upper:]]\{5\}' words
fray1:.../regrep '^[[:upper:]]\{2,3\}$' words
fray1:.../regrep '^[[:upper:]]\{5,\}$' words
fray1:.../regrep '^[[:upper:]]\{5\}$' words
fray1:.../reegrep '^O.?[[:upper:]]' words
fray1:.../reegrep --color '^.*' words
fray1:.../reegrep --color '^[[:upper:]]{2,}' words
fray1:.../reegrep --color '[[:upper:]]+$' words
fray1:.../reegrep --color '^[[:upper:]].?[[:upper:]]' words
fray1:.../regetent passwd | grep '^.*:.*:[1-9][0-9]\{3\}:'
^ .*
↓ ↓
^ .* :
↓ ↓
^ .* : .* :
↓ ↓ ↓
^ .* : .* : UID:
↓ ↓ ↓ ↓
⋅qq_13_04:##qq_13_04:9125:1001:docasne konto student:/home/stud/qq_13_04:/bin/bash
↑ ↑ ↑ ↑
^ [^:]* : [^:]* : UID:
fray1:.../regetent passwd | grep '^[^:]*:[^:]*:[1-9][0-9]\{3\}:'
\
mění speciální význam znaků (ruší nebo nastavuje)\(...\)
, resp. (...)
\1
... \9
fray1:.../regrep '[error]' apache.log
fray1:.../regrep '\[error]' apache.log
fray1:.../refgrep '[error]' apache.log
fray1:.../regrep -F '[error]' apache.log
fray1:.../regrep '.zip' apache.log
fray1:.../regrep '\.zip' apache.log
fray1:.../refgrep '.zip' apache.log
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
January February March April May June July August
September October November December
…grep '[MTWTFS][ouehra][neduit][sneru]*day' words
…egrep '(Mon|T(ue|hur)s|Wednes|Fri|S(atur|un))day' words
…grep '^[JFMASOND][aepuco][nbrynlgptvc][uarychestmbo]*$' words
…egrep '^((Jan|Febr)uary|April|Ma(rch|y)|Ju(ne|ly)|'\
'August|(Octo|(Sept|Nov|Dec)em)ber)$' words
fray1:.../reegrep '^([[:upper:]].*){5}$' words
fray1:.../reegrep '^[^[:upper:]]*([[:upper:]][^[:upper:]]*){5}$' words
fray1:.../regrep '^\(.\)\(.\).\2\1$' words
fray1:.../reegrep '^(.)(.).\2\1$' words
fray1:.../reinfo bash | grep '\<\([^ ][^ ]*\)\>.*\<\1\>'
fray1:.../reinfo bash | egrep '\<([^ ]+)\>.*\<\1\>'