Solved Batch file to extract certain text from a txt file

June 2, 2012 at 17:42:47
Specs: Windows 7
Hi,

I am trying to write an automatic batch file that will extract 3 lines after a certain string.

For example I have the a text file with the following information in it (this is just a portion of the text file),

Forecast for the rest of Sunday 3 June
Cloudy. Patchy morning fog. Isolated showers becoming scattered this afternoon.
Winds southeasterly and light tending east to southeasterly up to 25 km/h
during the morning.

City Centre A few showers. Max 15
Chance of any rainfall: 70%
Rainfall: 2 to 4 mm

Around Melbourne
Geelong Max 15 Watsonia Max 14
Laverton Max 15 Mount Dandenong Max 10
Tullamarine Max 14 Yarra Glen Max 14
Scoresby Max 14 Frankston Max 14

No UV Alert, UV Index predicted to reach 2 [Low]

Forecast for Monday 4 June
Cloudy. Scattered showers. Winds south to southeasterly up to 35 km/h.

City Centre A few showers. Min 10 Max 14

Forecast for Tuesday 5 June
Partly cloudy. Isolated showers clearing during the afternoon. Winds south to
southeasterly up to 30 km/h.

City Centre Shower or two clearing. Min 10 Max 14

I want to run a batch file that will extract all information between the first 'City Centre' and 'Around Melbourne' into a new txt file. So in this example the new text file should read,

A few showers. Max 15
Chance of any rainfall: 70%
Rainfall: 2 to 4 mm

The problem is 'City Centre' appears multiple times in the original text file, I only want to grab information from the first 'City Centre' and end at 'Around Melbourne'.

Any help would be much appreciated.


See More: Batch file to extract certain text from a txt file

Report •


✔ Best Answer
June 3, 2012 at 05:10:21
:: ===== script starts here ===============
::
:: che.bat Sun 03-06-2012 13:13:54.10
@echo off > newfile & setLocal enableDELAYedeXpansioN

set C=
set A=

for /f "tokens=1* delims=[]" %%a in ('find /v /n "\\\\\\" ^< weather') do (
echo.%%b|find "City Centre" > nul && if not defined C set C=%%a
echo.%%b|find "Around Melbourne" > nul && if not defined A set A=%%a
)

for /f "tokens=1* delims=[]" %%a in ('find /v /n "\\\\\\" ^< weather') do (
if %%a equ !C! (
set S=%%b
set S=!S:City Centre=!
>> newfile echo.!S!
)
if %%a gtr !C! if %%a lss !A! >> newfile echo.%%b
)
goto :eof

::====== script ends here =================


=====================================
Life is too important to be taken seriously.

M2



#1
June 3, 2012 at 00:07:54
:: ===== script starts here ===============
::
:: che.bat Sun 03-06-2012 13:13:54.10
@echo off > newfile & setLocal enableDELAYedeXpansioN

set C=
set A=

for /f "tokens=1* delims=[]" %%a in ('find /v /n "\\\\\\" ^< weather') do (
echo.%%b|find "City Centre" > nul && if not defined C set C=%%a
echo.%%b|find "Around Melbourne" > nul && if not defined A set A=%%a
)

for /f "tokens=1* delims=[]" %%a in ('find /v /n "\\\\\\" ^< weather') do (
if %%a gtr !C! if %%a lss !A! >> newfile echo.%%b
)
goto :eof

::====== script ends here =================


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
June 3, 2012 at 03:11:56
Thanks for the script - I just got it working but it is missing the first line.

It only returns this,

Chance of any rainfall: 70%
Rainfall: 2 to 4 mm

I am missing "A few showers. Max 15"

I think the reason might be because there are a number of spaces in the first line. The first line reads,

City Centre A few showers. Max 15

I want everything after 'City Centre', the above doesn't really show it, but in the original text file there are a number of blank spaces between the end of 'Centre' and the start of 'A few ...' (about 9 spaces) - there is then again a number of blank spaces between '...showers' and 'Max ...' (about 17 spaces). They all appear on the same line. Is this causing the issue?

Ideally I would like the text file to return the first line without the blank spaces eg,
A few showers. Max 15

Any ideas??


Report •

#3
June 3, 2012 at 05:02:38
I don't think it has anything to do with blank spaces, I think it's because it's all on the same line.

It is searching for the line that begins with 'City Centre' and bringing back everything AFTER that line.

How do I get it to search for "City Centre" only, and bring back everything after that (including what is after 'City Centre' on the same line?

Likewise if the original text files changes, how do i get it stop at a certain word, rather than seraching for the entire line contents (that might change each day).

As an example, at the moment it stops at 'Around Melbourne' - which is the only contents on that line. What if in the future this same line has included a date (which will change each day), therefore I want it stop when it finds 'Around Melbourne' even if there is something else (which is changing each day eg date) on the same line?

I hope the above makes some sense!


Report •

Related Solutions

#4
June 3, 2012 at 05:10:21
✔ Best Answer
:: ===== script starts here ===============
::
:: che.bat Sun 03-06-2012 13:13:54.10
@echo off > newfile & setLocal enableDELAYedeXpansioN

set C=
set A=

for /f "tokens=1* delims=[]" %%a in ('find /v /n "\\\\\\" ^< weather') do (
echo.%%b|find "City Centre" > nul && if not defined C set C=%%a
echo.%%b|find "Around Melbourne" > nul && if not defined A set A=%%a
)

for /f "tokens=1* delims=[]" %%a in ('find /v /n "\\\\\\" ^< weather') do (
if %%a equ !C! (
set S=%%b
set S=!S:City Centre=!
>> newfile echo.!S!
)
if %%a gtr !C! if %%a lss !A! >> newfile echo.%%b
)
goto :eof

::====== script ends here =================


=====================================
Life is too important to be taken seriously.

M2


Report •

#5
June 3, 2012 at 05:30:11
Excellent - that did the trick! It returned the following,

A few showers. Max 15
Chance of any rainfall: 70%
Rainfall: 2 to 4 mm

Just as I wanted - thank you very much!

One last thing, is there any chance you could change it so the end is similar and searches for a certain word (or words) rather than a complete line. As mentioned above at the moment 'Around Melbourne" is currently the only words on that line, but that might change in the future with the inclusion of a day/date (which will change each day)

As an example, the line might read...

'Around Melbourne on Sunday 3rd June'

I want it to stop (and do not include) at 'Around Melbourne'

Is that doable??


Report •

#6
June 3, 2012 at 19:08:59
I'm not with you.


=====================================
Life is too important to be taken seriously.

M2


Report •

#7
June 3, 2012 at 20:26:33
Ok I will try to explain as best I can.

At the moment you script stops when it finds the line 'Around Melbourne'. Currently in the original text file there is nothing else on that line and it is an exact match so it works fine.

What if in the future this line in the original text file changes to,

'Around Melbourne on Sunday 3rd June'

(added 'on Sunday 3rd June' to the line)

If this happens, I don't think your original script will work (because I think it will try to find a line with an exact match for 'Around Melbourne' and it will no longer exist).

Therefore can you change the code to end at just the words 'Around Melbourne' irrespective of what else might be on that line?

Does this help?


Report •

#8
June 3, 2012 at 21:07:52
Try it and see. I think it will fly.


=====================================
Life is too important to be taken seriously.

M2


Report •

#9
June 4, 2012 at 04:37:54
You were right - I tried it and it works.

I inserted some extra words after 'Around Melbourne' in the original text file and it still worked.

Thanks, I really appreciate it!

One more question (and I promise this the last!) - I have one other text file that is in a slightly different format. Below is an extract of the text file,

Forecast for the rest of Monday 4 June
Cloudy. Areas of rain. Winds south to southeasterly up to 50 km/h.

City Centre Rain at times. Windy.
Chance of any rainfall: 60%

Forecast for Tuesday 5 June
Cloudy. Isolated showers until afternoon. Winds south to southeasterly up to 40
km/h decreasing to 25 km/h by early evening.

As before I want to extract the following,

Rain at times. Windy.
Chance of any rainfall: 60%

So start after 'City Centre' and finish at 'Forecast for'.

From you original script I thought I could just replace 'Around Melbourne' for 'Forecast for', but it doesn't seem to work. It only returns the following,

Rain at times. Windy.

Any ideas why or how I can fix this??


Report •

#10
June 4, 2012 at 08:14:45
:: =====  script starts here  ===============
:: 
:: che.bat  Sun 03-06-2012 13:13:54.10
@echo off > newfile & setLocal enableDELAYedeXpansioN

set C=
set F=

for /f "tokens=1* delims=[] " %%a in ('find /v /n "\\\\\\" ^< weather3') do (
  echo.%%b|find "City Centre"  > nul      && if not defined C set C=%%a
  echo.%%b|find "Forecast for" > nul      &&                  set F=%%a
)

for /f "tokens=1* delims=[] " %%a in ('find /v /n "\\\\\\" ^< weather3') do (
  if %%a equ !C! (
    set S=%%b
    set S=!S:City Centre=!
    >> newfile echo.!S!
  )
if %%a gtr !C! if %%a lss !F! >> newfile echo.%%b
)
goto :eof

::======  script ends here  =================


=====================================
Life is too important to be taken seriously.

M2


Report •

#11
June 5, 2012 at 03:37:08
Thanks - almost there.

In the original text file there are a number of occurances of 'Forecast for' (before and after the section that I want). The script above stops at the last occurance of 'Forecast for', therefore giving me too much.

I need it to stop at the first occurance of 'Forecast for' after 'City Centre'

Is that doable?


Report •

#12
June 5, 2012 at 03:57:21
Post a fair chunk. Say, 30 lines.


=====================================
Life is too important to be taken seriously.

M2


Report •

#13
June 5, 2012 at 04:16:33
Here is an extract from the original text file, (I want to capture just the bit from the first 'City Centre' to the first 'Forecast for' - should read,
Shower or two.
Chance of any rainfall: 30%

Text extract below:

Warning Summary at issue time
Minor Flood Warning for the Bunyip River and Western Port Catchment at Iona and
Cora Lynn.
Flood Watch for Greater Melbourne Catchments (Werribee, Maribyrnong, Yarra,
Dandenong and Bunyip).
Details of warnings are available on the Bureau's website www.bom.gov.au, by
telephone 1300-659-217* or through some TV and radio broadcasts.

Forecast for the rest of Tuesday 5 June
Partly cloudy. Isolated showers. Winds south to southeasterly up to 25 km/h.

City Centre Shower or two.
Chance of any rainfall: 30%

Forecast for Wednesday 6 June
Partly cloudy. Isolated showers. Winds south to southeasterly up to 20 km/h
tending southwesterly and light later in the evening.

City Centre Shower or two. Min 8 Max 14
Chance of any rainfall: 40%
Rainfall: 0 to 1 mm

Around Melbourne
Geelong Min 7 Max 14 Watsonia Min 6 Max 13
Laverton Min 6 Max 13 Mount Dandenong Min 5 Max 8
Tullamarine Min 5 Max 12 Yarra Glen Min 4 Max 13
Scoresby Min 6 Max 13 Frankston Min 9 Max 13

No UV Alert, UV Index predicted to reach 2 [Low]

Forecast for Thursday 7 June
Cloudy. Patches of light frost in the northeast early in the morning. Areas of
fog early in the morning. Isolated showers in the morning and afternoon. Winds
south to southwesterly up to 20 km/h.

City Centre Shower or two clearing. Min 8 Max 14

Forecast for Friday 8 June
Cloudy periods with isolated showers. Winds west to southwesterly and light
increasing to 15 to 20 km/h during the morning then becoming light during the
afternoon.

City Centre Shower or two. Min 9 Max 14

Forecast for Saturday 9 June
Morning fog. A partly cloudy day with isolated showers, clearing in the
afternoon. Light winds becoming southwesterly 15 to 20 km/h during the day.

City Centre Shower or two clearing Min 8 Max 14

Forecast for Sunday 10 June
Partly cloudy. Light winds.

City Centre Partly cloudy. Min 7 Max 15

Forecast for Monday 11 June
Partly cloudy. Winds north to northeasterly and light.

City Centre Partly cloudy. Min 5 Max 16

Forecast for Tuesday 12 June
Partly cloudy. Winds north to northeasterly 15 to 20 km/h, increasing to 25 to
30 km/h during the morning.

City Centre Partly cloudy. Min 4 Max 16

The next routine forecast will be issued at 5:00 am EST Wednesday.


Report •


Ask Question