Unix grep-w,仅以空格作为分隔符
如何将grep设置为仅使用空格作为单词的分隔符?您无法更改Unix grep-w,仅以空格作为分隔符,unix,grep,word,Unix,Grep,Word,如何将grep设置为仅使用空格作为单词的分隔符?您无法更改grep-w的工作方式。但是,您可以使用tr或sed将标点替换为X字符,然后使用grep-w,这将起到作用。单词regexp标志有用,但有限。grep手册页上说: grep -w uses punctuations and whitespaces as delimiters. 如果您想使用自定义字段分隔符,awk可能更适合您。或者你可以用egrep或者grep--extended regexp编写一个扩展正则表达式,这样你就可以更好地
grep-w
的工作方式。但是,您可以使用tr
或sed
将标点替换为X
字符,然后使用grep-w
,这将起到作用。单词regexp标志有用,但有限。grep手册页上说:
grep -w uses punctuations and whitespaces as delimiters.
如果您想使用自定义字段分隔符,awk可能更适合您。或者你可以用egrep或者
grep--extended regexp
编写一个扩展正则表达式,这样你就可以更好地控制你的搜索模式。如果你只想匹配空格:grep-w foo
与grep“foo”相同。如果您还想匹配行尾或制表符,您可以开始这样做:grep'\(^\\\\\)foo\($\\\\\)”
,但您最好使用perl-ne'print If/\sfoo\s/'
使用tr
以新行替换空格。然后grep
您的字符串。我需要的连续字符串被grep-w
拆分,因为它有冒号。此外,我只知道第一部分,第二部分是我需要提取的未知数据。因此,以下几点帮助了我
-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.
请通过发布您尝试过的代码的相关部分来改进您的问题。此外,请将您正在使用的任何样本作为语料库进行测试。这个问题非常简单,需要粘贴代码。这是一个理论问题。威廉·珀塞尔理解并正确地回答了这个问题。我怎样才能用awk做到这一点?我想在一行中搜索一个特定的单词。如果它在那里,我必须考虑其他行忽略。it@ganducoder请使用您当前正在处理的代码的相关部分更新您的问题;人们会很乐意帮忙,但不会为你写。此外,包括您正在使用的任何样本作为语料库进行测试。
echo "$your_content" | tr ' ' '\n' | grep 'string'