Solved

Extract data from text file

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

743 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

13 Experts available now in Live!

Get 1:1 Help Now