We help IT Professionals succeed at work.

Text file containing 8 characters needs to changed to text file containing 4 characters

pogosru
pogosru asked
on
235 Views
Last Modified: 2013-11-27
I have a text file containing a list of 8 characters that needs to be changed to a list of 4 characters. Each 8 character line needs to be split in half and the right half needs to be placed on the following line.

start file example
00010203
04050607
08090a0b
0c0d0e0f
10111213

end file example
0001
0203
0405
0607
0809
0a0b
0c0d
0e0f
1011
1213
test.txt
Comment
Watch Question

The easiest way to do this is by using a program called 'notepad++' and recording a marco, you can find your example textfile split in lines of 4 chars attached.
test.txt

Commented:
if they all have 8, use substring method.  I guess you could place in a hashmap as a key/value pair so you can then print them out.  you could also just place in a stringbuilder in the order that they were separated.



Here is the substring method:

myString.Substring(0, 4)
myString.Substring(4, 8)
margajet24IT Business Analyst

Commented:

            StreamReader rdr = new StreamReader(@"C:\test.txt");
 
            List<string> data = new List<string>();
            string line;
            while (!rdr.EndOfStream)
            {
                line = rdr.ReadLine();
 
                data.Add(line.Substring(0, 4));
            }
 
            rdr.Close();
 
            StreamWriter wrtr = new StreamWriter(@"C:\test2.txt");
 
            foreach (string s in data)
            {
                wrtr.WriteLine(s);
            }
 
            wrtr.Flush();
            wrtr.Close();

Open in new window

Commented:
Imports System
Imports System.IO
Imports System.Collections
                  
Dim arrText As New ArrayList()
Dim separatedText As New ArrayList()
Sub Load()
Dim objReader As New StreamReader("c:\test.txt")  'place your own address in
                              
Dim sLine As String = ""
       ' to store input from files                        

Do
    sLine = objReader.ReadLine()
    If Not sLine Is Nothing Then
        arrText.Add(sLine)
    End If
Loop Until sLine Is Nothing
objReader.Close()
End Sub

Sub splitInput()

    For each string x in ArrText
        Dim Temp As String = (String)x
        Dim firstFour As String = Temp.SubString(0,4)
        Dim secondFour As String = Temp.SubString(4, 4)
       
       arrText.Add(firstFour)
       arrText.Add(secondFour)
   Next
End Sub

then write back to file

                              

Commented:
mara, he's a VB guy

Commented:
oopss
marga, he's a vb guy...excuse typo
margajet24IT Business Analyst

Commented:
yeah.. i know..

got few prob with my IDE

here's the VB version
		Dim rdr as System.IO.StreamReader = new System.IO.StreamReader("C:\test.txt")
 		Dim data as List(Of string) = new List(Of string)
 
        Dim line As String
        
        Do 
        	    line = rdr.ReadLine()
 
                data.Add(line.Substring(0, 4))
        Loop While (Not rdr.EndOfStream) 
 
        rdr.Close()
        
 		Dim wrtr As System.IO.StreamWriter = New System.IO.StreamWriter("C:\test2.txt")
 		
 		dim s as String
 		For Each s In data
 			wrtr.WriteLine(s)	
 		Next
 		
 		
 
        wrtr.Flush()
        wrtr.Close()

Open in new window

IT Business Analyst
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Perfect! Thanks margajet24.

Author

Commented:
Exactlay what I wanted. Thanks!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.