Solved Searching for and extracting lines of text from a file.

October 23, 2012 at 05:16:54
Specs: Windows XP
I want to create a batch file that will find specific text in a file and copy the line the text is in and the next line after it into a text file (on the same line separated by a comma). For example: I want to find "Address" and copy:
"Address 123 A Street, Zip Code 10003"
out of a file that contains:
"Name John Smith
Address 123 A Street
Zip Code 10003
USA"
Thanks very much,
Gabe.

See More: Searching for and extracting lines of text from a file.

Report •


✔ Best Answer
October 23, 2012 at 08:41:03
There, works perfectly:
@echo off & setlocal EnableDelayedExpansion
set cnt=0
for /F "delims=" %%j in (sample.txt) do (
set /A cnt+=1
set line!cnt!=%%j
)
set /A cnt -=1
set second=!line%cnt%!
set /A cnt -=1
set first=!line%cnt%!
set result=%first%, %second%
echo %result%>> result.txt
:LOOP
set /A cnt -=3
set second=!line%cnt%!
set /A cnt -=1
set first=!line%cnt%!
set result=%first%, %second%
echo %result%>> result.txt
if "%result%" LSS "1" exit /b
Goto :LOOP



#1
October 23, 2012 at 06:35:02
Will the names be identical, e.g will the second line following the address always be 'zip code'?

Report •

#2
October 23, 2012 at 06:37:36
No the second line will actually be a unique number.
A findstr can be executed on the word address but I can't figure out how to send that string and the next one below it to a single line in another text file.

Report •

#3
October 23, 2012 at 06:48:23
By that format this will work perfectly:
@echo off & setlocal EnableDelayedExpansion
set string=?
set /p string=Enter string to search for:
for /f "delims=" %%a in ('findstr /I /C:"%string%" sample.txt') do set first=!first!%%a
set cnt=0
for /F "delims=" %%j in (sample.txt) do (
set /A cnt+=1
set line!cnt!=%%j
)
set /a cnt -=1
set second=!line%cnt%!
set result=%first%, %second%
echo.%result%
pause > nul
exit /b


Report •

Related Solutions

#4
October 23, 2012 at 07:18:11
So? is it fine?

Report •

#5
October 23, 2012 at 07:30:00
As long as the origional poster calls it homework.bat right?

:: mike


Report •

#6
October 23, 2012 at 07:47:17
Sorry about the delay - I redirected the output to a text file using >> Result.txt echo.%%a.
I'm getting unpredictable results though. The input file is actually quite messy and is probably causing some problems (blank lines). Is there a limit to the string length?

Report •

#7
October 23, 2012 at 08:14:31
Here, it outputs the result to result.txt and opens the file
@echo off & setlocal EnableDelayedExpansion
set string=?
set /p string=Enter string to search for:
for /f "delims=" %%a in ('findstr /I /C:"%string%" sample.txt') do set first=!first!%%a
set cnt=0
for /F "delims=" %%j in (sample.txt) do (
set /A cnt+=1
set line!cnt!=%%j
)
set /a cnt -=1
set second=!line%cnt%!
set result=%first%, %second%
echo."%result%"> result.txt
notepad.exe result.txt
exit /b


Report •

#8
October 23, 2012 at 08:14:35
I cleaned up the input file and I can see the results more clearly in the output. All of the strings with the search term are put in one continuous line (with no spaces) followed by a space and comma and the the string after the last instance of the search term.

Report •

#9
October 23, 2012 at 08:16:24
Not clear by what you mean, post what you're getting.

Report •

#10
October 23, 2012 at 08:19:00
Input Data:
Name John626 Smith
Address 654245 A Street
1096787003
USA
Name John24562 Smith
Address 5424 A Street
1009643703
USA
Name John26254 Smith
Address 4256 A Street
18753003
USA
Name John245625 Smith
Address 457 A Street
1086576003
USA
Name John24566 Smith
Address 23767 A Street
100286536703
USA
Name John245626 Smith
Address 14532 A Street
100256203
USA

Output:
Address 654245 A StreetAddress 5424 A StreetAddress 4256 A StreetAddress 457 A StreetAddress 23767 A StreetAddress 14532 A Street, 100256203

Desired Output:
Address 654245 A Street, 1096787003
Address 5424 A Street, 1009643703
Address 4256 A Street, 18753003
Address 457 A Street, 1086576003
...


Report •

#11
October 23, 2012 at 08:21:12
The script i posted only works with the format you provided in the beginning, as i previously mentioned. Will fix.

Report •

#12
October 23, 2012 at 08:41:03
✔ Best Answer
There, works perfectly:
@echo off & setlocal EnableDelayedExpansion
set cnt=0
for /F "delims=" %%j in (sample.txt) do (
set /A cnt+=1
set line!cnt!=%%j
)
set /A cnt -=1
set second=!line%cnt%!
set /A cnt -=1
set first=!line%cnt%!
set result=%first%, %second%
echo %result%>> result.txt
:LOOP
set /A cnt -=3
set second=!line%cnt%!
set /A cnt -=1
set first=!line%cnt%!
set result=%first%, %second%
echo %result%>> result.txt
if "%result%" LSS "1" exit /b
Goto :LOOP


Report •

#13
October 23, 2012 at 08:41:38
Sorry about that, I thought I should be brief but I was too brief.

Report •

#14
October 23, 2012 at 08:44:21
Btw the output will be in reverse order, from bottom to top.
Keep in mind lol, that script will only work with the format provided e.g if address is 1 line further down it will fail.
Don't get me wrong, if you only have two sets e.g
Name John626 Smith
Address 654245 A Street
1096787003
USA
Name John24562 Smith
Address 5424 A Street
1009643703
USA
,it will work whereas if you have
Name John24562 Smith
1009643703
USA
Address 5424 A Street
it will fail.
Anyway tell me, is it fine?

Report •

#15
October 23, 2012 at 08:52:19
According to my tests it does what it should, when using the input you mentioned in your previous post the output is:
Address 23767 A Street, 100286536703
Address 457 A Street, 1086576003
Address 4256 A Street, 18753003
Address 5424 A Street, 1009643703
Address 654245 A Street, 1096787003

,

Report •

#16
October 23, 2012 at 09:08:20
Yes absolutely. Thanks very much!
You dropped the findstr and went with a line counter, Is it possible to use findstr to look for an instance of a search term and then grab the line and the following line in cases where there is bad data in the file or blank lines?

Report •

#17
October 23, 2012 at 09:18:42
Blank lines are no problem, as for doing that with findstr alone, no, you'd have to use the for command in addition for that, couldn't be bothered to go into it now. It's late and still have a lot of unrelated work to complete. Don't forget to select the best answer ;).

Report •

#18
October 23, 2012 at 12:26:39
Thank again, I really appreciate the time you've taken (as someone posted above, I will have to do my homework).

Report •


Ask Question