grep 仅显示匹配的字符
程序员文章站
2022-07-12 14:12:38
...
默认情况下grep会显示匹配了正则的整行,但是我们的正则只想显示匹配了的字符时怎么办
[quote]
This is an updated and cross distribution safe answer
grep -oh "[[:alpha:]]*th[[:alpha:]]*" 'filename'
To summaries -oh outputs the regular expression matches to the file content (and not its filename), just like how you would expect regular expression to work in vim/etc... What word or regular expression you would be searching for then, is up to you! As long as you remain to POSIX and not perl syntax (refer below)
More from the manual for grep
-o Print each match, but only the match, not the entire line.
-h Never print filename headers (i.e. filenames) with output lines.
-w The expression is searched for as a word (as if surrounded by
`[[:<:]]' and `[[:>:]]';
The reason why the original answer does not work for everyone
[/quote]
[url=http://*.com/questions/1546711/can-grep-show-only-words-that-match-search-pattern]Can grep show only words that match search pattern?[/url]
于是,就有了,查看weblogic GC日志的命令
在多个weblogic服务器日志上找FullGC
查找日志末5000行,打印文件名和错误的行
-E选项,支持扩展的正则表达式,支持或“|”操作
grep -oh "[[:alpha:]]*th[[:alpha:]]*" 'filename'
[quote]
This is an updated and cross distribution safe answer
grep -oh "[[:alpha:]]*th[[:alpha:]]*" 'filename'
To summaries -oh outputs the regular expression matches to the file content (and not its filename), just like how you would expect regular expression to work in vim/etc... What word or regular expression you would be searching for then, is up to you! As long as you remain to POSIX and not perl syntax (refer below)
More from the manual for grep
-o Print each match, but only the match, not the entire line.
-h Never print filename headers (i.e. filenames) with output lines.
-w The expression is searched for as a word (as if surrounded by
`[[:<:]]' and `[[:>:]]';
The reason why the original answer does not work for everyone
[/quote]
[url=http://*.com/questions/1546711/can-grep-show-only-words-that-match-search-pattern]Can grep show only words that match search pattern?[/url]
于是,就有了,查看weblogic GC日志的命令
在多个weblogic服务器日志上找FullGC
//查找FullGC
grep 'Full' `head -n 10 weblogic.log |grep -oh 'gc_.*log'`
查找日志末5000行,打印文件名和错误的行
tail -n 50000 */weblogic*.log |grep -E "weblogic.*log|2937u38338"
-E选项,支持扩展的正则表达式,支持或“|”操作
上一篇: 每日一句
下一篇: 英语笔记--每日一句
推荐阅读
-
aabccd统计每个字符出现的次数,结果显示{ a: 2, b: 1, c: 2, d: 1 };去掉重复的字符,使结果显示abcd
-
Vista系统下无线连接时显示“用于网络的保存在该计算机上的设置与网络的要求不匹配”
-
关于mysql查询字符集不匹配问题的解决方法
-
jsp页面中EL表达式被当成字符串处理不显示值问题的解决方法
-
js正则匹配字符串中的数字(js正则匹配字符串中的数字讲解)
-
Python实现字符串匹配的KMP算法
-
TextVie获取显示字符串的宽度之Android开发
-
HTML识别后台传输或者js变量中字符串里的 '\n' 并成功换行显示
-
PHP的preg_match匹配字符串长度问题解决方法
-
python3 kmp 字符串匹配的方法