grep中哪个空白是最佳标准? [英] Which white space in grep is the best standard?
问题描述
我需要找到一个在文件中用空格括起来的单词.
I needed to find a word which is enclosed by white spaces in a file.
文件test.txt
A file, test.txt
head target_word tail
上面的单词之间的空格可以是制表符.我尝试过
Spaces between words above can be tab. And I tried
grep -e '\starget_word\s' test.txt
找不到"target_word".但是
It failed to find "target_word". But
grep -e "[[:space:]]target_word[[:space:]]" test.txt
和
grep -e "[ \t]target_word[ \t]" test.txt
运作良好.
哪个空白是最可接受的空格?请让我知道.
Which white space is the most acceptable one? Please let me know.
推荐答案
要匹配空格或制表符,可以使用 [[:: blank:]]
.不管区域设置如何,都将其定义为空格或制表符.
To match either a space or a tab, you can use [[:blank:]]
. This is defined as either a space or a tab, regardless of the locale settings.
来自 POSIX规范:
在POSIX语言环境中,仅应包含
< space>
和< tab>
.
在语言环境定义文件中,< space>
和< tab>
会自动包含在此类中.
In a locale definition file, the <space>
and <tab>
are automatically included in this class.
您还可以使用 [[:: space:]]
,但这除了空格和制表符之外,还包含其他字符.
You can also use [[:space:]]
but this contains other characters in addition to spaces and tabs.
这篇关于grep中哪个空白是最佳标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!