find multiple strings with findstr and output to a text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
markj
Posts: 1
Joined: 27 May 2010 08:45

find multiple strings with findstr and output to a text file

#1 Post by markj » 27 May 2010 08:53

Hi all, I am having some issues trying to write a batch file. Sorry I haven't had much experience with them. Basically what I am trying to do is I have a bunch of files with random names e.g. device1.txt device2.txt device3.txt. I need to search each of those files for 2 different strings within each file and then output those to a file.

e.g. to use findstr to look in device1.txt and find "Hardware" & "Serial Number" and then output those to a text file on the same line. So in the end I should have an output text file with one line for each file that was searched with the 2 results in.

e.g.

device1.txt Hardware: xyz Serial Number 1234
device2.txt Hardware: abc Serial Number 5678

Any help appreciated on this. I have been messing around a bit and not really getting anywhere. Here is an example of what I was trying..

for /F "delims=" %%a in ('findstr /s "Hardware:" *.txt') do (
set var1=%%a
echo %var1% >> %temp%\output.txt
)

Something like that but I need both results shown on one line.

Help!

Thanks 8)

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: find multiple strings with findstr and output to a text

#2 Post by !k » 27 May 2010 10:38

Code: Select all

@echo off
setlocal enableextensions
for /f "delims=" %%f in ('dir /s/b/a-d *.txt') do call :p "%%f"
goto :eof

:p
for /f "delims=" %%a in ('findstr /c:"Hardware" %1') do set "hard=%%a"
for /f "delims=" %%b in ('findstr /c:"Serial Number" %1') do set "sn=%%b"
echo %~nx1 %hard% %sn%>> %temp%\output.txt
goto :eof

Bowlardo
Posts: 15
Joined: 28 Jun 2012 07:28

Re: find multiple strings with findstr and output to a text file

#3 Post by Bowlardo » 21 Sep 2016 08:26

I'm trying to do something similar to the above .
I want to pull a string , where the line begins with UNB ,from any line in a specific file * There should on be on instance of this in the file***
I also would like to pull another string , where the line begins with BGM ,from any line in a specific file ** note there could be hundreds of these number***
I want to output the results to a new text file

I am using the following
FOR /F "tokens=6 delims=+" %%a IN ('findstr /B /C:UNB D:\atemp\test.txt') DO set "hard=%%a"
FOR /F "tokens=3 delims=+" %%b IN ('findstr /B /C:BGM D:\atemp\test.txt') do set "sn=%%b"
echo %~nx1 %hard% %sn%>> D:\atemp\output.txt

and it is giving me
D:\atemp>FOR /F "tokens=6 delims=+" %a IN ('findstr /B /C:UNB D:\atemp\test.txt'
) DO set "hard=%a"
D:\atemp>set "hard=AVMZMUCD004229"
D:\atemp>FOR /F "tokens=3 delims=+" %b IN ('findstr /B /C:BGM D:\atemp\test.txt'
) do set "sn=%b"
D:\atemp>set "sn=928324960"
D:\atemp>set "sn=928324961"
D:\atemp>set "sn=928324963"
D:\atemp>set "sn=928324964"
D:\atemp>set "sn=928324965"
D:\atemp>set "sn=928324966"
D:\atemp>echo AVMZMUCD004229 928324966 1>>D:\atemp\output.txt


when I run it the result is that I only have one entry
AVMZMUCD004229 928324966
When I was hoping to have
AVMZMUCD004229 928324960
AVMZMUCD004229 928324961
AVMZMUCD004229 928324963
AVMZMUCD004229 928324964
AVMZMUCD004229 928324965
AVMZMUCD004229 928324966


Any advice would be greatly appreciated

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: find multiple strings with findstr and output to a text file

#4 Post by Squashman » 21 Sep 2016 08:50

Do not set the for variable to an environmental variable in the second FOR command. Just echo the hard variable and the for variable to a file.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: find multiple strings with findstr and output to a text file

#5 Post by Compo » 21 Sep 2016 09:41

Will this help?

Code: Select all

For /F "Tokens=6 Delims=+" %%a In ('FindStr/BC:"UNB" D:\atemp\test.txt') Do (
   Set "hard=%%a")

(   For /F "Tokens=3 Delims=+" %%b In ('FindStr/BC:"BGM" D:\atemp\test.txt'
   ) Do Echo=%hard% %%b)>>D:\atemp\output.txt

Bowlardo
Posts: 15
Joined: 28 Jun 2012 07:28

Re: find multiple strings with findstr and output to a text file

#6 Post by Bowlardo » 30 Sep 2016 10:46

Compo wrote:Will this help?

Code: Select all

For /F "Tokens=6 Delims=+" %%a In ('FindStr/BC:"UNB" D:\atemp\test.txt') Do (
   Set "hard=%%a")

(   For /F "Tokens=3 Delims=+" %%b In ('FindStr/BC:"BGM" D:\atemp\test.txt'
   ) Do Echo=%hard% %%b)>>D:\atemp\output.txt


Thanks Compo! That worked a treat. very much appreciated :D

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: find multiple strings with findstr and output to a text file

#7 Post by Aacini » 30 Sep 2016 21:23

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "var="
(for /F "tokens=1* delims=:" %%a in ('findstr /S /C:"Hardware" /C:"Serial Number" *.txt') do (
   if not defined var (
      set "var=%%b"
   ) else (
      echo %%a  !var!  %%b
      set "var="
   )
)) > "%temp%\output.txt"

Antonio

Post Reply