findstr multiple search in one single output line

混江龙づ霸主 提交于 2019-12-08 10:40:39

问题


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


回答1:


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



回答2:


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:


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.



来源:https://stackoverflow.com/questions/43223179/findstr-multiple-search-in-one-single-output-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!