Go Back   TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Batch Script Text file parse

Windows Server Help


Reply
 
Thread Tools Search this Thread
  #1  
Old 24-03-2009
tator.usenet@gmail.com
 
Posts: n/a
Batch Script Text file parse

Newbie here.

I am trying to get a single line of text from each file within a
directory of text files.

Example

I have directory X that contains files:

file1.txt
file2.txt
file3.txt

Each text file is similar in structure, and contains lines like:

Some mumbo jumbo
Uniqe ID: xyzpdq
some other mumbo jumbo
etc, etc, etc.

In all cases, line 2 contains "Unique ID:" and then the unique
identifier text, which is what I need to extract.
In the above case, I need the "xyzpdq". Note that this is what
changes in each test file.

I want to run a script that will parse either this unique ID, or the
entire 2nd line of text (in which case I can just trim it later) -
from all .txt files within the folder.

any help?

Thanks.

Reply With Quote
  #2  
Old 24-03-2009
T Lavedas
 
Posts: n/a
Re: Batch Script Text file parse

On Mar 24, 1:13*pm, tator.use...@gmail.com wrote:
> Newbie here.
>
> I am trying to get a single line of text from each file within a
> directory of text files.
>
> Example
>
> I have directory X that contains files:
>
> file1.txt
> file2.txt
> file3.txt
>
> Each text file is similar in structure, and contains lines like:
>
> Some mumbo jumbo
> Uniqe ID: xyzpdq
> some other mumbo jumbo
> etc, etc, etc.
>
> In all cases, line 2 contains "Unique ID:" and then the unique
> identifier text, which is what I need to extract.
> In the above case, I need the "xyzpdq". *Note that this is what
> changes in each test file.
>
> I want to run a script that will parse either this unique ID, or the
> entire 2nd line of text (in which case I can just trim it later) -
> from all .txt files within the folder.
>
> any help?
>
> Thanks.


How do you want the output stored/presented. Is it necessary to know
what file it comes from? Or are you just after a list of the unique
IDs?

The simplest way to do this is ...

find "Unique ID:" d:\pathspec\*.txt > output.txt

This will create a file with a line of hyphens followed by the file's
name; followed by the matching Unique ID line from that file. This
will repeat for all the .txt files in the named folder.

If you want JUST the unique IDs, with none of the other stuff, try
something like this ...

(for /f "tokens=3" %%a in ('find "Unique ID:" d:\pathspec\*.txt') do
echo.%%a) > output.txt

If you need/want something else, you will need to be more specific
about your requirements/desires.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
Reply With Quote
  #3  
Old 25-03-2009
Pegasus [MVP]
 
Posts: n/a
Re: Batch Script Text file parse


<tator.usenet@gmail.com> wrote in message
news:231436ae-72ed-499e-a100-0447517ea7af@a5g2000pre.googlegroups.com...
> Newbie here.
>
> I am trying to get a single line of text from each file within a
> directory of text files.
>
> Example
>
> I have directory X that contains files:
>
> file1.txt
> file2.txt
> file3.txt
>
> Each text file is similar in structure, and contains lines like:
>
> Some mumbo jumbo
> Uniqe ID: xyzpdq
> some other mumbo jumbo
> etc, etc, etc.
>
> In all cases, line 2 contains "Unique ID:" and then the unique
> identifier text, which is what I need to extract.
> In the above case, I need the "xyzpdq". Note that this is what
> changes in each test file.
>
> I want to run a script that will parse either this unique ID, or the
> entire 2nd line of text (in which case I can just trim it later) -
> from all .txt files within the folder.
>
> any help?
>
> Thanks.


Here is a batch file solution:
@echo off
for %%a in ("d:\temp\*.txt") do call :Sub %%a
goto :eof

:Sub
for /F "skip=1 tokens=3" %%b in ('type "%*"') do (
echo %* %%b & goto :eof
)

Its advantage is that it's simple. It's drawback is that it's slow and that
it will probably trip over so-called "poison characters". If you want
something robust and fast then a VB Script file would be a better solution.
How about having a go at it yourself, then requesting specific advice here
instead of asking for the whole thing to be delivered on a platter?


Reply With Quote
  #4  
Old 25-03-2009
Richard Mueller [MVP]
 
Posts: n/a
Re: Batch Script Text file parse


<tator.usenet@gmail.com> wrote in message
news:231436ae-72ed-499e-a100-0447517ea7af@a5g2000pre.googlegroups.com...
> Newbie here.
>
> I am trying to get a single line of text from each file within a
> directory of text files.
>
> Example
>
> I have directory X that contains files:
>
> file1.txt
> file2.txt
> file3.txt
>
> Each text file is similar in structure, and contains lines like:
>
> Some mumbo jumbo
> Uniqe ID: xyzpdq
> some other mumbo jumbo
> etc, etc, etc.
>
> In all cases, line 2 contains "Unique ID:" and then the unique
> identifier text, which is what I need to extract.
> In the above case, I need the "xyzpdq". Note that this is what
> changes in each test file.
>
> I want to run a script that will parse either this unique ID, or the
> entire 2nd line of text (in which case I can just trim it later) -
> from all .txt files within the folder.
>
> any help?
>
> Thanks.


Someone else can supply a batch file solution. Here is a VBScript solution:
==========
Option Explicit

Dim strFolder, objFSO, objFolder, objItem, objFile, strLine, strSearch,
strID

Const ForReading = 1

' Specify the directory.
strFolder = "c:\scripts"

' Specify the string to search for.
' Make all lower case for comparison (not spelling).
strSearch = "unique id:"

' Bind to the folder object.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)

' Enumerate all files in the folder.
For Each objItem in objFolder.Files
' Open the file with a textstream object.
Set objFile = objFSO.OpenTextFile(objItem.Path, ForReading)
' Read each line of the file.
Do Until objFile.AtEndOfStream
strLine = Trim(objFile.ReadLine)
' Check for ID search string.
If (InStr(LCase(strLine), strSearch) = 1) Then
' The ID is assumed to follow the first ":" in the line.
strID = Trim(Mid(strLine, InStr(strLine, ":") + 1))
Wscript.Echo strID
' No need to read any more of the file.
Exit Do
End If
Loop
' Close the file.
objFile.Close
Next

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


Reply With Quote
  #5  
Old 25-03-2009
tator.usenet@gmail.com
 
Posts: n/a
Re: Batch Script Text file parse

On Mar 24, 10:24*am, T Lavedas <tglba...@cox.net> wrote:
> On Mar 24, 1:13*pm, tator.use...@gmail.com wrote:
>
>
>
>
>
> > Newbie here.

>
> > I am trying to get a single line of text from each file within a
> > directory of text files.

>
> > Example

>
> > I have directory X that contains files:

>
> > file1.txt
> > file2.txt
> > file3.txt

>
> > Each text file is similar in structure, and contains lines like:

>
> > Some mumbo jumbo
> > Uniqe ID: xyzpdq
> > some other mumbo jumbo
> > etc, etc, etc.

>
> > In all cases, line 2 contains "Unique ID:" and then the unique
> > identifier text, which is what I need to extract.
> > In the above case, I need the "xyzpdq". *Note that this is what
> > changes in each test file.

>
> > I want to run a script that will parse either this unique ID, or the
> > entire 2nd line of text (in which case I can just trim it later) -
> > from all .txt files within the folder.

>
> > any help?

>
> > Thanks.

>
> How do you want the output stored/presented. *Is it necessary to know
> what file it comes from? *Or are you just after a list of the unique
> IDs?
>
> The simplest way to do this is ...
>
> * find "Unique ID:" d:\pathspec\*.txt > output.txt
>
> This will create a file with a line of hyphens followed by the file's
> name; followed by the matching Unique ID line from that file. *This
> will repeat for all the .txt files in the named folder.
>
> If you want JUST the unique IDs, with none of the other stuff, try
> something like this ...
>
> * (for /f "tokens=3" %%a in ('find "Unique ID:" d:\pathspec\*.txt') do
> echo.%%a) > output.txt
>
> If you need/want something else, you will need to be more specific
> about your requirements/desires.
>
> Tom Lavedas
> ***********http://there.is.no.more/tglbatch/- Hide quoted text -
>
> - Show quoted text -


Perfect! - ... almost - any way to get the info on the same line so I
can easily open it in two columns in excel (e.g. - column 1 = file
name, column 2 = unique ID? I'm sure I can manage with excel, but
would be easier to not have to deal with it.

In any case, very much appreciate the help!

Reply With Quote
  #6  
Old 25-03-2009
T Lavedas
 
Posts: n/a
Re: Batch Script Text file parse

On Mar 24, 1:55*pm, tator.use...@gmail.com wrote:
> On Mar 24, 10:24*am, T Lavedas <tglba...@cox.net> wrote:
>
>
>
> > On Mar 24, 1:13*pm, tator.use...@gmail.com wrote:

>
> > > Newbie here.

>
> > > I am trying to get a single line of text from each file within a
> > > directory of text files.

>
> > > Example

>
> > > I have directory X that contains files:

>
> > > file1.txt
> > > file2.txt
> > > file3.txt

>
> > > Each text file is similar in structure, and contains lines like:

>
> > > Some mumbo jumbo
> > > Uniqe ID: xyzpdq
> > > some other mumbo jumbo
> > > etc, etc, etc.

>
> > > In all cases, line 2 contains "Unique ID:" and then the unique
> > > identifier text, which is what I need to extract.
> > > In the above case, I need the "xyzpdq". *Note that this is what
> > > changes in each test file.

>
> > > I want to run a script that will parse either this unique ID, or the
> > > entire 2nd line of text (in which case I can just trim it later) -
> > > from all .txt files within the folder.

>
> > > any help?

>
> > > Thanks.

>
> > How do you want the output stored/presented. *Is it necessary to know
> > what file it comes from? *Or are you just after a list of the unique
> > IDs?

>
> > The simplest way to do this is ...

>
> > * find "Unique ID:" d:\pathspec\*.txt > output.txt

>
> > This will create a file with a line of hyphens followed by the file's
> > name; followed by the matching Unique ID line from that file. *This
> > will repeat for all the .txt files in the named folder.

>
> > If you want JUST the unique IDs, with none of the other stuff, try
> > something like this ...

>
> > * (for /f "tokens=3" %%a in ('find "Unique ID:" d:\pathspec\*.txt')do
> > echo.%%a) > output.txt

>
> > If you need/want something else, you will need to be more specific
> > about your requirements/desires.

>
> > Tom Lavedas
> > ***********http://there.is.no.more/tglbatch/-Hide quoted text -

>
> > - Show quoted text -

>
> Perfect! - ... almost - any way to get the info on the same line so I
> can easily open it in two columns in excel (e.g. - column 1 = file
> name, column 2 = unique ID? *I'm sure I can manage with excel, but
> would be easier to not have to deal with it.
>
> In any case, very much appreciate the help!


OK, now I know what your really wanted. Maybe something like this
will serve ...

echo."Header 1","Header 2" > output.csv
( for %%a in (d:\pathspec\*.txt) do (
for /f "tokens=3" %%B in ('find "Unique ID:" ^< %%a') do (
echo."%%a",%%B)
)
) >> output.csv
start "" output.csv

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
Reply With Quote
Reply

  TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Batch Script Text file parse"
Thread Thread Starter Forum Replies Last Post
batch script to find a file stevenwhite Software Development 1 10-04-2012 01:55 PM
code for using a batch file to parse html (.htm) files and export values in .csv format jamesnovello Software Development 1 19-03-2011 02:28 AM
Does java parse large text file Zeverto Software Development 3 30-07-2009 02:26 PM
Powershell script to parse system logs in text file SADIQ Operating Systems 2 02-06-2009 08:05 PM
Batch Script to parse lines in text file jntoner1@gmail.com Windows Server Help 8 28-01-2009 04:38 AM


All times are GMT +5.5. The time now is 02:14 AM.