运行以下代码时:
$txt = Get-Content file1.txt
$a = @"
-- file start --
$txt
-- file end --
"@
$a
Run Code Online (Sandbox Code Playgroud)
所有新行都将从文件内容中删除,但只是正在运行
$txt
Run Code Online (Sandbox Code Playgroud)
打印出文件而不剥离新行.
知道如何使用here-string让它按需运行吗?
谢谢!
Joe*_*oey 12
如果将数组放入字符串中,则会在项目之间展开$OFS(或者如果$OFS是空格$null).你可以看到相同的效果
"$txt"
''+$txt
Run Code Online (Sandbox Code Playgroud)
还有其他几个.您可以设置$OFS="`r`n"哪个会更改它们与换行符连接的空间.
您也可以Get-Content将开头的任何一个更改为
$txt = Get-Content file1.txt | Out-String
$txt = [IO.File]::ReadAllText((Join-Path $pwd file1.txt))
Run Code Online (Sandbox Code Playgroud)
将$ txt传递给子表达式中的Out-String.
$a = @"
-- file start --
$($txt | Out-String)
-- file end --
"@
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7222 次 |
| 最近记录: |