Parsing Text File
Moderator: DosItHelp
Parsing Text File
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
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
Re: Parsing Text File
If you want to process the lines contained in a text file between "Year end" and "Total":
Please note that this Batch file ends at the first empty line in the file. However, this limitation may be avoided if needed.
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.
Re: Parsing Text File
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.
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.
Re: Parsing Text File
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
Re: Parsing Text File
Thanks for the reply but I'm wondering why the codes is not working for me?
Re: Parsing Text File
Greetings dnishu,
Could you provide an example of the parsed text you are looking to achieve?
Best wishes!
Could you provide an example of the parsed text you are looking to achieve?
Best wishes!
Re: Parsing Text File
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
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
Re: Parsing Text File
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.
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
Re: Parsing Text File
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.
Note: SED is not supported in this forum.
Best wishes.
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.