使用外档时避免换行 [英] Avoid line breaks when using out-file

查看:61
本文介绍了使用外档时避免换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于正在编写的PowerShell脚本,我感到有些沮丧.

基本上,我遍历文本文件以根据正则表达式模式数组检查每一行. 结果将通过管道传输到输出文件cmdlet,然后将其附加到另一个文本文件.

Get-ChildItem $logdir -Recurse -Include @('*.txt') | Get-Content | ForEach-Object { 
Select-String $patterns -InputObject $_ | Out-File $csvpath -Append -Width 1000 }

我的问题是我无法退出文件,以忽略它在$ csvpath后面的文件中创建的其他换行符(每行后三). 我可以使用.NET Framework类来实现相同的目的,但是我宁愿坚持使用纯PowerShell;-)

非常感谢您的帮助.

凯文

解决方案

为什么不使用Add-Content?

gci $logdir -rec *.txt | gc | select-string $pattern | add-content $csvpath

您不需要指定宽度和-append开关,默认情况下文件大小不会加倍(尽管您可以指定编码),而且空行似乎没有问题. /p>

I'm getting a little frustrated on a little PowerShell script I'm writing.

Basically I loop through text files to check every line against an array of regular expression patterns. The result gets piped to the out-file cmdlet which appends it to another text file.

Get-ChildItem $logdir -Recurse -Include @('*.txt') | Get-Content | ForEach-Object { 
Select-String $patterns -InputObject $_ | Out-File $csvpath -Append -Width 1000 }

My problem is that I can't get out-file to omit those additional line breaks it creates in the file behind $csvpath (three after each line). I could use .NET framework classes to achieve the same thing but I'd rather stick to pure PowerShell ;-)

Any help is greatly appreciated.

Kevin

解决方案

Why don't you use Add-Content?

gci $logdir -rec *.txt | gc | select-string $pattern | add-content $csvpath

You don't need to specify the width and -append switch, the file size is not doubled by default (although you can specify encoding) and it seems that there is no problem with the empty lines like you have.

这篇关于使用外档时避免换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆