findstr 指令基本格式
基本格式:findstr “搜索內(nèi)容” 文件路徑
1. 單字符串搜索
::關(guān)閉回顯,設(shè)置延遲環(huán)境變量擴(kuò)展
@echo off &setlocal enabledelayedexpansion
:: 1.表示從test.txt中篩選包含 hello 的行,并導(dǎo)入到tmp.txt
findstr "hello" test.txt >>tmp.txt
:: 導(dǎo)入空行
echo=>>tmp.txt
:: 2.表示將test.txt中所有內(nèi)容導(dǎo)入到tmp.txt
findstr . test.txt >>tmp.txt
pause
結(jié)果:
2. 多字符串搜索
@echo off &setlocal enabledelayedexpansion
findstr "hello adc" test.txt >tmp.txt
pause
指令常用參數(shù)
詳細(xì)參數(shù)列表
參數(shù) | 參數(shù)說(shuō)明 |
---|---|
/B | 在一行的開(kāi)始配對(duì)模式。 |
/E | 在一行的結(jié)尾配對(duì)模式。 |
/L | 按字使用搜索字符串。 |
/R | 將搜索字符串作為一般表達(dá)式使用。 |
/S | 在當(dāng)前目錄和所有子目錄中搜索匹配文件。 |
/I | 指定搜索不分大小寫(xiě)。 |
/X | 打印完全匹配的行。 |
/V | 只打印不包含匹配的行。 |
/N | 在匹配的每行前打印行數(shù)。 |
/M | 如果文件含有匹配項(xiàng),只打印其文件名。 |
/O | 在每個(gè)匹配行前打印字符偏移量。 |
/P | 忽略有不可打印字符的文件。 |
/OFF[LINE] | 不跳過(guò)帶有脫機(jī)屬性集的文件。 |
/A:attr | 指定有十六進(jìn)位數(shù)字的顏色屬性。請(qǐng)見(jiàn) “color /?” |
/F:file | 從指定文件讀文件列表 (/ 代表控制臺(tái))。 |
/C:string | 使用指定字符串作為文字搜索字符串。 |
/G:file | 從指定的文件獲得搜索字符串。 (/ 代表控制臺(tái))。 |
/D:dir | 查找以分號(hào)為分隔符的目錄列表 |
strings | 要查找的文字。 |
[drive:][path]filename | 指定要查找的文件。 |
1. 參數(shù) /i(I) 忽略大小寫(xiě)
@echo off &setlocal enabledelayedexpansion
findstr /i "hello" test.txt >tmp.txt
pause
2. 參數(shù) /C:string 查找包含空格的字符串所在行
@echo off &setlocal enabledelayedexpansion
findstr /c:"t -a" test.txt >tmp.txt
pause
結(jié)果:
3. 參數(shù) /n 顯示篩選結(jié)果的行號(hào)
@echo off &setlocal enabledelayedexpansion
findstr /n "hello" test.txt >tmp.txt
pause
注意:行尾會(huì)添加一個(gè)空格
結(jié)果:
4. 參數(shù) /v 匹配結(jié)果反選
@echo off &setlocal enabledelayedexpansion
findstr /v "hello" test.txt >tmp.txt
pause
結(jié)果:
4. 參數(shù) /s 遞歸查找
@echo off &setlocal enabledelayedexpansion
:: 搜索當(dāng)前目錄及子目錄下所有的 .txt 文件,并查找還有 hello 字符串的行, 并顯示行號(hào)
findstr /n /s "hello" *.txt >tmp.txt
pause
簡(jiǎn)單腳本應(yīng)用 --文件中指定行的指定內(nèi)容替換
::關(guān)閉回顯,設(shè)置延遲環(huán)境變量擴(kuò)展
@echo off &setlocal enabledelayedexpansion
set fileName=.\test.txt
set oldText=hello
set newText=adc
set featureText=test
echo fileName=%fileName% oldText=%oldText% newText=%newText%
if not exist %fileName% (
echo txt -hello the world!>>test.txt
echo txt -hello the home!>>test.txt
echo test -hello the game!>>test.txt
)
if defined featureText (echo featureText=%featureText%)
::單引號(hào)代表命令 . 表示所有內(nèi)容
for /f "delims=" %%a in ('findstr /n . %fileName%') do (
set str=%%a
::echo !str!
rem 替換內(nèi)容
if defined featureText (
rem 查找每行字符串是否包含指定的特征字符,只對(duì)包含特征字符的行替換文本
rem >null表示不顯示結(jié)果
echo !str!| findstr %featureText% >nul && (
set str=!str:%oldText%=%newText%!
)
) else (
set "str=!str:%oldText%=%newText%!"
)
rem 將添加了行號(hào)的文本寫(xiě)入臨時(shí)文件
echo !str! >>tmp.txt
)
for /f "tokens=1* delims=:" %%i in (.\tmp.txt) do (
rem 按 : 分割每行字符串
set "str=%%j"
if "!str!"==" " (
rem 寫(xiě)入源文件里的空行
echo=>>new_A.txt
) else (
rem 將字符串寫(xiě)入文本,每行會(huì)多一個(gè)空格,使用字符串的截取功能去掉末尾的一個(gè)空格
echo !str:~0,-1!>>new_A.txt
)
)
rem 刪除臨時(shí)文件并將修改后的文件修改為源文件
del tmp.txt&move new_A.txt %fileName%
pause
結(jié)果:
原文本內(nèi)容
替換后文本內(nèi)容 文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-495203.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-495203.html
到了這里,關(guān)于Windows 批處理(bat) findstr命令使用教程的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!