Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract specific line of data from a text or csv file

Status
Not open for further replies.

Strippierfsqefqerfqe

IS-IT--Management
Nov 25, 2009
6
GB
Hiya and thanks in advance to any help...

I have a have a large text file (I could make it into a csv file if need) which contains the numbers 1-125000 and a 3 digit code next to each number

eg

1 ABC
2 CBA
3 ZZZ
.......
124999 ABC
125000 AXD

The file will grows each day from 125000 so needs to be dynamic.

I have tried to write a batch file to extract say the line of data from row 85498 but takes an age (like 1/2 hour for each run through)

I understand a vbs script may be quicker.

I have limited knowledge but am willing to learn, ultimatly I will have a batch file which will call this vbs script and ask it to get the data from a certain line (this will change with each batch file process), the script will then save the data line to a txt file which my batch file will then continue to use as a variable.

I am sure that vbs could do alot of the batch side of things but I need to minimise the vbs script as its outside of my comfort zone (at the moment at least

Thanks again for any assistance, obviously any questions let me know and I'll try to explain better.

Simon



 
See if the following works for you

@echo off
set /a lnum=%1% -1
if %lnum% equ 0 goto find1
FOR /F "skip=%lnum% tokens=*" %%G IN (BIGFILE.TXT) DO echo %%G && GOTO :EOF
:find1
FOR /F "tokens=*" %%G IN (BIGFILE.TXT) DO echo %%G && GOTO :EOF

create a bactch file say linenum.bat with the above contents (changing bigfile.txt to your own filename of course) and call it with the line number as a parameter.

e.g linenum 301


In order to understand recursion, you must first understand recursion.
 
This works well, thank you taupirho. I prefer you method than below as it keeps it all in the same language!!

I also found a solution in vb....(this batch file writes the vb script....

echo Option Explicit >> getbranch.vbs
echo Dim oFSO, sFile, oFile, wFile, sText, savefile >> getbranch.vbs
echo Set oFSO = CreateObject("Scripting.FileSystemObject") >> getbranch.vbs
echo sFile = "branch.txt" >> getbranch.vbs
echo savefile = "found.txt" >> getbranch.vbs
echo set oFile = oFSO.OpenTextFile(sFile, 1) >> getbranch.vbs
echo Do While Not oFile.AtEndOfStream >> getbranch.vbs
echo sText = Trim(ofile.ReadLine) >> getbranch.vbs
echo If InStr(sText, %CLIENT%) ^<^>0 Then >> getbranch.vbs
echo set wFile = oFSO.OpenTextFile(savefile, 8, True) >> getbranch.vbs
echo wFile.writeline(stext) >> getbranch.vbs
echo wfile.close >> getbranch.vbs
echo End If >> getbranch.vbs
echo Loop >> getbranch.vbs
 
i have to admit i previously wrote a vbscript which wrote a vbscript but that was to create a vbscript locally on a machine from a remote machine (seems sill thing to do and infact sounds silly as well) but was needed when one had lost control of an ADS client.

also wrote a vbscript to read a vbscript and find bugs in it

but, in your case, the example you give only serves as a way to avoid the use of passing arguments to your script or having the script consume external data:


Option Explicit
Dim oFSO, sFile, oFile, wFile, sText, savefile, str2Find
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Wscript.Arguments.Count = 1 Then
str2Find = Wscript.Arguments.Item(0)
Else
str2Find = "hmm"
End If
sFile = "branch.txt"
savefile = "found.txt"
set oFile = oFSO.OpenTextFile(sFile, 1)
set wFile = oFSO.OpenTextFile(savefile, 8, True)
Do While Not oFile.AtEndOfStream
sText = ""
sText = Trim(ofile.ReadLine)
If InStr(sText, str2Find) <> 0 Then
wFile.writeline(sText)
End If
Loop
wfile.close
oFile.Close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top