- 论坛徽章:
- 1
|
本帖最后由 defcon 于 2012-04-19 08:34 编辑
大牛勿喷,菜鸟可以掌握掌握,少走弯路
grep 'root' /etc/passwd >/dev/null 2>&1
if [ $? -eq 0 ]
then
.....
你还在用这样的判断方式吗?,那你是没有好好看grep的帮助文档
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit
immediately with zero status if any match is found, even if an
error was detected. Also see the -s or --no-messages option.
(-q is specified by POSIX.)
其实一个-q 参数就可以静默的输入。以后记得用这个啦
grep -q 'root' /etc/passwd
if [ $? -eq 0 ]
then
.....
还有一个文件就是你还在sed或者其他方式来查找指定的字符所在行的上下N行吗?
那你又out了,看man手册
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing a group separator (--) between
contiguous groups of matches. With the -o or --only-matching
option, this has no effect and a warning is given.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing a group separator (--) between
contiguous groups of matches. With the -o or --only-matching
option, this has no effect and a warning is given.
-C NUM, -NUM, --context=NUM
Print NUM lines of output context. Places a line containing a
group separator (--) between contiguous groups of matches. With
the -o or --only-matching option, this has no effect and a
warning is given.
例子:
[root@shanker ~]# seq 10 |grep 5 -A3
5
6
7
8
[root@shanker ~]# seq 10|grep 5 -B3
2
3
4
5
[root@shanker ~]# seq 10|grep 5 -C3
2
3
4
5
6
7
8
很容易理解吧
|
|