某次, 要在某日志中搜索一个错误码, 结果错用了grep命令, 最后被误导, 浪费半个小时, 下面我来简化叙述一下:
[taoge@localhost test]$ cat test.txt
-100
-99
-98
0
1
2
3
98
99
[taoge@localhost test]$ grep "-100" test.txt
敲了这么命令, 过了一段时间后, 发现没有任何反应, 查了一下, 以为文件太大所致(那个日志文件确实很大)。 过了一段时间后, 还是没有反应, 我想当然地以为没有对应的错误码日志“-100”, 就这样, 自己被误导了。
注意到, "-100"前面的“-”是一个特殊符号, 所以必须用转义来转, 如下:
[taoge@localhost test]$ cat test.txt
-100
-99
-98
0
1
2
3
98
99
[taoge@localhost test]$ grep "\-100" test.txt
-100
[taoge@localhost test]$
如果用cat test.txt | grep "-100"则会直接报错, 如下:
[taoge@localhost test]$ cat test.txt
-100
-99
-98
0
1
2
3
98
99
[taoge@localhost test]$ cat test.txt | grep "-100"
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
[taoge@localhost test]$
OK, 要吸取类似教训, 出现异常时, 也不要想当然。