regex - 如何将 "ip"字符串 grep 到文件中?

标签 regex linux bash sed grep

我正在尝试找到在某些文本中查找字符串的所有方法。我想找到更多使用 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/

相关文章:

javascript - 用正则表达式替换我不优雅的代码

c - 在 C 中抓取一个设备

linux - 如何获取bash中每条记录的最小值和最大值

python - 如何替换键+空值,Python

regex - 如何使用标准 linux utils 删除前导空格

linux - 如何在shell脚本中查找挂载目录的nfs文件夹

linux - 如何在 bash 中将自己的值添加到此脚本中

linux - rsync bash 脚本复制目录结构?

c - C 中的 Posix 正则表达式错误

c++ - 无法将 boost 安装到其他目​​录