How to show the place and only the words found Findstr?

时间:2015-06-15 14:37:36

标签: batch-file findstr

I wonder how do I show the location where the words were found in findstr?

My Code:

Set "LFiles=%temp%\Files\*.txt">nul 2>&1

Findstr /li /G:"List.txt" "%LFiles%">"Result.TxT"

(for /F %%a in (List.txt) do (
   Findstr /li /C:"%%a" "%LFiles%" > NUL
   if not errorlevel 1 echo %%a
))>"Result2.TxT"

List:

Disc
Music
Song
Album

Result:

DISC1312we7das67da
13dsdsa67dsahdsa7aMUSIC
dsadsdfdsaSONG1223234235

Result2:

Disc
Music
Song

The desired result in Result2:

--------------
Filelist1.txt
--------------
Song

--------------
Filelist2.txt
--------------
Song
Disc

--------------
Filelist3.txt
--------------
Disc
Music
Song

Note: the filenames are random

@Edit

I could make him show you what was found in each file...

Came here two problems:

  1. How do I show him the file names if they have spaces in the name?
  2. How do I join the results of the same file on a single line?

.

Dir /b "%temp%\Files\*.txt">Files2.txt

Set "LFiles=LFiles=%temp%\Files\*.txt">nul 2>&1

Findstr /li /G:"List.txt" "%LFiles%">"Result.TxT"

(for /F %%a in (Files2.txt) do (
(for /F %%b in (List.txt) do (
   Findstr /li /C:"%%b" "Result.txt" > NUL
   if not errorlevel 1 echo "%%a" "%%b"
))))>>"Result2.TxT"

Current result:

"File" "Disc"
"File" "Song"
"File" "Music"
"Files_and" "Disc"

The desired result

File 01.txt: Song, Music
File 02.txt: Music, Disc

or

File 01.txt:
Song
Music

File 02.txt:
Music
Disc

2 个答案:

答案 0 :(得分:1)

@echo off
setlocal enabledelayedexpansion
set "LFiles=%temp%\Files\*.txt"
(
for %%i in ("%LFiles%") do (
  set "result="
  for /f %%j in (list.txt) do (
    findstr /li /c:"%%j" "%%~i">nul && set result=!result!, %%j
  )
  if defined result echo %%i:!result:~1!
)
)>result.txt

答案 1 :(得分:1)

以下解决方案在List.txt文件中每个单词执行findstr命令只执行一次,因此它应该运行得更快。它还将结果保存在" file"用于任何进一步处理的数组:

@echo off
setlocal EnableDelayedExpansion

Set "LFiles=%temp%\Files\*.txt"

for /F %%a in (List.txt) do (
   for /F "delims=" %%b in ('findstr /LIM /C:"%%a" "%LFiles%"') do (
      set "file[%%~Nb]=!file[%%~Nb]! %%a"
      set /A "word[%%a]+=1"
   )
)

(for /F "tokens=2* delims=[]=" %%a in ('set file[') do (
   echo --------------
   echo %%a.txt
   echo --------------
   for %%c in (%%b) do echo %%c
   echo/
)) > Result2.txt

ECHO RESULT IN REQUESTED FORMAT:
TYPE RESULT2.TXT
ECHO/
ECHO/
ECHO VALUES STORED IN "FILE" ARRAY:
SET FILE[

ECHO WORD COUNT:
SET WORD[

编辑:按照评论中的要求计算每个文件每个单词数的新方法。

@echo off
setlocal EnableDelayedExpansion

Set "LFiles=%temp%\Files\*.txt"

rem Accumulate counts of all file/word combinations in a two-dimensional array
for /F %%a in (List.txt) do (
   for /F "delims=:" %%b in ('findstr /LI /C:"%%a" "%LFiles%"') do (
      set /A "count[%%~Nb][%%a]+=1"
   )
)

rem Group word counts for the same file
for /F "tokens=2,3* delims=[]=" %%a in ('set count[') do (
   set "file[%%a]=!file[%%a]!, %%b (%%c)"
)

rem Show the final result
(for /F "tokens=2* delims=[]=" %%a in ('set file[') do (
   set "line=%%b"
   echo %%a.txt = !line:~2!
)) > Result2.TxT


ECHO THE COUNT ARRAY:
SET COUNT[
ECHO/
ECHO THE FILE ARRAY:
SET FILE[

此新方法计算每个文件中匹配的的数量。如果相同的单词可能在一行中出现两次或多次,则它将被计为一次。这一点可能是固定的,以便计算单个单词,但结果代码会慢得多......