Solved

Extract data from text file

Posted on 2006-11-07
4
276 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 84

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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sp_configure 'external scripts enabled', 1; 7 106
Math Equation 23 122
Assignment from incompatible pointer type? 2 34
Ruby or Python 7 41
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

680 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