How to save newlines when using variables here

When running the following code:

$txt = Get-Content file1.txt $a = @" -- file start -- $txt -- file end -- "@ $a 

All new lines are removed from the file, but simply run.

 $txt 

prints a file without deleting new lines.

Any idea how to make it work as desired with this line?

Thanks!

+4
source share
2 answers

Pipe $ txt to Out-String inside a subexpression.

 $a = @" -- file start -- $($txt | Out-String) -- file end -- "@ 
+8
source

If you put an array in a string, it will be expanded with $OFS (or space if $OFS is $null ) between elements. You can see the same effect with

 "$txt" ''+$txt 

and several others. You can set $OFS="`r`n" , which will change the space with which they are connected with line break.

You can also change Get-Content at the beginning to

 $txt = Get-Content file1.txt | Out-String $txt = [IO.File]::ReadAllText((Join-Path $pwd file1.txt)) 
+10
source

Source: https://habr.com/ru/post/1442148/


All Articles