findstr命令

findstr命令

玩服务器,很多时候是linux,常用grep,偶尔也会遇到windows服务器,要做自动化,findstr也很常用。

findstr用来搜索匹配字符串的文件。

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        strings [[drive:][path]filename[ ...]]

常用参数:

S:在当前目录和子目录中搜索匹配的文件

X:打印出完全匹配要搜索的字符串的行

N:打印匹配字符串行的同时加上行号

I:搜索时不区分大小写

M:匹配字符串时打印出该文件名

注意/C:string的用法。比如说你想在当前目录下搜索包含有“I'll be the bright and black that's making you run”这句话文件,输出文件名。

findstr /M /C:"I'll be the bright and black that's making you run" .*

不加/C:这表示只要字符串中的任意子串符合即可。

FINDSTR命令的详细帮助:

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        strings [[drive:][path]filename[ ...]]

  /B         在一行的开始配对模式。
  /E         在一行的结尾配对模式。
  /L         按字使用搜索字符串。
  /R         将搜索字符串作为一般表达式使用。
  /S         在当前目录和所有子目录中搜索匹配文件。
  /I         指定搜索不分大小写。
  /X         打印完全匹配的行。
  /V         只打印不包含匹配的行。
  /N         在匹配的每行前打印行数。
  /M         如果文件含有匹配项,只打印其文件名。
  /O         在每个匹配行前打印字符偏移量。
  /P         忽略有不可打印字符的文件。
  /OFF[LINE] 不跳过带有脱机属性集的文件。
  /A:attr    指定有十六进位数字的颜色属性。请见 "color /?"
  /F:file    从指定文件读文件列表 (/ 代表控制台)。
  /C:string  使用指定字符串作为文字搜索字符串。
  /G:file    从指定的文件获得搜索字符串。 (/ 代表控制台)。
  /D:dir     查找以分号为分隔符的目录列表
  strings    要查找的文字。
  [drive:][path]filename
             指定要查找的文件。

例如:
不用 /c:参数 what is 每个都是一个独立的搜索项
D:\setup\apache-maven-3.8.6>findstr /I /N "what is" README.txt
4:  What is it?
7:  Maven is a software project management and comprehension tool. Based on
19:  The full list of changes can be found at https://maven.apache.org/docs/history.html.
25:    1.7 or above (this is to execute Maven - it still allows you to build against 1.3
29:  Disk:
30:    Approximately 10MB is required for the Maven installation itself. In addition to
31:    that, additional disk space will be used for your local Maven repository. The size
36:    Unix based systems (Linux, Solaris and Mac OS X) and others:
44:    Unix-based operating systems (Linux, Solaris and Mac OS X)
53:    Unix-based operating systems (Linux, Solaris and Mac OS X)
58:  4) Make sure JAVA_HOME is set to the location of your JDK
60:  5) Run "mvn --version" to verify that it is correctly installed.
74:  Release Notes:      https://maven.apache.org/docs/history.html
75:  Mailing Lists:      https://maven.apache.org/mailing-lists.html
77:  Issue Tracking:     https://issues.apache.org/jira/browse/MNG
78:  Wiki:               https://cwiki.apache.org/confluence/display/MAVEN/

D:\setup\apache-maven-3.8.6>findstr /I /N /c:"what is" README.txt
4:  What is it?



一般表达式的快速参考:
  .        通配符: 任何字符
  *        重复: 以前字符或类出现零或零以上次数
  ^        行位置: 行的开始
  $        行位置: 行的终点
  [class]  字符类: 任何在字符集中的字符
  [^class] 补字符类: 任何不在字符集中的字符
  [x-y]    范围: 在指定范围内的任何字符
  x       Escape: 元字符 x 的文字用法
  <xyz    字位置: 字的开始
  xyz>    字位置: 字的结束

常用场景实例

D:\setup\apache-maven-3.8.6>netstat -an -o -i |findstr 8081
TCP    192.168.0.112:58225    116.162.46.142:8081    TIME_WAIT          6     87606
posted @ 2022-09-26 10:00  红尘过客2022  阅读(468)  评论(0编辑  收藏  举报