Archive

Posts Tagged ‘grep’

Tips: grep, output only matched patterns

June 13th, 2006

“sed的in-place edit选项,和RTFM”中,说到过:

…关于 Search and Replace 和 Regular Expression,还有一个问题困扰我,在这儿记下来。 就是将文件中符合某个pattern的字符串打印出来。注意,只是符合这个pattern的字符串本身,而不是所在行。直接一看,这怎么都是grep的工作嘛,可以在对grep狠狠的RTFM以后,还是未果…


GNU grep's new features上看到

If you haven't been paying attention to GNU grep recently, you should be happily surprised by some of the new features and options that have come about with the 2.5 series. They bring it functionality you can't get anyWhere else — including the ability to output only matched patterns (not lines), color output, and new file and directory options.


于是特地又看了一下grep的man page,我手边的是公司里cygwin下的grep-2.5.1a,果然有这么一个参数:

  -o, --only-matching       show only the part of a line matching PATTERN


试了一下,真是好用。看来是2.5系列新加的功能。(不过现在又不免想,是不是早在发sed的in-place edit选项,和RTFM,grep就已经有-o可用了?汗~……)

Linux ,