Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2011
    Posts
    12

    Post Unanswered: 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?

  2. #2
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    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 17:29.

  3. #3
    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

  4. #4
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    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.

  5. #5
    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...

  6. #6
    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...

  7. #7
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •