Wednesday, January 30, 2013

GREP command : How to use ??


GREP:
Three version of grep command in UNIX 'grep, fgrep, egrep'.
'fgrep' stands for Fixed 'grep'.
'egrep' Extended 'grep'.

1. To find relevant word and exclusion irrelevant word.
# grep Exception logfile.txt | grep -v ERROR

2.If you want to count of a particular word in log file you can use grep -c option to count the word.
# grep -c "Error" logfile.txt

3. grep --context option allows us to print lines around matching pattern. Below example of grep command in UNIX will print 6 lines around matching line of word "successful" in logfile.txt
# grep --context=6 successful logfile.txt
# grep -C 2 'hello' *
Prints two lines of context around each matching line.

4. egrep stands for extended grep and it is more powerful than grep command in Unix and allows more regular exception
# egrep 'Error|Exception' logfile.txt

5. If you want to do case insensitive search than use -i option from grep command in UNIX. Grep -i will find occurrence of both Error, error and ERROR
# grep -i Error logfile

6. Below command will print all files which have "Error" on them. ZGREP used to perform same operation as grep does but with .gz files
# zgrep -i Error *.gz

7. Above grep command in UNIX searches only for instances of 'ERROR' that are entire words; it does not match `SysERROR'.
# grep -w ERROR logfile
# grep 'ERROR>' *
Searches only for words ending in 'ERROR', so it matches the word `SysERROR'.

8. "grep -l" which display only the file names which matches the given pattern. Below command will only display file names which have ERROR
# grep -l ERROR *.log

9. If you want to see line number of matching lines you can use option "grep -n" below command will show on which lines Error has appeared.
# grep -n ERROR log file.

10. If you want to do recursive search using grep command in Unix there are two options either use "-R" command line option or increase directory one by one as shown below.
Read more ...

grep命令基础练习题


常用的grep选项是:

-c 只输出匹配行的计数。
-i 不区分大小写(只适用于单字符)。
-h 查询多文件时不显示文件名。
-l 查询多文件时只输出包含匹配字符的文件名。
-n 显示匹配行及行号。
-s 不显示不存在或无匹配文本的错误信息。
-v 显示不包含匹配文本的所有行。

文件格式:

第1列:城市位置编号。
第2列:月份。
第3列:存储代码及出库年份。
第4列:产品代号。
第5列:产品统一标价。
第6列:标识号。
第7列:合格数量。

file.txt文件内容:
48      Dec     3BC1977 LPSX   68.00   LVX2A   138
483     Sept    5AP1996 USP    65.00   LVX2C   189
47      Oct     3ZL1998 LPSX   43.00   KVM9D   512
219     dec     2CC1999 CAD    23.00   PLV2C   68
484     nov     7PL1996 CAD    49.00   PLV2C   234
483     may     5PA1998 USP    37.00   KVM9D   644
216     sept    3ZL1998 USP    86.00   KVM9E   234

练习:

1. 含有“48”字符串的行的总数
[root@www.linuxidc.com]# grep -c 48 file.txt
4

2. 显示含有“48”字符串的所有行的行号
[root@www.linuxidc.com]# grep -n 48 file.txt
1:48      Dec     3BC1977 LPSX    68.00   LVX2A   138
2:483     Sept    5AP1996 USP     65.00   LVX2C   189
5:484     nov     7PL1996 CAD     49.00   PLV2C   234
6:483     may     5PA1998 USP     37.00   KVM9D   644

3. 精确匹配只含有“48”字符串的行
[root@www.linuxidc.com]# grep "48\>" file.txt
48      Dec     3BC1977 LPSX    68.00   LVX2A   138

PS:使用grep抽取精确匹配的一种更有效方式是在抽取字符串后加\ >

4. 抽取代码为4 8 4和4 8 3的城市位置
[root@www.linuxidc.com]# grep -E "48[3|4]\>" file.txt
483     Sept    5AP1996 USP     65.00   LVX2C   189
484     nov     7PL1996 CAD     49.00   PLV2C   234
483     may     5PA1998 USP     37.00   KVM9D   644

PS:使用[ ]来指定字符串范围

5. 显示使行首不是4或8
[root@www.linuxidc.com]# grep -v "^[4|8]" file.txt

[root@www.linuxidc.com]# grep -v "^[48]" file.txt

[root@www.linuxidc.com]# grep -v "^[4,8]" file.txt

[root@www.linuxidc.com]# grep "^[^48]" file.txt       #这个是直接最简单的方法
219     dec     2CC1999 CAD     23.00   PLV2C   68
216     sept    3ZL1998 USP     86.00   KVM9E   234

PS:可以在方括号中使用^记号,表示行首不是某字符

6. 显示含有九月份的行
[root@www.linuxidc.com]# grep -i Sept file.txt

[root@www.linuxidc.com]# grep -E "[Ss]ept" file.txt
483     Sept    5AP1996 USP     65.00   LVX2C   189
216     sept    3ZL1998 USP     86.00   KVM9E   234

PS :使用- i开关可以屏蔽月份S e p t的大小写敏感

7. 显示以K开头,以D结尾的所有代码
[root@www.linuxidc.com]# grep "K...D" file.txt
47      Oct     3ZL1998 LPSX    43.00   KVM9D   512
483     may     5PA1998 USP     37.00   KVM9D   644

PS:点.代表任意一个字符,由于代码是五位字符的字符串,所以中间用三个点表示任意字符

8. 显示头两个是大写字母,中间两个任意,并以C结尾的代码
[root@www.linuxidc.com]# grep "[A-Z][A-Z]..C" file.txt
483     Sept    5AP1996 USP     65.00   LVX2C   189
219     dec     2CC1999 CAD     23.00   PLV2C   68
484     nov     7PL1996 CAD     49.00   PLV2C   234

PS:[A-Z]表示任意一个大写字母

9. 查询所有以5开始以1 9 9 6或1 9 9 8结尾的所有记录
[root@www.linuxidc.com]# grep "5..199[68]" file.txt
483     Sept    5AP1996 USP     65.00   LVX2C   189
483     may     5PA1998 USP     37.00   KVM9D   644

10. 假定要取得城市代码,第一个字符为0-9,第二个字符在0到5之间,第三个字符在0到6之间
[root@www.linuxidc.com]# grep "[0-9][0-5][0-6]" file.txt        #一般情况下很多人会想到使用这个错误方式,没考虑到最后一列也是三位数字
47      Oct     3ZL1998 LPSX    43.00   KVM9D   512
484     nov     7PL1996 CAD     49.00   PLV2C   234
483     may     5PA1998 USP     37.00   KVM9D   644
216     sept    3ZL1998 USP     86.00   KVM9E   234

[root@www.linuxidc.com]# grep "^[0-9][0-5][0-6]" file.txt
216     sept    3ZL1998 USP     86.00   KVM9E   234

11. 抽取包含数字4至少重复出现两次的所有行
[root@www.linuxidc.com]# grep "4\{2,\}" file.txt
483     may     5PA1998 USP     37.00   KVM9D   644

12. 匹配8重复出现2到6次,并以3结尾
[root@www.linuxidc.com]# cat num.txt
83.
888883
8884
88883

[root@www.linuxidc.com]# grep "8\{2,6\}3" num.txt
888883
88883

13. 匹配特殊字符
查询有特殊含义的字符,诸如$ . ' " * [] ^ |\ + ? ,必须在特定字符前加\进行转义
如要查询文件名c o n f t r o l l . co n f(这是一个配置文件)
[root@www.linuxidc.com]# grep "conftroll\.conf" file.txt

14. 查询格式化文件名
格式:最多六个小写字符,后跟句点,接着是两个大写字符
[root@www.linuxidc.com]# grep "[a-z]\{1,6\}\.[A-Z]\{2\}" file.txt

15. 查询IP地址
要查看n n n . n n n网络地址
[root@www.linuxidc.com]# grep "[0-9]\{3\}\.[0-9]\{3\}\." 的IP地址范围为1.0.0.1-127.255.255.254;

16. 类名
[ [ : u p p e r : ] ][ A - Z ] [ [ : a l n u m : ] ] [ 0 - 9 a - zA-Z]
[ [ : l o w e r : ] ] [ a - z ] [ [ : s p a c e : ] ] 空格或t a b键
[ [ : d i g i t : ] ] [ 0 - 9 ] [ [ : a l p h a : ] ] [ a - z A - Z ]
围为:192.0.1.1-223.255.255.254

取以5开头,后跟至少两个大写字母
[root@www.linuxidc.com]# grep "5[[:upper:]]\{2,\}" file.txt
483     Sept    5AP1996 USP     65.00   LVX2C   189
483     may     5PA1998 USP     37.00   KVM9D   644

取以P或D结尾的所有产品代码
[root@www.linuxidc.com]# grep "[[:upper:]]\{2\}[P,D]" file.txt
483     Sept    5AP1996 USP     65.00   LVX2C   189
219     dec     2CC1999 CAD     23.00   PLV2C   68
484     nov     7PL1996 CAD     49.00   PLV2C   234
483     may     5PA1998 USP     37.00   KVM9D   644
216     sept    3ZL1998 USP     86.00   KVM9E   234
Read more ...