none
Reading Text Files RRS feed

  • Question

  • OK, so my script reads the contents of a directory (which happen to be long symantec logs) and collates them all into one file.

    Anyone who use's Symantec will know the formatting of there logs is not very good with a lot of commas in there.

    Does anyone know a useful command that will strip out the commas, i.e replace ,,,,,,,,,,,,, with a space.

    The problem is there is always a different number of commas but only want a few spaces for each of the series, so replace ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, with "   " as well as replace ,,,,,,,,,,,,,,,,, with "  "  leaving me the useful stuff only.

    Many Thanks

    Friday, September 27, 2013 1:37 PM

Answers

  • You can try something like

    Get-Content C:\log.txt | Foreach-Object {
      $_ -replace ",{2,}", " "
    }
    And just in case you are curious, the power is with the regex expression of ",{2,}" what that means is find 2 or more commas, and then the -replace, will replace it with a space.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet



    • Edited by clayman2 Friday, September 27, 2013 2:00 PM edit
    • Marked as answer by Tarrley Saturday, September 28, 2013 2:02 PM
    Friday, September 27, 2013 1:56 PM
  • Hi,

    quick and dirty.

    $File = Get-Content "C:\path\logfile.txt"
    foreach ($Line in $File) {
        $a = ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
        for ($b = $a.length ; $b -gt 0 ; $b--) {
            $Line = $Line.Replace($a.Substring(0,$b)," ")
            Write-Host $Line
        }
    }

    • Marked as answer by Tarrley Saturday, September 28, 2013 2:02 PM
    Friday, September 27, 2013 2:14 PM

All replies

  • You can try something like

    Get-Content C:\log.txt | Foreach-Object {
      $_ -replace ",{2,}", " "
    }
    And just in case you are curious, the power is with the regex expression of ",{2,}" what that means is find 2 or more commas, and then the -replace, will replace it with a space.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet



    • Edited by clayman2 Friday, September 27, 2013 2:00 PM edit
    • Marked as answer by Tarrley Saturday, September 28, 2013 2:02 PM
    Friday, September 27, 2013 1:56 PM
  • Hi,

    quick and dirty.

    $File = Get-Content "C:\path\logfile.txt"
    foreach ($Line in $File) {
        $a = ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
        for ($b = $a.length ; $b -gt 0 ; $b--) {
            $Line = $Line.Replace($a.Substring(0,$b)," ")
            Write-Host $Line
        }
    }

    • Marked as answer by Tarrley Saturday, September 28, 2013 2:02 PM
    Friday, September 27, 2013 2:14 PM
  • Hi,

    quick and dirty.

    $File = Get-Content "C:\path\logfile.txt"
    foreach ($Line in $File) {
        $a = ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
        for ($b = $a.length ; $b -gt 0 ; $b--) {
            $Line = $Line.Replace($a.Substring(0,$b)," ")
            Write-Host $Line
        }
    }


    That will only work if the commas are always a fixed size, but what if they are not? Then regular expressions are better

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet

    Friday, September 27, 2013 2:15 PM
  • Perhaps a stupid question, but are you sure you want to remove these? My thought is that the file is comma delimited for easy loading into Excel and such.

    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,000+ strong and growing)

    Friday, September 27, 2013 2:20 PM
    Moderator
  • Hi,

    quick and dirty.

    $File = Get-Content "C:\path\logfile.txt"
    foreach ($Line in $File) {
        $a = ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
        for ($b = $a.length ; $b -gt 0 ; $b--) {
            $Line = $Line.Replace($a.Substring(0,$b)," ")
            Write-Host $Line
        }
    }


    That will only work if the commas are always a fixed size, but what if they are not? Then regular expressions are better

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet

    This sample works from 40 down to 1 commas. If you need more you have to append $a. Dirty ;-)
    Friday, September 27, 2013 2:27 PM
  • This sample works from 40 down to 1 commas. If you need more you have to append $a. Dirty ;-)

    Or you can just simply do $text -replace ",{2,}"," " and it won't matter how many there are :-)

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet

    Friday, September 27, 2013 2:36 PM