【发布时间】:2011-02-26 16:48:21
【问题描述】:
如果出现了单词列表中的至少一个元素,grep 可以选择行吗?例如
grep "hello world" file1
grep 必须给我所有包含单词 hello 或单词 world 或两者的行。
【问题讨论】:
标签: linux shell scripting csh tcsh
如果出现了单词列表中的至少一个元素,grep 可以选择行吗?例如
grep "hello world" file1
grep 必须给我所有包含单词 hello 或单词 world 或两者的行。
【问题讨论】:
标签: linux shell scripting csh tcsh
grep "hello\|world" file1
【讨论】:
将您的模式放入某个文件 patterns.txt,每行一个模式,然后运行
grep -Ff patterns.txt file1
【讨论】:
怎么样
grep -r "hello\|world" file1
顺便说一句,这是一个递归的grep。它在 file1 中递归搜索术语“hello world”。它也可以应用于这样的目录:
grep -r "hello\|world" dir/dir2/
【讨论】:
试试这个,
echo "hello world "| grep -o "\bworld\b"
输出是
world
或
grep -E 'hello|world' filename
【讨论】:
1 hello world、2 hello 和3 the world-wide web,应返回所有三行。您的答案不会选择第 2 行。我也不认为答案应该只返回单词。