Shell grep的几个参数
来源:互联网 发布:jsp是javascript吗 编辑:程序博客网 时间:2024/06/07 06:52
Shell grep的几个参数
lxg@2015-03-16
-o参数
echo "aaaabc"|grep -o "aa"|grep -v grep的输出结果是?如果存在一个”testing:grep”的进程,你想通过 ps aux|grep –o “testing: grep”|grep –v grep来查找进程,那么前面的输出结果是?
第一个示例的输出是:
lxg ~ $ echo "aaaabc"|grep -o "aa"
aa
aa
第二个示例的输出是:
lxg ~ $ ps aux|grep –o “testing: grep”|grep –v grep
testing: grep
testing: grep
从上面的输出还真是有点跟我自己预想的结果有一些不一样,预想的两个示例输出应该都只有一行了,但是从结果来看每一个输出都有两个一模一样的输出结果。
首先看一下grep –o参数的含义:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
-o就是只输出匹配PATTERN的字符串,也就是说只要跟PATTERN匹配那么就会输出,而不是整个行,而且只会输出匹配部分。
所以第一个示例中因为” aaaabc”可以匹配到”aa”两次,那么输出就会输出两个”aa”字符串,如果把示例换成” echo"aaaabc"|grep -o ‘ab’”那么输出也就只有一个”ab”字符串了。
第二个示例中我明明在后面进行了grep –v grep操作了,为什么还是会有两个输出呢?这是因为-o参数只会输出匹配部分,而不是输出匹配的整行了。所以经过-o参数后已经没有grep字符串了,也就是说最后的grep –v没有起作用。
从上文来看如果没有理解-o参数的真实含义,那么很有可能就会掉到一个坑里面,本来以为只匹配一次其实匹配到了两次。
-c参数,输出匹配的个数,而不是匹配的内容。有时候你想判断当前有几个” testing: grep”进程在运行,一种方式是:
lxg ~ $ ps aux|grep –o “testing: grep”|grep –v grep|wc -l
1
另外一种方式是:
lxg ~ $ ps aux|grep –c “testing: grep”|grep –v grep -c
1
在查找进程的时候,利用[]实现同时grep –vgrep的功能。
lxg ~ $ ps aux|grep “testing: grep”
lxg 11722 0.0 0.0 5804 724 pts/1 S+ 11:46 0:00 grep --colour=auto testing: grep #grep自身也出来了
lxg 11513 0.0 0.0 5804 154 pts/1 S+ 10:26 0:00 testing: grep
lxg ~ $ ps aux|grep “testing: grep”|grep –v grep
lxg 11513 0.0 0.0 5804 154 pts/1 S+ 10:26 0:00 testing: grep #-v 去掉grep进程的结果
lxg ~ $ ps aux|grep “[t]esting: grep”
lxg 11513 0.0 0.0 5804 154 pts/1 S+ 10:26 0:00 testing: grep #神奇的事情发生了
从上面的输出来看[]起到了一个很神奇的作用,其实这个的原理很简单了:
grep “[t]esting: grep”这个进程要在ps进程先启动,然后ps执行结束后,grep再继续执行。所以ps进程对其命令记录的是完全的字符串” [t]esting: grep”,然后等ps结束后grep运行的时候对” [t]esting: grep”进行正则表达式的解析,[t]esting: grep解析后就是testing: grep。
我们还可以加上下面这个验证:
ps -ef | tee pro.log | grep [b]ash
cat pro.log | grep grep
lxg 6868 6795 0 13:07 pts/1 00:00:00 grep [b]ash
- Shell grep的几个参数
- grep 匹配的几个参数
- grep命令几个重要的参数
- shell grep 参数详解
- 几个有用的shell参数
- shell grep的作用
- grep 的Pattern 参数
- grep 命令 的参数
- grep的高级参数
- grep sort uniq wc等几个常用于shell编程的命令
- shell几个特殊参数应用
- shell之grep的学习
- linux shell grep的用法
- Shell Grep命令的使用
- grep和find的参数疑云: 左手shell通配符,右手正则表达式
- 几个常用的文本处理shell 命令:find、grep、xargs、sort、uniq、tr、cut、paste、wc、sed、awk
- 几个常用的文本处理shell 命令:find、grep、xargs、sort、uniq、tr、cut、paste、wc、sed、awk
- 【shell】grep
- Oracle数据库远程连接设置的四种方法及其注意事项
- xfire+spring+hibernate 配置文件
- 黑马程序员————OC中内存管理有关
- mysql中int型的数字怎么转换成字符串
- HBase source code. HStore
- Shell grep的几个参数
- 生成一个包含数字和字母的自定义位数的验证码
- 使用VS2013加载文件:使用Unicode(UTF-8)加载文件时。。。。
- windows7中大量的透明元素是怎么显示出来的
- 畅通工程(并查集的运用)
- 深入理解Linux网络技术内幕——中断与网络驱动程序
- Windows下利用live555实现H264实时流RTSP发送
- Python的logging功能使用过程中的一个问题
- Binary String Matching