Solved

Extract data from text file

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 80
detecting  the added row index in a datagridview 3 59
Modify a small python script 19 96
iframe detection of parent window scale 20 60
A short article about a problem I had getting the GPS LocationListener working.
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now