• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

Text file cleaning

Hello

I have a text file with 800 entries and I am trying to find a solution to clean it a little bit.
The contents are split into "Records" ( record 1, 2 , 3 , etc ).
What I am trying to do is eliminate the empty records from the file.
I am attaching a sample of the file and some other explanation.
I can code in C# .NET , CMD ( BATCH FILE ) , visual basic script, etc

Thank you in advance

RECORD 00001
2000/01/01 00:17:32
PAR 1: 1 
PAR 6:     
PAR 2:   
PAR 7:     
PAR 3:   
PAR 8:     
PAR 4:   
PAR 9:     
PAR 5:   
PAR 10:      
-----------------------
RECORD 00002
2000/01/01 00:17:32
PAR 1: 1 
PAR 6:     
PAR 2: 34242342342  
PAR 7:     
PAR 3:   
PAR 8: 55535328883    
PAR 4:   
PAR 9:     
PAR 5:   
PAR 10:      
-----------------------
RECORD 00003
2000/01/01 00:17:32
PAR 1:  
PAR 6:    
PAR 2:   
PAR 7:      
PAR 3:   
PAR 8:      
PAR 4:   
PAR 9:     
PAR 5:   
PAR 10:     
-----------------------



/// In this file, i need to eliminate both Record 00001 and 00003, because they contain invalid information.
/// I have calculated the total number of characters that constitute an empty record : 98
/// I want to set a rule in the script, to delete the record if it has less then 100 characters...
/// For example Record 00001 has 99 characters, therefore will be deleted.
/// Thank you

Open in new window

0
giberisk
Asked:
giberisk
  • 3
1 Solution
 
wdosanjosCommented:
Please try the following:

var istream = new StreamReader(new FileStream(@"c:\temp\infile.txt", FileMode.Open));
var ostream = new StreamWriter(new FileStream(@"c:\temp\outfile.txt", FileMode.Create));
StringBuilder sb = new StringBuilder();
int size = 0;

for (string line = istream.ReadLine(); line != null; line = istream.ReadLine())
{
	line = line.Trim();

	sb.Append(line).Append("\r\n");
	
	if (line != "-----------------------")
	{
		size += line.Length;
	}
	else
	{	// end of record
		if (size >= 100)
		{
			ostream.Write(sb.ToString());
		}

		sb.Length = 0;
		size = 0;
	}
}

istream.Close();
ostream.Close();

Open in new window

0
 
giberiskAuthor Commented:
What language is this please ?
0
 
giberiskAuthor Commented:
Nevermind I got it working...
Thank you so much
if you care for my other question I will be very greatfull !
Thanks again

http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_26936272.html
0
 
giberiskAuthor Commented:
Super
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now