The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

goldfinger (9) [Avatar] Offline
#1
Replace Tab with two CR/LF

Working:
"Line 1`tLine 2`tLine 3" | Set-Content -encoding ascii test1.txt
Get-Content unixlike1.txt -encoding ascii| foreach {$_ -replace "`t","`r`n`r`n"} | Set-Content -encoding ascii win1.txt

Working:
I tried to use regular expressions in the replace statement.
Get-Content test1.txt -encoding ascii| foreach {$_ -replace " ","`r`n`r`n"} | Set-Content -encoding ascii win1.txt

Working too:
Get-Content test1.txt -encoding ascii| foreach {$_ -replace "[ ]","`r`n`r`n"} | Set-Content -encoding ascii win1.txt


Test:
"Line 1`nLine 2`nLine 3`n" | Set-Content -encoding ascii unixlike2.txt
Get-Content unixlike2.txt -encoding ascii | Set-Content -encoding ascii win2.txt

unixlike2.txt looks ok. As expected the get-contents has attached a two bytes (CR/LF) at the end of file.
win2.txt is ok,too. The newlines are automatically converted to CR/LF

Newline replacement
Now I tried to convert the newlines of the unixline textfile to two CR/LF.

Failt:
Get-Content unixlike2.txt -encoding ascii| foreach {$_ -replace "`n","`r`n`r`n"} | Set-Content -encoding ascii win2.txt
Get-Content unixlike2.txt -encoding ascii| foreach {$_ -replace "
","`r`n`r`n"} | Set-Content -encoding ascii win2.txt
Get-Content unixlike2.txt -encoding ascii| foreach {$_ -replace "[
]","`r`n`r`n"} | Set-Content -encoding ascii win2.txt

Test to replace newline to tab
Failt:
Get-Content unixlike2.txt -encoding ascii| foreach {$_ -replace "`n","`t"} | Set-Content -encoding ascii win3.txt

Now I has a understanding problem.
Why I can't convert a newline to two CR/LF.
What will get-content or the replace operator do that this are not working.
Any solution for the problem without using the byte encoding of get-content?
Kiron (38) [Avatar] Offline
#2
Re: Newline replacement
Not certain if this is what you intend to do, hope it helps.
Try piping Get-Content's output to Out-String to compare the pattern to a single string, then replace all newlines except the one at the end of the string using zero-width assertions.

# Newline to two CR/LF
# using negative lookahead
Get-Content unixlike2.txt -encoding ascii | out-string | foreach {$_ -replace "`n(?!$)","`r`n`r`n"} | Set-Content -encoding ascii win2.txt

# Newline to Tab
# using negative lookahead
Get-Content unixlike2.txt -encoding ascii | Out-String | foreach {$_ -replace "`r`n(?!$)","`t"} | Set-Content -encoding ascii win3.txt
# using positive lookbehind
Get-Content unixlike2.txt -encoding ascii | Out-String | foreach {$_ -replace "(?<=.)`r`n","`t"} | Set-Content -encoding ascii win3.txt

Since by default Out-String returns a single string you can omit a foreach block like this:

# Newline to two CR/LF
# using negative lookahead
(Get-Content unixlike2.txt -encoding ascii | out-string) -replace "`n(?!$)","`r`n`r`n" | Set-Content -encoding ascii win2.txt

# Newline to Tab
# using negative lookahead
(Get-Content unixlike2.txt -encoding ascii | Out-String) -replace "`r`n(?!$)","`t" | Set-Content -encoding ascii win3.txt
# using positive lookbehind
(Get-Content unixlike2.txt -encoding ascii | Out-String) -replace "(?<=.)`r`n","`t" | Set-Content -encoding ascii win3.txt