grep -w with only space as delimiter

2020-04-11 18:43发布

grep -w uses punctuations and whitespaces as delimiters. 

How can I set grep to only use whitespaces as a delimiter for a word?

标签: unix grep word
4条回答
兄弟一词,经得起流年.
2楼-- · 2020-04-11 19:02

If you want to match just spaces: grep -w foo is the same as grep " foo ". If you also want to match line endings or tabs you can start doing things like: grep '\(^\| \)foo\($\| \)', but you're probably better off with perl -ne 'print if /\sfoo\s/'

查看更多
叛逆
3楼-- · 2020-04-11 19:09

The --word-regexp flag is useful, but limited. The grep man page says:

   -w, --word-regexp
          Select  only  those  lines  containing  matches  that form whole
          words.  The test is that the matching substring must  either  be
          at  the  beginning  of  the  line,  or  preceded  by  a non-word
          constituent character.  Similarly, it must be either at the  end
          of  the  line  or  followed by a non-word constituent character.
          Word-constituent  characters  are  letters,  digits,   and   the
          underscore.

If you want to use custom field separators, awk may be a better fit for you. Or you could just write an extended regular expression with egrep or grep --extended-regexp that gives you more control over your search pattern.

查看更多
小情绪 Triste *
4楼-- · 2020-04-11 19:25

You cannot change the way grep -w works. However, you can replace punctuations with, say, X character using tr or sed and then use grep -w, that will do the trick.

查看更多
霸刀☆藐视天下
5楼-- · 2020-04-11 19:28

Use tr to replace spaces with new lines. Then grep your string. The contiguous string I needed was being split up with grep -w because it has colons in it. Furthermore, I only knew the first part, and the second part was the unknown data I needed to pull. Therefore, the following helped me.

echo "$your_content" | tr ' ' '\n' | grep 'string'
查看更多
登录 后发表回答