我正在尝试找到在某些文本中查找字符串的所有方法。我想找到更多使用 grep 或 sed 的方法。 (请记住区分大小写)
- 每个包含字符串“ip”的单词(字符串)并将输出结果重定向到/root/found;
grep ip /usr/share/dict/words/ > /root/found
- 只是以“ip”开头的单词(字符串)并将输出结果重定向到/root/found;
grep ^ip /usr/share/dict/words > /root/found
- 只需输入“ip”并将输出结果重定向到/root/found;
grep ^ip$ /usr/share/dict/words > /root/found
最佳答案
如果您希望使用 >
将输出附加到文件/root/found 中,则必须使用 >>
Grep 不考虑大小写:
grep -i "ip" /ust/share/dict/words >> /root/found
如果要搜索/ust/share/dict/words的子目录和当前目录下的所有文件
find /ust/share/dict/words -name "*" -exec grep "ip" '{}' \; -print >> /root/found
关于regex - 如何将 "ip"字符串 grep 到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31101271/