PC Review


Reply
Thread Tools Rate Thread

Better way to extract data

 
 
Matt Williamson
Guest
Posts: n/a
 
      15th Dec 2006
Given the following script

setlocal enabledelayedexpansion
if exist file.txt (
for /f "tokens=*" %%a in ('type file.txt^|find "BOF"') do (set dt=%%a)
set dt=!dt: = !&set dt=!dt: = !&set dt=!dt: = !
for /f "tokens=7" %%a in ('echo !dt!') do set dt=%%a
echo !dt!
)

Where after the 3rd line the dt variable contains:

BOF PING GLOBAL ACTIVITY DATA OF 12/13/2006 TO REMOTE 123 BEGINS
HERE 12/13/2006 23:13:38

Is there a better way to extract the Date after DATA OF? Right now, I'm
replacing 2 spaces with 1 space multiple times so I can use the second for
loop to reliably extract the date. I'm just wondering what other ways there
are to do it.

TIA

Matt



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Phil Robyn
Guest
Posts: n/a
 
      15th Dec 2006
Matt Williamson wrote:
> Given the following script
>
> setlocal enabledelayedexpansion
> if exist file.txt (
> for /f "tokens=*" %%a in ('type file.txt^|find "BOF"') do (set dt=%%a)
> set dt=!dt: = !&set dt=!dt: = !&set dt=!dt: = !
> for /f "tokens=7" %%a in ('echo !dt!') do set dt=%%a
> echo !dt!
> )
>
> Where after the 3rd line the dt variable contains:
>
> BOF PING GLOBAL ACTIVITY DATA OF 12/13/2006 TO REMOTE 123 BEGINS
> HERE 12/13/2006 23:13:38
>
> Is there a better way to extract the Date after DATA OF? Right now, I'm
> replacing 2 spaces with 1 space multiple times so I can use the second for
> loop to reliably extract the date. I'm just wondering what other ways there
> are to do it.
>
> TIA
>
> Matt
>
>
>


This works for me:

- - - - - - - begin screen capture WinXP MCE 2005 SP2 - - - - - - -
c:\cmd>type c:\temp\file.txt
sadsdlkj weroiuaglxv sdfkljsdf zlkdflk ertiou asldkfjf rtiou vbnx,nv
BOF PING GLOBAL ACTIVITY DATA OF 12/13/2006 TO REMOTE 123 BEGINS HERE 12/13/2006 23:13:38
xvcm,n weroiu qwe fgklj qweopir topi zxlkdj cvb.,m
c:\cmd>demo\GetBOFdt
[12/13/2006]

c:\cmd>rlist demo\GetBOFdt.cmd
=====begin c:\cmd\demo\GetBOFdt.cmd ====================
1. @echo off
2. for /f "tokens=7" %%a in (
3. 'findstr "BOF" c:\temp\file.txt'
4. ) do set dt=%%a
5. echo [%dt%]
=====end c:\cmd\demo\GetBOFdt.cmd ====================
- - - - - - - end screen capture WinXP MCE 2005 SP2 - - - - - - -

--
Phil Robyn
University of California, Berkeley
 
Reply With Quote
 
Alexander Suhovey
Guest
Posts: n/a
 
      15th Dec 2006
"Matt Williamson" <(E-Mail Removed)> wrote in message
news:4582a7ad$0$15551$(E-Mail Removed)...
> Given the following script
>
> setlocal enabledelayedexpansion
> if exist file.txt (
> for /f "tokens=*" %%a in ('type file.txt^|find "BOF"') do (set dt=%%a)
> set dt=!dt: = !&set dt=!dt: = !&set dt=!dt: = !
> for /f "tokens=7" %%a in ('echo !dt!') do set dt=%%a
> echo !dt!
> )
>
> Where after the 3rd line the dt variable contains:
>
> BOF PING GLOBAL ACTIVITY DATA OF 12/13/2006 TO REMOTE 123 BEGINS
> HERE 12/13/2006 23:13:38
>
> Is there a better way to extract the Date after DATA OF? Right now, I'm
> replacing 2 spaces with 1 space multiple times so I can use the second for
> loop to reliably extract the date. I'm just wondering what other ways
> there are to do it.
>


Matt,

Since several delimiters in a row are threated as a single one, you don't
need any conversion since following should be enough

for /f "tokens=7" %%i in (
'findstr "BOF" file.txt') do (
echo %%i
)

And talking about other ways to do it, you could use slash as delimiter
which can be more appropriate in cases you don't have better clue about
substring position in the string:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2,3 delims=/" %%i in (data.txt) do (
set out=%%i
set out=!out:~-2!/%%j/%%k
set out=!out:~0,10!
echo !out!
)

--
Alexander Suhovey

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract web-data DianePDavies Microsoft Access 3 27th Apr 2010 08:45 PM
Extract matching data from large data file (csv) Utahstew Microsoft Excel Programming 8 14th Apr 2008 03:57 AM
Tying to extract all data from a pivot pulling from external data =?Utf-8?B?VGVkIFVyYmFu?= Microsoft Excel Misc 3 14th Sep 2007 10:50 AM
How to extract the data Pro Microsoft Access 3 14th Jul 2004 12:20 PM
extract data george Microsoft Excel Discussion 1 3rd Jun 2004 05:47 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:20 AM.