Parsing Text File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dnishu
Posts: 4
Joined: 27 Jan 2012 10:11

Parsing Text File

#1 Post by dnishu » 27 Jan 2012 10:22

I want to parse file based on starting line text and ending line text. File looks something like,
Year end
123 555 666
456 546 656
Total
Can I parse text starting from year end to totals? I would appreciate DOS batch file to do this. Thanks

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

Re: Parsing Text File

#2 Post by Aacini » 29 Jan 2012 22:26

If you want to process the lines contained in a text file between "Year end" and "Total":

Code: Select all

@echo off
set "start=Year end"
set "end=Total"
call :ParseFile < input.txt
goto :EOF

:ParseFile
   set line=
   set /P line=
   if not defined line exit /B
if not "%line%" == "%start%" goto ParseFile
echo %line%
:ShowLines
   set line=
   set /P line=
   if not defined line exit /B
   echo %line%
if not "%line%" == "%end%" goto ShowLines
exit /B

Please note that this Batch file ends at the first empty line in the file. However, this limitation may be avoided if needed.

dnishu
Posts: 4
Joined: 27 Jan 2012 10:11

Re: Parsing Text File

#3 Post by dnishu » 30 Jan 2012 08:04

Yes it ends at the first empty line which is a problem for me. Can you please suggest how to fix that. I'm new to this thus please guide me to the process the batch file is doing. Thanks for your help

Also, if multiple files in a folder can be parsed. Files like input1, input2, input3 ect and output can be placed in new text file. I'm sorry for being too demanding.

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

Re: Parsing Text File

#4 Post by Aacini » 31 Jan 2012 06:49

The Batch file below have these modifications: 1- It process several files with .TXT extension, 2- It does not end at the first empty line in the file(s), and 3- It store the output in RESULT.OUT file; note that this file can NOT have .TXT extension because otherwise it interfere with the *.TXT files being processed:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "start=Year end"
set "end=Total"
if exist result.out del result.out
for %%f in (*.txt) do (
   findstr /N ^^ < %%f | find /C ":" > numLines.tmp
   set /P numLines=< numLines.tmp
   call :ParseFile !numLines! < %%f >> result.out
goto :EOF

:ParseFile numLines
set i=0
:SeekStart
   set line=
   set /P line=
   set /A i+=1
   if !i! gtr %1 exit /B
if not "%line%" == "%start%" goto SeekStart
echo %line%
:ShowLines
   set line=
   set /P line=
   set /A i+=1
   if !i! gtr %1 exit /B
   echo(%line%
if not "%line%" == "%end%" goto ShowLines
exit /B

dnishu
Posts: 4
Joined: 27 Jan 2012 10:11

Re: Parsing Text File

#5 Post by dnishu » 06 Feb 2012 08:25

Thanks for the reply but I'm wondering why the codes is not working for me?

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Parsing Text File

#6 Post by Ocalabob » 07 Feb 2012 09:40

Greetings dnishu,

Could you provide an example of the parsed text you are looking to achieve?

Best wishes!

dnishu
Posts: 4
Joined: 27 Jan 2012 10:11

Re: Parsing Text File

#7 Post by dnishu » 09 Feb 2012 08:10

Thanks for taking interest in the post:
The file looks like this.

Data Data Data Data Data Data Data Data Data Data Data
--------------------------------------------------------
Year end total
--------------------------------------------------------
1. Dr. A 45654 66525 6652
2. Dr. B 54545 5545 5545
---------------------------------------------------------
Total 456885 545485 545454
---------------------------------------------------------
data data data data

Now, the out put I'm looking for is starting from "Year end Total" until Total. So the out come would be somthing like this.

Year end total
--------------------------------------------------------
1. Dr. A 45654 66525 6652
2. Dr. B 54545 5545 5545
---------------------------------------------------------
Total 456885 545485 545454


I would appreciate your help in this matter

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

Re: Parsing Text File

#8 Post by Aacini » 09 Feb 2012 12:30

You must note that the requirements of your last example are NOT the same of the original question. In your original question the starting line was "Year end" and the ending line was "Total", but in your new requirements the starting line is "Year end total" and the ending line have several numbers after the "Total" word.

Please note that "Year end" and "Year end total" strings are NOT the same, so if a program must detect anyone of them, it must be specifically written to manage such detail.

The Batch file below compare just the first characters of the line to detect the start and end strings. Note that you must set the number of characters of the start and end text.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "start=Year end"
set startLen=8
set "end=Total"
set endLen=5
if exist result.out del result.out
for %%f in (*.txt) do (
   findstr /N ^^ < %%f | find /C ":" > numLines.tmp
   set /P numLines=< numLines.tmp
   call :ParseFile !numLines! < %%f >> result.out
goto :EOF

:ParseFile numLines
set i=0
:SeekStart
   set line=
   set /P line=
   set /A i+=1
   if !i! gtr %1 exit /B
if /I "!line:~0,%startLen%!" neq "%start%" goto SeekStart
echo %line%
:ShowLines
   set line=
   set /P line=
   set /A i+=1
   if !i! gtr %1 exit /B
   echo(%line%
if /I "!line:~0,%endLen%!" neq "%end%" goto ShowLines
exit /B

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Parsing Text File

#9 Post by Ocalabob » 09 Feb 2012 15:47

Greetings dnishu,

If you have a file with a lot of 'data', as your example suggests
and you simply want to pluck a few lines from that file you
may want to consider using SED.EXE in a batch file to get the
job done.

Code: Select all

::Tested with the example provided in post.
@echo off
TYPE data_file.txt | SED -n "/Year end/,/Total/ p">results_file.txt


Note: SED is not supported in this forum.

Best wishes.

Post Reply