0

I have looked at this question, and it's close to what I need to do, but the text I need to replace is inconsistent.

I need to replace "`r`n with ", but only the first of the 2 adjacent lines

example: (the full file is 50k lines and up to 500 chars wide)

    ID,Name,LinkedRecords
    54429,Abe,
    54247,Jonathan,"
    63460|63461"
    54249,Teresa,
    54418,Cody,
    58046,Joseph,
    58243,David,
    ,Barry,"
    74330"
    C8876,Simon,
    X_10934,David,

should become

    ID,Name,LinkedRecords
    54429,Abe,
    54247,Jonathan,"63460|63461"
    54249,Teresa,
    54418,Cody,
    58046,Joseph,
    58243,David,
    ,Barry,"74330"
    C8876,Simon,
    X_10934,David,

I can see this will probably be useful, but I'm having a hard time getting the command to work as desired

SeanC
  • 15,695
  • 5
  • 45
  • 66
  • [1] are the lines REALLY terminated with literal "backtick r backtick n" sequences? [2] how do you decide what lines to merge? it LOOKS like you want to mere any line that has no name with the line ahead of it. – Lee_Dailey Apr 27 '20 at 21:23
  • I was using the formatting from the replacement strings - it is carriage return/linefeed – SeanC Apr 28 '20 at 13:05
  • you may want to correct your posted sample data to NOT show the literal `cr/lf` chars. [*grin*] i see that `AdminOfThings` understood how to determine what lines to merge ... and posted a neat bit of code for it. i'm glad to know you got your solution ... [*grin*] – Lee_Dailey Apr 28 '20 at 13:17

1 Answers1

1

If the `r`n characters are literal, then you can do the following:

[System.IO.File]::ReadAllText('c:\path\file.txt') -replace '(?<=,")`r`n\r?\n' |
    Set-Content c:\path\file.txt

If `r`n are actual carriage return and line feed chars, then you can do the following:

[System.IO.File]::ReadAllText('c:\path\file.txt') -replace '(?<=,")\r\n' | 
    Set-Content c:\path\file.txt

Note if memory becomes an issue, a different approach may be needed.

mklement0
  • 382,024
  • 64
  • 607
  • 775
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27