Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Extract data from text file

Posted on 2006-11-07
4
Medium Priority
?
280 Views
Last Modified: 2010-04-16
Hi,

I have the following in a text file:

-------------------------------------------
Number: - 012345
Smith, John
Total: 1

Number: - 012234
Jones, Ben
Total 1

Number: - 012224
Thomas, James
Total 1

etc,etc
------------------------------------------

What I would like is some method of extracing the numbers only into the following format:

012345
012234
012224

etc etc,

Is there a DOS command to do this, for instance is the command FOR capable of doing this?

Any help would be greatly appreciated.

Many thanks

Bootneck2222
0
Comment
Question by:bootneck2222
4 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 17894166
perl -ne "print if s/Number:\D*//" textfile
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 17894174
I can't help with a batch file, but if you want another option, here's a Perl solution.

perl -ne "print $1,$/ if /^Number:\D+(\d+)/" file.txt
0
 
LVL 6

Accepted Solution

by:
dave_moats earned 2000 total points
ID: 17894470
You can do this with DOS commands, the following will parse your example text and give you the results you describe:

Replace [textFile.txt] in the following command with the appropriate file name or path.

If you want to redirect the results to a file then remove the square brackets from around the [ >> outFile.txt ]

for /f "tokens=1,2 delims=:- " %j in ([textFile.txt]) do ( if "%j"=="Number" ( echo %k )) [ >> outFile.txt]

Here is a batch file that will handle it as well:

[START OF CODE ]
@echo off

:: make sure we have something to work with
if "%1"=="" goto SHOWUSE

:: make sure the file exists
if not exist %1 goto SHOWNOFILE


for /f "tokens=1,2 delims=:- " %%j in (%1) do (
   if "%%j"=="Number" (
      echo %%k
   )
)

goto JOBDONE


:: just let the user know the file that was passed in
:: was not found
:SHOWNOFILE
echo Unable to find %1 - please try again with a valid file path


:: tell the user what we are expecting
:SHOWUSE
echo Usage: [name of batch file] path to file to parse


:JOBDONE

[END OF CODE]
Dave
0
 

Author Comment

by:bootneck2222
ID: 17898681
Many thanks for all comments.

Thank you dave_moats, just what I was after.

Excellent

Bootneck2222
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question