...

View Full Version : Pearl or BAT? to do this



dahman007
02-16-2005, 02:17 PM
i need to create a BAT file which does that following.

-Take a Text file and replace data in it.
(B_)(E_) = (BEGIN) (END)
EX.
F1 VA 5183 19.55150 81.345 89.24340"B_EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"E_EP"
**THIS IS THE ORIGINAL FILE FORMAT IN A *.TXT**

I NEED IT TO LOOK LIKE THIS:
BEG_EP
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
END

AND THE FOLLOWING: "C3_"

F1 VA 5183 19.55150 81.345 89.24340"B_EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"C3_EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"E_EP"

TO LOOK LIKE THIS
BEGIN
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
C3
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
F1 VA 5183 19.55150 81.345 89.24340"EP"
END

Is this possible?
I want to drop the ascii file into the bat file and then it spits it out with all the changes into another file or the same one.... But there is nothing really with fields... just takeing a B_ or E_ and renaming it to a BEG or END on a new line above or below of where that "B_" is located...

I hope this is not hard...

thanks for the help! really!

Roelf
02-18-2005, 12:57 PM
Is VBScript ok als well?


Dim objFileSys, objInputFile, objOutputFile, objArgs
Dim strInputFileName, strOutputFileName, strInputLine, strOutputLine, strControl
Dim intPosition, intPosition2

'get filenames from commandline arguments
Set oArgs = WScript.Arguments
If oArgs.Count = 2 then
strInputFileName = oArgs(0)
strOutputFileName = oArgs(1)
else
msgbox("Please specify input- and outputfilename in command")
Wscript.quit
end if

Set objFileSys = CreateObject("Scripting.FileSystemObject")
Set objInputFile = objFileSys.OpenTextFile(strInputFileName,1,False)
Set objOutputFile = objFileSys.OpenTextFile(strOutputFileName,2,True)

Do
strInputLine = objInputFile.ReadLine
'check for presence of an underscore
intPosition = InStr(strInputLine, "_")
If intPosition > 0 Then
'get position of doublequote
intPosition2 = InStr(strInputLine, """")
'get the characters between the doublequote and the underscore
strControl = Mid(strInputLine, (intPosition2 + 1) , (intPosition - intPosition2 - 1) )
'create the outputline, consisting of first part and last part concatenated, without the control part in it
strOutputLine = Left(strInputLine, intPosition2) & Right(strInputLine, (Len(strInputLine) - intPosition))
'write the neccesary lines in the oututfile
Select Case strControl
Case "B"
objOutputFile.WriteLine "BEG_EP"
objOutputFile.WriteLine strOutputLine
Case "E"
objOutputFile.WriteLine strOutputLine
objOutputFile.WriteLine "END"
Case Else
objOutputFile.WriteLine strControl
objOutputFile.WriteLine strOutputLine
End Select
Else
'no underscore found, just write the line in outputfile
objOutputFile.WriteLine strInputLine
End If
Loop Until objInputFile.AtEndOfStream

' clean up
set objFileSys = Nothing
objInputFile.Close
set objInputFile = Nothing
objOutputFile.Close
set objOutputFile = Nothing
set objArgs = Nothing



save this code in a vbs-file

call it like filename.vbs inputfile.txt outputfile.txt

bcarl314
02-18-2005, 01:04 PM
Technically, you could do this with just about any language you wanted to, but seeing as how PERL is the Practical Extraction and Reporting Language and was designed for fast, easy text manipulation, it may be a better tool (and faster, and more secure) than VB.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum