[英]Powershell -Select String (SLS) spliting one line into multiples
I've been looking for a way to perform basic grep with Powershell.我一直在寻找一种使用 Powershell 执行基本 grep 的方法。
I have an input text file but want to extract only some of them For instance,我有一个输入文本文件,但只想提取其中的一些例如,
2020-02-04 05:42:12,132 INFO UselessLog 8=FIX4.4|35=D|49=sender1|56=recipient|....
2020-02-04 05:42:12,134 INFO UselessLog2 8=FIX5.0|35=AB|49=sender2|56=recipient2|....
2020-02-04 05:42:12,136 INFO UselessLog2 8=FIX5.0|35=AB|49=sender3|56=recipient2|....
I'm trying to parse the file with我正在尝试解析文件
sls "8=FIX" fixlogs -ca | select -exp line |sls "=sender1" > parseFix
However, when I open the parseFix file, sls just transformed one line into multiple ones;但是,当我打开 parseFix 文件时,sls 只是将一行转换为多行; eg, the following two lines represent a single match:
例如,以下两行表示单个匹配:
2020-02-04 05:42:12,132 INFO UselessLog
8=FIX4.4|35=D|49=sender1|56=recipient|...
I'm fairly certain there's a config in Powershell to advise not to split but I've investigated and failed to find the one.我相当肯定 Powershell 中有一个配置建议不要拆分,但我已经调查过但没有找到。
I have no access to Unix/Linux server to do a grep magic also I have checked within StackOverFlow but didn't find anything matching my Question.我无法访问 Unix/Linux 服务器来执行 grep 魔术,我也在 StackOverFlow 中进行了检查,但没有找到与我的问题匹配的任何内容。
You need another select -exp line
call after your 2nd sls
( Select-String
) call to make sure that the matching lines are written as-is to the output file:在第二次
sls
( Select-String
) 调用之后,您需要另一个select -exp line
调用,以确保匹配的行按原样写入输出文件:
sls "8=FIX" fixlogs -ca | select -exp line |sls "=sender1" | select -exp line > parseFix
Note: In PowerShell [Core] 7.0+ you can use Select-String -Raw
to directly output the matching lines, without the wrapper object that provide metadata about the match that you get by default.注意:在 PowerShell [Core] 7.0+ 中,您可以使用
Select-String -Raw
直接输出匹配的行,而无需提供默认情况下提供的匹配元数据的包装器对象。 That is, there is then no need to extract the lines via separate select -exp Line
( Select-Object -ExpandProperty Line
) calls.也就是说,不需要通过单独的
select -exp Line
( Select-Object -ExpandProperty Line
) 调用来提取行。
Why your output lines were broken apart :为什么你的输出线被分开了:
Unlike grep
or findstr
, Select-String
outputs match-information objects (of type Microsoft.PowerShell.Commands.MatchInfo
) that wrap the matching lines, not just the matching lines themselves.与
grep
或findstr
不同, Select-String
输出包装匹配行的匹配信息对象( Microsoft.PowerShell.Commands.MatchInfo
类型),而不仅仅是匹配行本身。
These objects, because they aren't single values such as a string or a number, loosely speaking, are subject to PowerShell's default output formatting when you send them to a file with the >
operator, which is effectively an alias for Out-File
, and this formatting (the same representation you'd see in the console) involves automatic word wrapping, ie breaking long lines into multiple ones to fit the display width (even though a file has no inherent width; Out-File -Width
would allow you to control this width).这些对象,因为它们不是单个值,例如字符串或数字,松散地说,当您将它们发送到带有
>
运算符的文件时,它们会受到 PowerShell 的默认输出格式的约束,这实际上是Out-File
的别名,并且这种格式(与您在控制台中看到的表示相同)涉及自动换行,即将长行分成多行以适应显示宽度(即使文件没有固有宽度; Out-File -Width
将允许您来控制这个宽度)。
There's a lot going on here.这里发生了很多事情。 Select-string is outputting a object with a custom format view.
Select-string 正在输出具有自定义格式视图的对象。 ">" is the same as "| out-file", which adds extra formatting, like wrapping the line if it thinks it's too long.
">" 与 "| out-file" 相同,它增加了额外的格式,比如如果它认为它太长就换行。 You didn't show the whole lines of the log but they look pretty long.
您没有显示日志的整行,但它们看起来很长。 By the way, out-file defaults to utf16 encoding (PS 5).
顺便说一下,out-file 默认为 utf16 编码(PS 5)。 Set-content seems to work ok and convert the object to a string:
Set-content 似乎可以正常工作并将对象转换为字符串:
sls 8=FIX fixlogs -ca | select -exp line | sls =sender1 | set-content parsefix
Also, you can make that one regex pattern if the whole thing can be case sensitive or case insensitive.此外,如果整个事情可以区分大小写或不区分大小写,您可以制作一个正则表达式模式。
sls 8=FIX.*=sender1 fixlogs
fixlogs:1:2020-02-04 05:42:12,132 INFO UselessLog 8=FIX4.4|35=D|49=sender1|56=recipient|....
Or just skip the select:或者只是跳过选择:
sls 8=FIX fixlogs -ca | sls =sender1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.