Some basic staff and parsing help

Welcome Forums General PowerShell Q&A Some basic staff and parsing help

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
1 month, 2 weeks ago.

  • Author
    Posts
  • #117442

    Participant
    Points: 16
    Rank: Member

    Hi all,
    I'm now in PS and need a help.
    I have text file like this:
    1234,00 0,23RF$%$,TIFF ,050000000,049480158.15,9128284B3,TEST AND Note ,20181018,20181019,new york basement PC1,
    1234,3,3L501C12,TIFF,050000000,049480158.15,9128284B3,TEST & Note ,20181018,20181019,PC2 in basement,NY ,
    1234,00 0,23RF$%$,TIFF ,050000000,049480158.15,9128284B3,TEST AND Note ,20181018,20181019,new york basement PC1,
    1234,3,3L501C12,TIFF,050000000,049480158.15,9128284B3,TEST & Note ,20181018,20181019,PC2 in basement,NY ,
    1234,00 0,23RF$%$,TIFF ,050000000,049480158.15,9128284B3,TEST AND Note ,20181018,20181019,new york basement PC1,
    1234,3,3L501C12,TIFF,050000000,049480158.15,9128284B3,TEST & Note ,20181018,20181019,PC2 in basement,NY ,

    witch i need to export to csv file with comma delimiter , but need to remove comma in PC2 in basement,NY and special character 3th column but not % and & character form column 8.
    Any help

    Thanks

  • #117583

    Participant
    Points: 10
    Rank: Member

    I chose to use the switch statement because I am not sure how large your text file is or if you have multiple files. Switch statement is more optimal than 'Get-Content'.

    $file = Get-ChildItem \\path\to\textfile.txt
    
    $result = 
    switch -Regex -File ($file){
       {$_} {$line = $_ -replace "basement,NY","basement NY" -split ','}
       {$_} {[PSCustomObject]@{
            H0 = $line[0]
            H1 = $line[1]
            H2 = $line[2] -replace '[^\w%]'
            H3 = $line[3]
            H4 = $line[4]
            H5 = $line[5]
            H6 = $line[6]
            H7 = $line[7] -replace '&'
            H8 = $line[8]
            H9 = $line[9]
            H10 = $line[10]
            }}
    }
    
    $result | Export-Csv -Path \\path\to\result.csv -NoTypeInformation
    
  • #118683

    Participant
    Points: 16
    Rank: Member

    Thanks, but basement and NY was just example, I need something more like to repleace comma with space in 10th column of txt file,

    but my problem is that I can use only comma(,) as field separator but  I want to remove all strings afrer 10th one,

    Example :

    col1(4 character)|Col2(4ch) |Col3(5ch)|...|col9(30ch)| col10(30ch)
    1234,                      1234,           12345,.... .....,string with 30 character, this is column 10th but have extra coma in,

    thanks for your help.

     

  • #118687

    Participant
    Points: 266
    Helping Hand
    Rank: Contributor

    You want to replace all commas after the... ninth instance per line?

    Hmm. I think regex can do that.

    $File = Get-Content '.\File.txt'
    $Output = foreach ($Line in $File) {
    $Line -replace '(?< =(.*,.*){9,}),', ' '
    }
    $Output | Set-Content '.\NewFile.txt'

    EDIT: This forum seems to insert spaces in odd places. Let me gist that instead:

You must be logged in to reply to this topic.