Solved

Extract data from text file

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

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 91
Eclipse Neon start with Admin account only 6 125
Arduino EDI - Programming Language - Voice Recorder 4 81
Regular Expression Calculator Tester 2 72
This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …

770 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