findstr multiple search in one single output line

2019-08-17 07:52发布

I have multiple text files that contains 3 lines of information that I want to output as one single line for each file

Example

File1.txt contains

User: "John"
Date: "13-March-2017"
Time: "10.30am"
Remarks: "xcvsfas"

File2.txt contains

User: "Mary"
Date: "13-March-2017"
Time: "11.30am"
Remarks: "xerteyas"

My expected output is as follows

c:\temp\file1.txt:User: "John"; Date: "13-March-2017"; Time: "10.30am"
c:\temp\file2.txt:User: "Mary"; Date: "13-March-2017"; Time: "11.30am"

I tried findstr /s /i "user date time:" %inputfolder%\*.* > %outputfolder%\final.txt

3条回答
可以哭但决不认输i
2楼-- · 2019-08-17 08:36

I'm assuming all your .txt are in the same folder and only them. Then I get the first three lines of each file and print them in one line by using the set command like:

@echo off

setlocal EnableDelayedExpansion

pushd <files dir>
for /f %%i in ('dir /b') do (
    set "c=0"
    for /f "tokens=*" %%j in ('type "%%i"') do (
        set /a "c=c+1"
        if "!c!" equ "3" (
            set /p "=%%j" <nul
        ) else if "!c!" lss "3" (
            set /p "=%%j; "<nul
        )
    )
    echo(
)
popd

I tested with the input you gave and the result is:

User: "John"; Date: "13-March-2017"; Time: "10.30am";
User: "Mary"; Date: "13-March-2017"; Time: "11.30am";

Hope it helps.

查看更多
虎瘦雄心在
3楼-- · 2019-08-17 08:39

EDIT: Code modified per new specifications posted in a comment in other answer... :(

@echo off
setlocal EnableDelayedExpansion

set "file="
(
for /F "tokens=1* delims=:" %%a in ('findstr /S /I "user date time" %inputfolder%\*.*') do (
   if "!file!" neq "%%a" (
      if defined file echo !file!:!out!
      set "file=%%a"
      set "out=%%b"
   ) else (
      set "out=!out!; %%b"
   )
)
echo !file!:!out!
) > %outputfolder%\final.txt
查看更多
手持菜刀,她持情操
4楼-- · 2019-08-17 08:44

You could loop through the files by a for loop and do the search individually -- like this:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_LOCATION=."
set "_INPUTFILES=File*.txt"
set "_OUTPUTFILE=%~dpn0.log"

rem // Write everything into output file:
> "%_OUTPUTFILE%" (
    rem // Iterate over matching files recursively:
    for /R "%_LOCATION%" %%F in ("%_INPUTFILES%") do (
        rem // Initialise line string variable:
        set "LINE=%%~F:"
        rem // Search currently iterated input file for the keywords:
        for /F "delims=" %%L in ('findstr /L /I /B "User: Date: Time:" "%%~F"') do (
            rem // Store found item:
            set "ITEM=%%L"
            rem // Toggle delayed expansion in order not to lose exclamation marks:
            setlocal EnableDelayedExpansion
            rem // Build line string and transfer result over `endlocal` barrier:
            for /F "delims=" %%E in ("!LINE!!ITEM!; ") do (
                endlocal
                set "LINE=%%E"
            )
        )
        rem // Return built line string:
        setlocal EnableDelayedExpansion
        echo !LINE:~,-2!
        endlocal
    )
)

endlocal
exit /B

The fields User:, Date: and Time: are returned in the original order they appear in every file.

查看更多
登录 后发表回答