Board logo

标题: [文本处理] 【已解决】批处理如何查找文本文件重复行? [打印本页]

作者: 回家路上    时间: 2015-7-9 23:22     标题: 【已解决】批处理如何查找文本文件重复行?

本帖最后由 回家路上 于 2015-7-10 09:39 编辑

问题一:
1.txt
  1. <script>1<script>
  2. <script>12<script>
  3. <script>3<script>
  4. <script>14<script>
  5. aaa
  6. bbb
  7. abcd
  8. a
复制代码
2.txt
  1. <script>1<script>
  2. <script>22<script>
  3. <script>3<script>
  4. <script>24<script>
  5. aaa
  6. bbb
复制代码
我写了,查找并输出重复行的测试代码,结果不正确,求解
  1. @echo off
  2. for /f "delims=" %%c in (1.txt) do (
  3. findstr "^%%c$" 2.txt>nul && echo %%c
  4. )
  5. pause & exit /b
复制代码
在2.txt中后面再追加一行,保证bbb不是最后一行,就能匹配到了,为什么呀。

问题二:
想要匹配两个文本文件中
  1)不重复
  2)行中包含<script>标签
的行,怎么
作者: pcl_test    时间: 2015-7-10 00:03

本帖最后由 pcl_test 于 2015-7-10 00:09 编辑
在2.txt中后面再追加一行,保证bbb不是最后一行,就能匹配到了,为什么呀。
回家路上 发表于 2015-7-9 23:22


问题一
因为你用了首尾匹配,然1.txt的bbb有回车换行,而2.txt的bbb没有,故不匹配
可用
  1. findstr /g:1.txt 2.txt
复制代码
  1. for /f "delims=" %%c in (1.txt) do findstr "\<%%c\>" 2.txt>nul && echo %%c
复制代码
问题二
  1. @echo off
  2. for /f "delims=" %%c in ('findstr "<script>" 1.txt') do set #%%c=def&echo;%%c
  3. for /f "delims=" %%i in ('findstr "<script>" 2.txt') do if not defined #%%i echo;echo%%i
  4. pause
复制代码

作者: 回家路上    时间: 2015-7-10 09:38

回复 2# pcl_test


嗯,忘了这看不到的东西了。

当时第一个问题的时候,我是想如果 2.txt 中有 “ccc a”这样的行的话,会把 1.txt 的a也匹配到,就加了行首尾匹配。结果就出问题了。呵呵

然后看到findstr原来是有相应开关的。
  1. @echo off
  2. for /f "delims=" %%c in (1.txt) do findstr /x %%c 2.txt>nul && echo %%c
  3. pause
复制代码
谢啦。:-D
作者: 回家路上    时间: 2015-7-10 11:41

本帖最后由 回家路上 于 2015-7-10 11:46 编辑

回复 2# pcl_test


当行中有特殊字符怎么办?比如有双引号?
  1. <script>"1"<script>
复制代码
这个时候
  1. for /f "delims=" %%c in (1.txt) do findstr /x %%c 2.txt>nul && echo %%c
复制代码
findstr还行吗?还是必须用你给的另一种循环的方式了。
作者: pcl_test    时间: 2015-7-10 12:00

可转义
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%c in (1.txt) do (
  3.     set str=%%c
  4.     set str=!str:"=\"!
  5.     findstr /x "!str!" 2.txt>nul && echo %%c
  6. )
  7. pause
复制代码

作者: 回家路上    时间: 2015-7-10 16:21

回复 5# pcl_test


嗯嗯,谢谢。
我对于基本命令都还没记全呢。
赶紧特地又一个一个试了试findstr命令的开关
  1. @echo off
  2. if exist findstr.txt del findstr.txt
  3. for /f "skip=113 delims=" %%a in (%~fs0) do (
  4.     echo;%%a>>findstr.txt
  5. )
  6. echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7. :: 匹配包含“hello”或“world”的行
  8. echo;findstr "hello world" findstr.txt :
  9. findstr "hello world" findstr.txt & echo;&echo;
  10. :: 匹配包含“hello world”的行
  11. echo;findstr /c:"hello world" findstr.txt :
  12. findstr /c:"hello world" findstr.txt & echo;&echo;
  13. :: 匹配包含“^hello world$”的行  全部字符原样最为一个字符串匹配 /c 默认是不正则匹配的
  14. echo;findstr /c:"^hello world$" findstr.txt :
  15. findstr /c:"^hello world$" findstr.txt & echo;&echo;
  16. :: 匹配整行只有“hello world”的行
  17. echo;findstr /x /c:"hello world" findstr.txt :
  18. findstr /x /c:"hello world" findstr.txt & echo;&echo;
  19. :: 匹配包含“hello”的行
  20. echo;findstr "\<hello\>" findstr.txt  :
  21. findstr "\<hello\>" findstr.txt & echo;&echo;
  22. :: 匹配以“world”开头的行 /b begin
  23. echo;findstr /b "world" findstr.txt :
  24. findstr /b "world" findstr.txt & echo;&echo;
  25. :: 匹配以“world”结尾的行————/e在遇到最后一行时候会识别不出,是因为最后一行没有换行符? /e end
  26. echo;findstr /e "world" findstr.txt :
  27. findstr /e "world" findstr.txt & echo;&echo;
  28. :: 匹配以“world”开头且结尾的行
  29. echo;findstr /be "world" findstr.txt :
  30. findstr /be "world" findstr.txt & echo;&echo;
  31. :: /l literally 字面地 忽略正则匹配
  32. echo;findstr /l "^hello world$" findstr.txt :
  33. findstr /l "^hello world$" findstr.txt & echo;&echo;
  34. :: /r regular 引用正则匹配
  35. :: . 通配符: 任何字符
  36. :: * 重复: 以前字符或类出现零或零以上次数
  37. :: ^ 行位置: 行的开始
  38. :: $ 行位置: 行的终点
  39. :: [class] 字符类: 任何在字符集中的字符
  40. :: [^class] 补字符类: 任何不在字符集中的字符
  41. :: [x-y] 范围: 在指定范围内的任何字符
  42. :: \x Escape: 元字符 x 的文字用法
  43. :: \<xyz 字位置: 字的开始
  44. :: xyz\> 字位置: 字的结束
  45. :: 匹配以“hello”开头或者以“world”结尾的行
  46. echo;findstr /r "^hello world$" findstr.txt :
  47. findstr /r "^hello world$" findstr.txt & echo;&echo;
  48. :: 匹配“hello world”开头且结尾的行
  49. echo;findstr /r /c:"^hello world$" findstr.txt :
  50. findstr /r /c:"^hello world$" findstr.txt & echo;&echo;
  51. :: /s subdirectory 子目录 查询范围扩展至子目录该文件
  52. echo;findstr /s "world" findstr.txt :
  53. findstr /s "world" findstr.txt & echo;&echo;
  54. :: 匹配是忽略大小写
  55. echo;findstr /i "hello world" findstr.txt :
  56. findstr /i "hello world" findstr.txt & echo;&echo;
  57. :: 匹配完全匹配“hello world”的行 /x exactly
  58. :: 匹配只有“hello”或者“world”的行
  59. echo;findstr /x "hello world" findstr.txt :
  60. findstr /x "hello world" findstr.txt & echo;&echo;
  61. :: 匹配只有“hello world”的行
  62. echo;findstr /x /c:"hello world" findstr.txt :
  63. findstr /x /c:"hello world" findstr.txt & echo;&echo;
  64. :: 匹配不含“hello”或“world”的行  /v 翻转
  65. echo;findstr /v "hello world" findstr.txt :
  66. findstr /v "hello world" findstr.txt & echo;&echo;
  67. :: 匹配结果输出时,显示行号 /n number 显示行号
  68. echo;findstr /n "hello world" findstr.txt :
  69. findstr /n "hello world" findstr.txt & echo;&echo;
  70. :: 匹配到是,只是显示文件名,/m merely 仅仅
  71. echo;findstr /m "hello world" findstr.txt :
  72. findstr /m "hello world" findstr.txt & echo;&echo;
  73. :: 在每行前打印行首字符距文件头偏移量 /o offset
  74. echo;findstr /o "hello" findstr.txt :
  75. findstr /o "hello" findstr.txt & echo;&echo;
  76. :: 用文本制定要查找的文件————findstr会以/f参数文件中列举的文件为查找范围
  77. echo;findstr /f:tmp.txt /i "hello" :
  78. (echo findstr.txt&echo notexist.txt)>tmp.txt&findstr /f:tmp.txt /i "hello" 2>nul&del tmp.txt & echo;&echo;
  79. :: 用文本制定要查找的字符串————findstr会以/g参数文件中列举的字符串作为字符串逐个在目标文件中匹配
  80. echo;findstr /g:tmp1.txt tmp2.txt :
  81. (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;
  82. :: 匹配当前目录以及上一次目录所有txt文件,查找字符串“hello”   /d 查找以分号为分隔符的目录列表
  83. echo;findstr /d:.;.. "hello" "*.txt" :
  84. findstr /d:.;.. "hello" "*.txt" & echo;&echo;
  85. ::del findstr.txt
  86. echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  87. pause & exit /b
  88. hello
  89. world
  90. hello world
  91. hello world other
  92. helloworld
  93. ^hello world$
  94. Hello World
复制代码
想问一下,查询文本文件所有以“world”(举例)结尾的行使用
  1. findstr /ie "world" findstr.txt
复制代码
而这样,又出现了最后一行没有回车换行符,不能匹配到。
需要加一个回车换行符来匹配吗?我是这样想的
  1. copy findstr.txt __findstr.txt&echo;>>__findstr.txt&findstr /ie "world" __findstr.txt&del __findstr.txt
复制代码

作者: 回家路上    时间: 2015-7-17 17:02

今天发现findstr 的/c:string开关时可以使用多个的,多个之间是或的关系(只要满足一个即可)
记录一下
  1. (echo;1111&echo;2222&echo;3333&echo;6666)|findstr /c:"1" /c:"6"
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2