
我喜欢Linux上的grep
命令,它有助于轻松地搜索和过滤字符串,总是想知道Windows上的等效工具是什么,并在最近找到了这个findstr
。
在本文中,我将分享一些我在Linux上最喜欢的“ grep”示例,以及如何使用“ findstr”命令将其“移植”到Windows。
1.筛选结果
1.1过滤列表结果的经典示例。
#Linux
$ ls -ls | grep mkyong
#Windows
c:\> dir | findstr mkyong
1.2添加忽略大小写,并使用多个字符串过滤列表结果。
#Linux - Need '-E' option and Uses "|" to separate multiple search strings.
$ ls -ls | grep -iE "mkyong|music"
#Windows - Use spaces to separate multiple search strings
c:\> dir | findstr -i "mkyong music"
2.搜索文件
2.1在文件中搜索匹配的字符串。
#Linux
$ grep mkyong test.txt
#Windows
c:\> findstr mkyong test.txt
2.2计算比赛次数。
#Linux
$ grep -c mkyong test.txt
#Windows - Piped with find /c command.
c:\> findstr -N "mkyong" test.txt | find /c ":"
3.搜索文件列表
3.1在文件列表中搜索匹配的字符串。
#Linux
$ grep mkyong -lr /path/folder
#Windows
c:\> findstr /M mkyong c:\folder\*
*(grep)-l,(findstr)/ M =仅打印包含匹配项的文件的名称。
4.帮助
4.1最强大的命令〜
#Linux
$ grep --help
$ man grep
#Windows
c:\> findstr -?
注意
还有其他例子吗? 请在下面分享它,谢谢。
参考文献
翻译自: https://mkyong.com/linux/grep-for-windows-findstr-example/