PowerShell中读取多行文本示例
2018-10-12 14:10
399 查看
适用于PowerShell3.0或者更高本版
有时,你可能会偶尔发现下面的代码:
$FilePath = "$env:SystemRoot\WindowsUpdate.log"
$ContentsWithLinebreaks = (Get-Content $FilePath) -join "`r`n"
猜猜它,想干啥子奥,Get-Content 默认将文本文件以单行读取,并且返回一个多行数组,而-join操作符可以将它们转换成一个单独的字符串。而伴随着PowerShell3.0 的低调问世,有这么一个参数: -Raw,它可以非常高效的得到上面代码同样的结果:
$FilePath = "$env:SystemRoot\WindowsUpdate.log"
$ContentsWithLinebreaks = (Get-Content $FilePath) -join "`r`n"
$ContentsWithLinebreaks2 = Get-Content $FilePath -Raw
$ContentsWithLinebreaks -eq $ContentsWithLinebreaks2
试着运行上面的代码, $ContentWithLinebreaks 和$ContentWithLinebreaks2可能比较的结果略微有所不同,其不同可能只是换行符而已。
那我们继续辨别真伪,果不其然:
PS> $ContentsWithLinebreaks -eq $ContentsWithLinebreaks2.TrimEnd("`r`n")
True
相关文章推荐
- Windows系统中使用C#读取文本文件内容的小示例
- sharepoint开发技巧:读取启用了追加功能的多行文本的历史版本记录
- C++多行文本读取
- textarea中的多行文本(有换行)的内容提交到access数据库中 ,如何读取出来和编辑时一样
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- 利用缓冲流来实现多行文本的读取
- 【PowerShell】逐行处理文本示例
- dxfgrabber简单读取dxf文件中的多行文本和单行文本信息
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- perl一次读取多行文本的策略
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- Matlab处理读取ASCII文本数据找到规则需求,重新写入数据到新的文本文件[示例:有限元边界条件施加]
- Python实现读取邮箱中的邮件功能示例【含文本及附件】
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例 .
- perl一次读取多行文本的策略
- PowerShell控制台输出符号+函数参数类型指定+文本内容读取
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例