2.txt复制代码
- <script>1<script>
- <script>12<script>
- <script>3<script>
- <script>14<script>
- aaa
- bbb
- abcd
- a
我写了,查找并输出重复行的测试代码,结果不正确,求解复制代码
- <script>1<script>
- <script>22<script>
- <script>3<script>
- <script>24<script>
- aaa
- bbb
在2.txt中后面再追加一行,保证bbb不是最后一行,就能匹配到了,为什么呀。复制代码
- @echo off
- for /f "delims=" %%c in (1.txt) do (
- findstr "^%%c$" 2.txt>nul && echo %%c
- )
- pause & exit /b
或复制代码
- findstr /g:1.txt 2.txt
问题二复制代码
- for /f "delims=" %%c in (1.txt) do findstr "\<%%c\>" 2.txt>nul && echo %%c
复制代码
- @echo off
- for /f "delims=" %%c in ('findstr "<script>" 1.txt') do set #%%c=def&echo;%%c
- for /f "delims=" %%i in ('findstr "<script>" 2.txt') do if not defined #%%i echo;echo%%i
- pause
谢啦。:-D复制代码
- @echo off
- for /f "delims=" %%c in (1.txt) do findstr /x %%c 2.txt>nul && echo %%c
- pause
这个时候复制代码
- <script>"1"<script>
findstr还行吗?还是必须用你给的另一种循环的方式了。复制代码
- for /f "delims=" %%c in (1.txt) do findstr /x %%c 2.txt>nul && echo %%c
复制代码
- @echo off&setlocal enabledelayedexpansion
- for /f "delims=" %%c in (1.txt) do (
- set str=%%c
- set str=!str:"=\"!
- findstr /x "!str!" 2.txt>nul && echo %%c
- )
- pause
想问一下,查询文本文件所有以“world”(举例)结尾的行使用复制代码
- @echo off
- if exist findstr.txt del findstr.txt
- for /f "skip=113 delims=" %%a in (%~fs0) do (
- echo;%%a>>findstr.txt
- )
- echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- :: 匹配包含“hello”或“world”的行
- echo;findstr "hello world" findstr.txt :
- findstr "hello world" findstr.txt & echo;&echo;
- :: 匹配包含“hello world”的行
- echo;findstr /c:"hello world" findstr.txt :
- findstr /c:"hello world" findstr.txt & echo;&echo;
- :: 匹配包含“^hello world$”的行 全部字符原样最为一个字符串匹配 /c 默认是不正则匹配的
- echo;findstr /c:"^hello world$" findstr.txt :
- findstr /c:"^hello world$" findstr.txt & echo;&echo;
- :: 匹配整行只有“hello world”的行
- echo;findstr /x /c:"hello world" findstr.txt :
- findstr /x /c:"hello world" findstr.txt & echo;&echo;
- :: 匹配包含“hello”的行
- echo;findstr "\<hello\>" findstr.txt :
- findstr "\<hello\>" findstr.txt & echo;&echo;
- :: 匹配以“world”开头的行 /b begin
- echo;findstr /b "world" findstr.txt :
- findstr /b "world" findstr.txt & echo;&echo;
- :: 匹配以“world”结尾的行————/e在遇到最后一行时候会识别不出,是因为最后一行没有换行符? /e end
- echo;findstr /e "world" findstr.txt :
- findstr /e "world" findstr.txt & echo;&echo;
- :: 匹配以“world”开头且结尾的行
- echo;findstr /be "world" findstr.txt :
- findstr /be "world" findstr.txt & echo;&echo;
- :: /l literally 字面地 忽略正则匹配
- echo;findstr /l "^hello world$" findstr.txt :
- findstr /l "^hello world$" findstr.txt & echo;&echo;
- :: /r regular 引用正则匹配
- :: . 通配符: 任何字符
- :: * 重复: 以前字符或类出现零或零以上次数
- :: ^ 行位置: 行的开始
- :: $ 行位置: 行的终点
- :: [class] 字符类: 任何在字符集中的字符
- :: [^class] 补字符类: 任何不在字符集中的字符
- :: [x-y] 范围: 在指定范围内的任何字符
- :: \x Escape: 元字符 x 的文字用法
- :: \<xyz 字位置: 字的开始
- :: xyz\> 字位置: 字的结束
- :: 匹配以“hello”开头或者以“world”结尾的行
- echo;findstr /r "^hello world$" findstr.txt :
- findstr /r "^hello world$" findstr.txt & echo;&echo;
- :: 匹配“hello world”开头且结尾的行
- echo;findstr /r /c:"^hello world$" findstr.txt :
- findstr /r /c:"^hello world$" findstr.txt & echo;&echo;
- :: /s subdirectory 子目录 查询范围扩展至子目录该文件
- echo;findstr /s "world" findstr.txt :
- findstr /s "world" findstr.txt & echo;&echo;
- :: 匹配是忽略大小写
- echo;findstr /i "hello world" findstr.txt :
- findstr /i "hello world" findstr.txt & echo;&echo;
- :: 匹配完全匹配“hello world”的行 /x exactly
- :: 匹配只有“hello”或者“world”的行
- echo;findstr /x "hello world" findstr.txt :
- findstr /x "hello world" findstr.txt & echo;&echo;
- :: 匹配只有“hello world”的行
- echo;findstr /x /c:"hello world" findstr.txt :
- findstr /x /c:"hello world" findstr.txt & echo;&echo;
- :: 匹配不含“hello”或“world”的行 /v 翻转
- echo;findstr /v "hello world" findstr.txt :
- findstr /v "hello world" findstr.txt & echo;&echo;
- :: 匹配结果输出时,显示行号 /n number 显示行号
- echo;findstr /n "hello world" findstr.txt :
- findstr /n "hello world" findstr.txt & echo;&echo;
- :: 匹配到是,只是显示文件名,/m merely 仅仅
- echo;findstr /m "hello world" findstr.txt :
- findstr /m "hello world" findstr.txt & echo;&echo;
- :: 在每行前打印行首字符距文件头偏移量 /o offset
- echo;findstr /o "hello" findstr.txt :
- findstr /o "hello" findstr.txt & echo;&echo;
- :: 用文本制定要查找的文件————findstr会以/f参数文件中列举的文件为查找范围
- echo;findstr /f:tmp.txt /i "hello" :
- (echo findstr.txt&echo notexist.txt)>tmp.txt&findstr /f:tmp.txt /i "hello" 2>nul&del tmp.txt & echo;&echo;
- :: 用文本制定要查找的字符串————findstr会以/g参数文件中列举的字符串作为字符串逐个在目标文件中匹配
- echo;findstr /g:tmp1.txt tmp2.txt :
- (echo 111&echo 222)>tmp1.txt&(echo 111&echo 123&echo 222&echo 333)>tmp2.txt&findstr /g:tmp1.txt tmp2.txt&del tmp1.txt tmp2.txt & echo;&echo;
- :: 匹配当前目录以及上一次目录所有txt文件,查找字符串“hello” /d 查找以分号为分隔符的目录列表
- echo;findstr /d:.;.. "hello" "*.txt" :
- findstr /d:.;.. "hello" "*.txt" & echo;&echo;
- ::del findstr.txt
- echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- pause & exit /b
- hello
- world
- hello world
- hello world other
- helloworld
- ^hello world$
- Hello World
而这样,又出现了最后一行没有回车换行符,不能匹配到。复制代码
- findstr /ie "world" findstr.txt
复制代码
- copy findstr.txt __findstr.txt&echo;>>__findstr.txt&findstr /ie "world" __findstr.txt&del __findstr.txt
复制代码
- (echo;1111&echo;2222&echo;3333&echo;6666)|findstr /c:"1" /c:"6"
欢迎光临 批处理之家 (http://www.bathome.net/) | Powered by Discuz! 7.2 |