Solved

Cutting the first three characters of each line

Posted on 2011-02-21
8
345 Views
Last Modified: 2012-06-21
Hi Experts,

I have a text file that includes different lines and an each line there is some elements divided by a comma (,):
ABC1235,ASD45,A2R45546=4
V5R48654=,TR4548
R58sgd%$,YT841d4,ABC127

What I want is that, I would to load that text file and then cut each element after the third character. So, the final result should be something like this:
ABC,ASD,A2R
V5R,TR4
R58,YT8,ABC

As you can see the above result is cutting each element (the elements divided by comma ,) and then rewrite the output data to the same file.
Thanks in advance
Regards
0
Comment
Question by:Sat80
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 34943651
Several steps to perform:
1. Read the file line by line
2. use the string.substring-method on that line http://msdn.microsoft.com/en-us/library/aka44szs.aspx
3. save everything back into the file


Dim sFileName As String
Dim srFileReader As System.IO.StreamReader
Dim sInputLine As String

dim sOutput as String

sFileName = "test.txt"
srFileReader = System.IO.File.OpenText(sFileName)
sInputLine = srFileReader.ReadLine()
Do Until sInputLine is Nothing
    sOutput += sInputLine.Substring(3, sInputLine.Length() - 3)
    sInputLine = srFileReader.ReadLine()
Loop

Dim objWriter As New System.IO.StreamWriter(sFileName)
objWriter.Write(sOutput)
'Close objectwriter:
objWriter.Close()

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
ID: 34943672
Try this:
Public Sub RewriteFile(ByVal filename As String)
	Dim temp As String = System.IO.Path.GetTempFileName()

	Using reader As New System.IO.StreamReader(filename)
		Using writer As New System.IO.StreamWriter(temp)
			While Not reader.EndOfStream
				Dim line As String = reader.ReadLine()
				Dim parts() As String = line.Split(","c)

				For i As Integer = 0 To parts.Length - 1
					parts(i) = parts(i).Substring(0, 3)
				Next

				writer.WriteLine(String.Join(",", parts))
			End While
		End Using
	End Using

	System.IO.File.Move(temp, filename)
End Sub

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 34943677
Sorry misread, I understood the string-requirement.
You need to adjust this:
Do Until sInputLine is Nothing
    sOutput += sInputLine.Substring(3, sInputLine.Length() - 3)
    sInputLine = srFileReader.ReadLine()
Loop



into

dim sInput as string()
Do Until sInputLine is Nothing
    sInput = sinputLine.split(',')
    For i = 0 To UBound(aryTextFile)
         sInput(i) = sInput(i).Substring(3, sInput(i)Length() - 3)
     Next i

    sOutput += String.Join(",", sInput)
    sInputLine = srFileReader.ReadLine()
Loop

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 200 total points
ID: 34943683
Still made a fault
sInput(i) = sInput(i).Substring(3, sInput(i)Length() - 3)
must be
sInput(i) = sInput(i).Substring(0, 3)

Dim sFileName As String
Dim srFileReader As System.IO.StreamReader
Dim sInputLine As String

dim sOutput as String

sFileName = "test.txt"
srFileReader = System.IO.File.OpenText(sFileName)
sInputLine = srFileReader.ReadLine()
dim sInput as string()
Do Until sInputLine is Nothing
    sInput = sinputLine.split(',')
    For i = 0 To UBound(aryTextFile)
         sInput(i) = sInput(i).Substring(0, 3)
     Next i

    sOutput += String.Join(",", sInput)
    sInputLine = srFileReader.ReadLine()
Loop


Dim objWriter As New System.IO.StreamWriter(sFileName)
objWriter.Write(sOutput)
'Close objectwriter:
objWriter.Close()

Open in new window

0
 
LVL 9

Assisted Solution

by:Hawkvalley1
Hawkvalley1 earned 100 total points
ID: 34943731
Dim newList As New List(Of String)
Using sr As New StreamReader(<path>)
 While Not sr.EndOfStream
  Dim str() As String = sr.Readline.Split(","c)
  Dim line As String
  For Each s As String In str
      line &= s.Remove(3) & ","
  Next
  line = line.TrimEnd(","c)
 newLine.Add(line)
 End While
End Using
 
0
 

Author Closing Comment

by:Sat80
ID: 34943794
Thank you all
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34944043
NP. Glad to help  : )
0
 
LVL 9

Expert Comment

by:Hawkvalley1
ID: 34948489
That was fun, how many ways???
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

773 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