If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > tracing new lines and remove, how?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-17-11, 17:49
neo_phyte neo_phyte is offline
Registered User
 
Join Date: Feb 2011
Posts: 12
Post tracing new lines and remove, how?

I have this text file containing this.

Code:
cmg_number|cmg_name|cmg_description
1|Self Management|
2|Relationship Management|

3|Communication|
I want to remove the new lines after #2.. so it should be something like this..

Code:
cmg_number|cmg_name|cmg_description
1|Self Management|
2|Relationship Management|
3|Communication|
How will I do that?
Reply With Quote
  #2 (permalink)  
Old 03-20-11, 16:00
rokslide rokslide is offline
Registered User
 
Join Date: Nov 2003
Location: Christchurch, New Zealand
Posts: 1,617
So essentially you want a script to read through a file line by line and write it back out again with any blank lines being removed.

I assume you want to ignore spaces and tabs....

Code:
Dim FSOInput
Set FSOInput= Server.CreateObject("Scripting.FileSystemObject") 
Dim FSOOutput
Set FSOOutput= Server.CreateObject("Scripting.FileSystemObject") 
Dim inputTextStream
Set inputTextStream = objFSO.OpenTextFile("C:\SomeFile.txt", 1)  
Dim outputTextStream
Set outputTextStream = objFSO.OpenTextFile("C:\SomeFile_output.txt", 2, true)  
Do While inputTextStream .AtEndOfStream <> true
    contents = inputTextStream .ReadLine
    if(trim(contents) != chr(10)+chr(13) then
        outputTestStream.WriteLine(contents)
    end if
loop
outputTextStream.close
inputTextStream.close
The above should be pretty close, it's completely untested but should get you close enuf to figure the rest out for yourself.... the chr(10) + chr(13) is the end of line, carriage return characters and may need to be the other way around.

Last edited by rokslide; 03-20-11 at 16:29.
Reply With Quote
  #3 (permalink)  
Old 03-20-11, 20:32
neo_phyte neo_phyte is offline
Registered User
 
Join Date: Feb 2011
Posts: 12
Post

What if I have this case:

Code:
cmg_number|cmg_name|cmg_description
1|Self Management|
2|Relationship Management|

3|Communication|

cmp_number|cmp_competency|cmp_shortText|cmp_description
1|Self-Development||Test.
2|Adaptability/Stress Tolerance||Cool.
3|Self-Control||Thanks.

4|Trustworthiness||Living.


And should be changed to this:

Code:
cmg_number|cmg_name|cmg_description
1|Self Management|
2|Relationship Management|
3|Communication|

cmp_number|cmp_competency|cmp_shortText|cmp_description
1|Self-Development||Test.
2|Adaptability/Stress Tolerance||Cool.
3|Self-Control||Thanks.
4|Trustworthiness||Living.
Rule: Between 3|Communication| and cmp_number|cmp_competency|cmp_shortText|cmp_descri ption, there should be a NEW LINE to separate new header

Actually I have this code to check if the line has string or not, if not then I have to remove that new lines.

PHP Code:
'open the file for verifying
dim fso, fsoIOMode, fsoCreate, fsoFormat, fsoTextStream, line
set fso = server.createobject("scripting.fileSystemObject")
fsoIOMode = 1        '
for reading only
fsoCreate 
false    'do not create a new file
fsoFormat = -1        '
unicode
set fsoTextStream 
fso.openTextFile(file.pathfsoIOModefsoCreatefsoFormat)

do while 
not fsoTextStream.atEndOfStream
    line 
fsoTextStream.readLine
    
    
'check if the line has no string
    if line <> "" then
        response.write fsoTextStream.line & ": " & line & "<br />"
    else
        response.write fsoTextStream.line & ": " & "none" & "<br />"
    end if
loop 
Now my problem is to check between the header and the last string from above, that there should one new line of that.

Let me know if how will I do that... Yeah, your solution is really great, I will follow it.


Thanks
Reply With Quote
  #4 (permalink)  
Old 03-20-11, 21:16
rokslide rokslide is offline
Registered User
 
Join Date: Nov 2003
Location: Christchurch, New Zealand
Posts: 1,617
Being that you are reading from top to bottom what you are going to need to do for that case is to look at more than 1 line at a time...

Code:
previousLine = ""
do while not fsoTextStream.atEndOfStream 
    currentLine = fsoTextStream.readLine 
     
    'check if the line has no string 
    if previousLine <> "" then 
        response.write fsoTextStream.line & ": " & previousLine & "<br />" 
    else if previousLine = "" and isHeader(currentLine)
        response.write fsoTextStream.line & ": " & previousLine & "<br />" 
    else 
        response.write fsoTextStream.line & ": " & "none" & "<br />" 
    end if 
    previousLine = currentLine
loop  

if previousLine <> "" then 
    response.write fsoTextStream.line & ": " & previousLine & "<br />" 
else if previousLine = "" and isHeader(currentLine)
    response.write fsoTextStream.line & ": " & previousLine & "<br />" 
else 
    response.write fsoTextStream.line & ": " & "none" & "<br />" 
end if
Something similar to the above... you would need to create your isHeader function so that it returns a boolean value to indicate if you have a header line or not. You also need to remember that you will exit your reading loop early so you need the duplicated code at the end of the loop (you could easily wrap this code into another method so no duplication is needed.

You could of course read the whole file at once and split it into an array of strings which would let you move back and forth through the lines as you wanted.... can supply code for this if needed.
Reply With Quote
  #5 (permalink)  
Old 03-20-11, 21:49
neo_phyte neo_phyte is offline
Registered User
 
Join Date: Feb 2011
Posts: 12
Quote:
Originally Posted by rokslide View Post
Being that you are reading from top to bottom what you are going to need to do for that case is to look at more than 1 line at a time...

Code:
previousLine = ""
do while not fsoTextStream.atEndOfStream 
    currentLine = fsoTextStream.readLine 
     
    'check if the line has no string 
    if previousLine <> "" then 
        response.write fsoTextStream.line & ": " & previousLine & "<br />" 
    else if previousLine = "" and isHeader(currentLine)
        response.write fsoTextStream.line & ": " & previousLine & "<br />" 
    else 
        response.write fsoTextStream.line & ": " & "none" & "<br />" 
    end if 
    previousLine = currentLine
loop  

if previousLine <> "" then 
    response.write fsoTextStream.line & ": " & previousLine & "<br />" 
else if previousLine = "" and isHeader(currentLine)
    response.write fsoTextStream.line & ": " & previousLine & "<br />" 
else 
    response.write fsoTextStream.line & ": " & "none" & "<br />" 
end if
Something similar to the above... you would need to create your isHeader function so that it returns a boolean value to indicate if you have a header line or not. You also need to remember that you will exit your reading loop early so you need the duplicated code at the end of the loop (you could easily wrap this code into another method so no duplication is needed.

You could of course read the whole file at once and split it into an array of strings which would let you move back and forth through the lines as you wanted.... can supply code for this if needed.
Rokslid, your are genius.

But how can I remove the wrong new line? And also can you show me the split function solution for this. Thank you so much...
Reply With Quote
  #6 (permalink)  
Old 03-20-11, 22:01
neo_phyte neo_phyte is offline
Registered User
 
Join Date: Feb 2011
Posts: 12
And also can you do it for me the isHeader function. I am really dumb to think on this... Please...
Reply With Quote
  #7 (permalink)  
Old 03-20-11, 23:46
rokslide rokslide is offline
Registered User
 
Join Date: Nov 2003
Location: Christchurch, New Zealand
Posts: 1,617
A couple of things you need to bear in mind...

1. You are never removing lines as such, you are rewriting your file. If you have a blank line that you don't want to write to your new file just don't write it. eg. remove the
Code:
response.write fsoTextStream.line & ": " & "none" & "<br />"
2. I can't tell you how to write the isHeader function except to say it would look something like this....
[code]
function isHeader(line)
isHeader = false
lineStart = left(line, 4)
if(lineStart = "cmp_" or lineStart="cmg_") isHeader = true
end function
[code]
... having said that you need to look at what is common for header lines but does not turn up in the report lines. This is a great example of why you should not have multiple sets of data in a single flat file.

For the spliting and treating everything like an array.... Look at the ReadAll() method for the TextStream object (here is a starting point http://msdn.microsoft.com/en-us/library/312a5kbt(v=vs.85).aspx) and then at the split function (http://www.w3schools.com/VBscript/func_split.asp). If you know that a line ends with chr(10)+chr(13) you can use that for your delimiter in the split function call. Then you can loop through the array using UBound function (http://www.w3schools.com/VBScript/func_ubound.asp) to determine how many times to loop.

I refuse to accept the "I'm too dumb" line if you don't give it a bash first and post your attempt.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On