如何grep只是将字符串“ip”写入文件?

时间:2023-01-09 15:29:06

I am trying to find every way to find a string in some text. I want to find more ways using grep or sed. (bear in mind It's case sensitive)

我试图找到在某些文本中找到字符串的各种方法。我想找到更多使用grep或sed的方法。 (请记住这是区分大小写的)

  • Every word (strings) containing string "ip" and redirect the output result in /root/found;
  • 包含字符串“ip”的每个单词(字符串),并将输出结果重定向到/ root / found;
 grep ip /usr/share/dict/words/ > /root/found
  • Just words (strings) initiating with "ip" and redirect the output result in /root/found;
  • 只是用“ip”启动的单词(字符串)并将输出结果重定向到/ root / found;
 grep ^ip  /usr/share/dict/words > /root/found 
  • Just the word "ip" and redirect the output result in /root/found;
  • 只需单词“ip”并将输出结果重定向到/ root / found;
grep ^ip$ /usr/share/dict/words > /root/found

1 个解决方案

#1


1  

If you prefer to append output into file /root/found instead using > you have to use >>

如果您希望使用>将输出附加到文件/ root / found中,则必须使用>>

Grep over irrespective of case:

无论如何,Grep over:

grep -i "ip" /ust/share/dict/words >> /root/found 

If you want to search all files in sub directories and current directories of /ust/share/dict/words

如果要搜索/ ust / share / dict / words的子目录和当前目录中的所有文件

find /ust/share/dict/words -name "*" -exec grep "ip"  '{}' \; -print  >> /root/found 

#1


1  

If you prefer to append output into file /root/found instead using > you have to use >>

如果您希望使用>将输出附加到文件/ root / found中,则必须使用>>

Grep over irrespective of case:

无论如何,Grep over:

grep -i "ip" /ust/share/dict/words >> /root/found 

If you want to search all files in sub directories and current directories of /ust/share/dict/words

如果要搜索/ ust / share / dict / words的子目录和当前目录中的所有文件

find /ust/share/dict/words -name "*" -exec grep "ip"  '{}' \; -print  >> /root/found