File Manipulation

Hi guys, I hope you are well and can help.

I have a text file (Original.txt) that I wish to manipulate.


============================================ Original.txt

File: Complex-Lecture 1 - annotated.pdf
Annotation summary:
--- Page 10 ---
common disorders
Cataracts
Glaucoma
Macular degeneration
Conjunctivitis/iritis
Foreign body
--- Page 15 ---
Cataract
clouding
lens
vision impairment
--- Page 16 ---
Blurred
Glare
Distortion
veil
signs
--- Page 17 ---
Treatments
glasses
bifocals
magnification
lighting
surgery
--- Page 18 ---
surgery
removal
lens
replacement
implantable lens

-------------------
So, the format is pretty much, that it has annotations taken from each page of a pdf.


-----------------------------------
The result I am seeking is as seen below,in Processed.txt

============================================ Processed.txt
File: Complex-Lecture 1 - annotated.pdf
Annotation summary:

Page 10: common disorders,Cataracts,Glaucoma,Macular degeneration,Conjunctivitis/iritis,Foreign body
Page 15: Cataract,clouding,lens,vision impairment
Page 16: Blurred,Glare,Distortion,veil,signs
Page 17: Treatments,glasses,bifocals,magnification,lighting,surgery
Page 18: surgery,removal,lens,replacement,implantable lens

------------------------------------------------

Basically, Im trying to get all lines that "belong" to a Page, to be on the same line, as that Page.
So, for example, if there are 5 lines directly after Page 15, but before Page 16, then those 5 lines "belong" to Page 15, and as such, I want them placed on the same line as Page 15, with each of those lines, separated by a comma.

I hope this makes sense.

Any help greatly appreciated.
annotations.jpg
LVL 1
Simon336697Asked:
Who is Participating?
 
oBdACommented:
How about this:
@echo off
setlocal enabledelayedexpansion
set InFile=D:\Temp\Original.txt
set OutFile=D:\Temp\Processed.txt
if exist "%OutFile%" del "%OutFile%"
set /a i = 0
for /f "delims=" %%a in ('type "%InFile%"') do (
	set /a i += 1
	echo %%a
	>>"%OutFile%" echo %%a
	if !i!==2 goto HeaderEnd
)
:HeaderEnd
echo.
>>"%OutFile%" echo.
set Page=
set Annotations=
for /f "skip=2 delims=" %%a in ('type "%InFile%"') do (
	set Line=%%a
	if "!Line:~0,9!"=="--- Page " (
		if defined Page (
			echo !Page!: !Annotations:~2!
			>>"%OutFile%" echo !Page!: !Annotations:~2!
			set Annotations=
		)
		set Page=!Line:~4!
		set Page=!Page:~0,-4!
	) else (
		set Annotations=!Annotations!, %%a
	)
)
echo !Page!: !Annotations:~2!
>>"%OutFile%" echo !Page!: !Annotations:~2!

Open in new window

0
 
Simon336697Author Commented:
Hi oBdA,

That script is amazing. Thank you so much.

Hi aikimark,

Thanks to you too.

I am happy with oBdAs.

Thanks to you both :>)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.