?
Solved

Parse text file, trim each line, remove carriage returns, add CRLF after delimiter character?

Posted on 2007-12-04
6
Medium Priority
?
7,669 Views
Last Modified: 2013-11-07
I believe this is a fairly simple question, but unfortunately I have no experience working with text files so I'm asking for help rather than figuring it out myself.

I need to read in a text file (it's going to be 5 megs up to a couple hundred).

I need to do the following with it
1) I need to remove the trailing spaces from each line.
2) Delete all carriage returns and line feeds.
3) Add a Carriage Return/ Line Feed after any instance of "</END??>" where the ?? can be anything.

I believe each line is around 1000 bytes.  Speed doesn't really matter.
I mention the byte count since I think this is a large file and should use something like a string buffer no? Not really familiar with anything other than ordinary strings.

I'll gladly bump up the point value for a quick quality solution.
0
Comment
Question by:Brothernod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20409231
let's assume you read each line into a String object:
       Dim line as String

To remove new lines and CRs, use:
       line = line.Replace(chr(10).ToString(), "")
       line = line.Replace(char(13).ToString(), "")

Then remove trailing spaces
       line = line.TrimEnd(null);

about the </END??> issue, you can do it with an algorithm:
      Dim i as Integer = 0
      Do
             i = line.IndexOf("</END", i)
             If i < 0 Then Exit Do
             i = line.IndexOf(">", i)
             If i < 0 Then Exit Do
             line = line.Insert(i+1, char(13) & chr(10))
      While True
0
 
LVL 19

Expert Comment

by:weellio
ID: 20409421
here ya go
Dim source, target
source = "myfile.txt"
target = "myfile2.txt"
 
Dim fso, f, f2
dim x, y
Const ForReading = 1, ForWriting = 2
 
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(source) Then
   Dim inputLine, outputLine
   Set f = fso.OpenTextFile(source, ForReading, False)
   Set f2 = fso.OpenTextFile(target, ForWriting, True)
   While Not f.AtEndOfStream
      inputLine = f.ReadLine
			outputline = parseme(inputLine)
      f2.Write outputline
   Wend
   f.Close
   f2.Close
   Set f2 = Nothing
   Set f = Nothing
   MsgBox "Done"
Else
   MsgBox source, vbOKOnly, "Source File Not Found"
End If
Set fso = Nothing
 
function parseme(strtext)
' remove trailing lines
	parseme = rtrim(strtext)
 
'remove carriage returns
	parseme = Replace(parseme, vbCr, "")
 
'remove line feeds
	parseme = Replace(parseme, vblf, "")
 
'find "</END??>" and add vbcrlf
	x = instr(parseme, "</END") 'if exists find a location 
'loop to find multiple '</end"'s
	do until x = 0 
			y = instr(x, parseme, ">")
			parseme = left(parseme, y+1) & vbcrlf & mid(parseme, y+1) 'starting on character past the ">"
			x = instr(y, parseme, "</END") 
	loop
end function

Open in new window

0
 
LVL 19

Expert Comment

by:weellio
ID: 20409426
hmm,.. i did a simple vbscript,.. but i'm sure you don't really have to change it to make it work with vb

just name the file something.vbs and edit the 'inputfile and outputfilenames.
or setup as arguments or something.
0
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 
LVL 19

Accepted Solution

by:
weellio earned 1000 total points
ID: 20409435
tiny change
Dim source, target
source = "myfile.txt"
target = "myfile2.txt"
 
Dim fso, f, f2
dim x, y
Const ForReading = 1, ForWriting = 2
 
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(source) Then
   Dim inputLine, outputLine
   Set f = fso.OpenTextFile(source, ForReading, False)
   Set f2 = fso.OpenTextFile(target, ForWriting, True)
   While Not f.AtEndOfStream
      inputLine = f.ReadLine
			outputline = parseme(inputLine)
      f2.Write outputline
   Wend
   f.Close
   f2.Close
   Set f2 = Nothing
   Set f = Nothing
   MsgBox "Done"
Else
   MsgBox source, vbOKOnly, "Source File Not Found"
End If
Set fso = Nothing
 
function parseme(strtext)
' remove trailing lines
	parseme = rtrim(strtext)
 
'remove carriage returns
	parseme = Replace(parseme, vbCr, "")
 
'remove line feeds
	parseme = Replace(parseme, vblf, "")
 
'find "</END??>" and add vbcrlf
	x = instr(parseme, "</END") 'if exists find a location 
'loop to find multiple '</end"'s
	do until x = 0 
			y = instr(x, parseme, ">")
			parseme = left(parseme, y) & vbcrlf & mid(parseme, y+1) 'starting on character past the ">"
			x = instr(y, parseme, "</END") 
	loop
end function

Open in new window

0
 
LVL 1

Author Comment

by:Brothernod
ID: 20411400
I won't lie, I was really hoping for a nice clean vb.net example.

BUT

I needed this quick, and weelio did provide a quick complete solution.

Thank you.
0
 
LVL 1

Author Closing Comment

by:Brothernod
ID: 31412783
It's in VBA not VB.NET but I guess it was close enough since I'm in a hurry :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

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