过滤匹配数据:

[root@hunt1574 bin]#
cat nohup.out | grep java.lang.ArrayIndexOut
    2012-05-10 11:48:10,447 ERROR [STDERR] java.lang.ArrayIndexOutOfBoundsException: 1
 
此时查找到了报错的数据,但我不知道该数据的上下文是什么,
grep 中其实已提供查出匹配行的上下文参数。效果如下:
 
[root@hunt1574 bin]#
cat nohup.out | grep -2 java.lang.ArrayIndexOut
2012-05-10 11:48:10,326 INFO [STDOUT] load tfa_trapconfig_main from db successful
2012-05-10 11:48:10,338 INFO [STDOUT] load tsm_device_mapping from db successful
2012-05-10 11:48:10,447 ERROR [STDERR] java.lang.ArrayIndexOutOfBoundsException: 1
2012-05-10 11:48:10,447 ERROR [STDERR] at com.boco.nms.collection.memory.database.ref.dcentry.DCRangeEntry.<init>(DCRangeEntry.java:18)
2012-05-10 11:48:10,448 ERROR [STDERR] at com.boco.nms.collection.memory.database.operation.Database$1.eachItem(Database.java:185)
 
Grep  -n 指定匹配数据的前后
N行数据。
 
很明显显示行过多时,我们查看匹配行会有些困难,此时可以这样做:
 
[root@hunt1574 bin]# cat nohup.out | grep --color -2 java.lang.ArrayIndexOutO
2012-05-10 11:48:10,326 INFO [STDOUT] load tfa_trapconfig_main from db successful
2012-05-10 11:48:10,338 INFO [STDOUT] load tsm_device_mapping from db successful
2012-05-10 11:48:10,447 ERROR [STDERR] java.lang.ArrayIndexOutOfBoundsException: 1
2012-05-10 11:48:10,447 ERROR [STDERR] at com.boco.nms.collection.memory.database.ref.dcentry.DCRangeEntry.<init>(DCRangeEntry.java:18)
2012-05-10 11:48:10,448 ERROR [STDERR] at com.boco.nms.collection.memory.database.operation.Database$1.eachItem(Database.java:185)
 
--color 查高亮显示匹配行。
 
Grep还支持以下用法:
-A n
a
after匹配指定行的后
n行数据
-B n
b
before 匹配指定行的前
N行数据。
-C n:显示匹配行的前后
N
-n :显示匹配的行号
-E :等价于
egrep
-v :匹配非过滤内容