定义:Grep (Globally search for the reqular expression and print out the line).
好处:Grep 在执行时不需要先调用编辑程序,同时不需要以“//” 来括起规则表达式,所以比vi等快很多。
格式:grep pattern filename1 filename2 .....
术语:pattern, 字符串样式,多被单引号‘ ’包围。
原理:Grep 在文件里查找规则表达式,并打印包含该表达式的所有行。
Practice script
#/bin/bash echo 'here is a grep script' echo '======= print the content of employees' cat employees #search line including eric grep eric employees echo '======= grep option practice' #show line and line number grep -n '^er' employees #print bash return, 0,1,2 echo $? #print line number only grep -c '^er' employees #print filename only grep -l '^er' employees awkfile #display matching word file and line grep -w tokoyo employees awkfile #ignore capital grep -i tOKoyo employees awkfile #print unmatch line grep -v tokoyo employees #mark module number grep -b tokoyo employees #print error only grep -s tokoyo employees #regular expression practice echo '======= regular expression practice' grep frank emp* grep '^green' employees grep '12$' employees grep '939..' employees grep '^[ej]' employees grep '^j..[nk]' employees grep '<mar.*' employees grep '<mar.*12' employees grep -vin 'ic' employees grep -l 'ic' employe*
Output of the script
# ./grep.sh here is a grep script ======= print the content of employees eric beijing 123456 10 john xian 849383 11 mark henan 939394 12 frank tokoyo 283098 2 green england 788888 3 eric beijing 123456 10 ======= grep option practice 1:eric beijing 123456 10 0 1 employees employees:frank tokoyo 283098 2 employees:frank tokoyo 283098 2 eric beijing 123456 10 john xian 849383 11 mark henan 939394 12 green england 788888 3 70:frank tokoyo 283098 2 frank tokoyo 283098 2 ======= regular expression practice frank tokoyo 283098 2 green england 788888 3 mark henan 939394 12 mark henan 939394 12 eric beijing 123456 10 john xian 849383 11 john xian 849383 11 mark henan 939394 12 mark henan 939394 12 2:john xian 849383 11 3:mark henan 939394 12 4:frank tokoyo 283098 2 5:green england 788888 3 employees
q