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!

Strip text before & after specific characters

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
Hi,

I am trying to figure out how to remove text from a file while stripping out everything else. I have found similar things online but cannot find just what I need. This is what I need to do with something as shown:

Code:
#R FID4;CE;125;-25.400;12.700
#R FID5;CE;125;-25.400;106.680
#R FID6;CE;125;-218.440;12.700

#A BOARD;0.000;0.000;0.000

#M BOARD
#C RE67-0351R1;R3;90.000;-42.545;87.630
#C CR244-15VE;VR4;270.000;-189.865;66.675
#C CR244-15VE;VR7;90.000;-196.850;73.025
#C CR244-15VE;VR8;90.000;-193.040;73.025
#C RE99-1002F;R26;90.000;-184.785;66.675
#C RE99-1000F;R23;270.000;-180.340;73.152
#C RE99-1001F;R27;270.000;-185.420;71.247
#C RE113-011003J;R59;270.000;-114.300;34.925
#C CA237-06-100JA;C62;270.000;-112.395;38.354
#C RE99-1001F;R62;0.000;-111.125;45.085
#C CA266-102K0501N;C64;0.000;-109.220;43.180

So in the first line where #C starts, I need to strip everything in that line but RE67-0351R1. The items I need to remove will always begin with the same format #C and at the end of the item will always be a semicolon. Thanks for any help!
 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is what I am playing with... which strips out everything but the lines with #C.

Code:
' Writing Data to a Text File
Const ForReading = 1
Dim words(1)
Dim msg
words(0) = "#C"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set inFile = objFSO.OpenTextFile("c:\Temp2\partsB.dat", ForReading)
Set outFile = objFSO.OpenTextFile("c:\Temp2\output.txt", 8, True)

Do Until inFile.AtEndOfStream
    strSearchString = inFile.ReadLine
	For i = 0 To UBound(words)-1
	If InStr(strSearchString,words(i)) Then
		msg = msg&strSearchString&vbcrlf
	End If
	next
Loop

inFile.Close
outfile.WriteLine msg

WScript.Echo "Done!"
 
Replace this:
msg = msg&strSearchString&vbcrlf
msg = msg&strSearchString&vbcrlf
with this:
msg = msg & Mid(Split(strSearchString,";")(0),4) & vbCrLf

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV... Just what I need. If you don't mind, can you tell me what this line means? I'm still learning.

Thanks
 
Just for fun, here's a regular expressions solution:

Code:
[blue]    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set infile = objFSO.OpenTextFile("c:\Temp\partsB.dat", 1)
    Set outfile = objFSO.OpenTextFile("c:\Temp\output.txt", 8, True)

    With CreateObject("vbscript.regexp")
        .Global = True
        .MultiLine = True
        .Pattern = "(#C.+?)(;.*?)($)"
        outfile.write .Replace(infile.readall, "$1$3")
    End With[/blue]
 
@Nu2Java

Code:
Mid(Split(strSearchString,";")(0),4)

There's a SPLIT command, which is set to split the string at each ";" it finds in to an array and return the first element ("(0)") of that array to the MID command.

So
Code:
#C RE67-0351R1;R3;90.000;-42.545;87.630
becomes an array containing
Code:
#C RE67-0351R1
R3
90.000
-42.545
87.630

Each line is a numbered from 0 (so the first line is element 0, and the last line is element 4).

Then MID takes that first element returned ("#C RE67-0351R1") and returns the 'middle' of that string starting at the 4th character:
Code:
Mid("#C RE67-0351R1",4)

...and returns the answer:
Code:
RE67-0351R1
:)

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, or photo, or breakfast...and so on)[/small]
 
JPJeffery ... Thanks for the detailed breakdown, Much appreciated. That helps a lot while learning.
 
Hello All

I am back with this code and I am trying to merge two pieces together to Open a file, Strip the file, Open the stripped file and remove the duplicates. I keep getting "Access Denied". I am very SURE I am not putting this together properly. It creates the first output file ok, but then just creates a blank final result file.

Code:
Dim InputFile
Dim outFile

' Writing Data to a Text File
Const ForReading = 1
Dim words(1)
Dim msg

words(0) = "#C"

Set objFSO = CreateObject("Scripting.FileSystemObject")
InputFile = InputBox("Enter the you want to format: ")
Set inFile = objFSO.OpenTextFile("c:\Temp2\" & InputFile & ".txt", ForReading)
'Set outFile = objFSO.OpenTextFile("c:\Temp2\output.txt", 8, True)
Set outFile = objFSO.OpenTextFile("c:\Temp2\" & InputFile & "-Stripped.txt", 8, True)

Do Until inFile.AtEndOfStream
    strSearchString = inFile.ReadLine
	For i = 0 To UBound(words)-1
	If InStr(strSearchString,words(i)) Then
		msg = msg & Mid(Split(strSearchString,";")(0),4) & vbCrLf
	End If
	next
Loop

inFile.Close
outfile.WriteLine msg

'Stripped file complete

'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

'Const ForReading = 1
Const ForWriting = 2

Set objNetwork = CreateObject("Wscript.Network")


strFile1 = "C:\Temp2\" & inputFile & "-Stripped.txt"
strFile2 = "C:\Temp2\FinalResult " & InputFile & ".txt"

If Not objFSO.FileExists(strFile1) Then
	MsgBox "Input File does not exist, Make the text file called" &_
			" input.txt and try again!", vbOKOnly, "Error"
	WScript.Quit
End If


Set objFile = objFSO.OpenTextFile(strFile1, ForReading)


If Not objFSO.FileExists(strFile2) Then
	objFSO.CreateTextFile(strFile2)
End If


result = MsgBox("Are you just removing the blank lines", vbYesNo)

Select Case result

    Case vbYes
    
        Do Until objFile.AtEndOfStream
            strLine = objFile.Readline
            strLine = Trim(strLine)           
                If Len(strLine) > 0 Then
                    If Not LCase(Left(strLine, 7)) = "version" Then
                        strNewContents = strNewContents & strLine & vbCrLf
                    End If
                End If
        Loop
        
    Case vbNo
    
        Do Until objFile.AtEndOfStream
            strLine = objFile.Readline
            strLine = Trim(strLine)
                If Len(strLine) > 0 Then
                    If Not LCase(Left(strLine, 7)) = "version" Then            
                        strNewContents = strNewContents & "Case" & vbTab & Chr(34) & strLine & Chr(34) & vbCrLf
                    End If
                End If
        Loop
        
End Select

objFile.Close

Set objFile = objFSO.OpenTextFile(strFile1, ForWriting)
objFile.Write strNewContents
objFile.Close


Set objOutputFile = objFSO.OpenTextFile(strFile2, ForWriting, True)
Set objFile = objFSO.OpenTextFile(strFile1, ForReading)
Set Dict = CreateObject("Scripting.Dictionary")

Do until objFile.AtEndOfStream
    strCurrentLine = objFile.ReadLine
	If not Dict.Exists(strCurrentLine) then 
		objOutputFile.WriteLine strCurrentLine
		Dict.Add strCurrentLine,strCurrentLine
	End if 
Loop

objOutputFile.Close
 
You can't open an already open file in a different mode. Close the outFile before you open it for reading.

[tt]outFile.close[/tt]

-Geates
 
Code:
inFile.Close
outfile.WriteLine msg
[red]outfile.close[/red]
 
Thanks Geates... I feel good hearing it from a pro. I found that and fixed it and then saw your reply. I hope it means I'm learning a little bit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top