图案和尺寸的配套字符串 [英] Matching string of pattern and size

查看:125
本文介绍了图案和尺寸的配套字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本文件中搜索两个字符串. 仅当第一个字符串大于8个字符时才输出输出.

I want to search a text file for two strings. The output will only be printed if the first string is greater than 8 characters.

这是我要运行的命令:

Get-Content -Path .\std_server*.out | Select-String '((if "cpu=" -gt 8)|application=")' | out-File  -width 1024 .\test.txt

因此,我想在文件std_server * .out中搜索CPU和APPLICATION的值,但是我只想在CPU的值大于8个字符的情况下打印这些值.

So I want to search file std_server*.out for both values CPU and APPLICATION, but I only want to print these values if the value for CPU is greater than 8 characters.

我该怎么做?

当前,我有一个与'(cpu = | application =)'一起使用的基本版本,但是可以打印出所有CPU值,我只希望打印出该应用程序当CPU的值过高(CPU> 8)时.

Currently, I have a basic version that works with '(cpu=|application=")', however that prints out all values of CPU, and I only want the application to be printed out when CPU is an unreasonably high value (CPU > 8).

谢谢.

推荐答案

如果他们自己行,可以使用类似的东西

If they're on their own lines could use something like

Get-Content -Path .\std_server*.out | ?{($_.StartsWith("CPU=") -And $_.Length -gt 12) -Or $_.StartsWith("Application=")} | out-File  -width 1024 .\test.txt

或者您可以检查结束字符(例如分号),例如

Or you could check for an end character (semicolon for example) like

Get-Content -Path .\std_server*.out | ?{(($_.StartsWith("CPU=") -And $_.Length -gt 12) -Or $_.StartsWith("Application=")) -And $_.EndsWith(";")} | out-File  -width 1024 .\test.txt

或者只使用正则表达式,例如

Or just use a regular expression, like

Get-Content -Path .\std_server*.out | Select-String '^(cpu=\d{8,}|application=\d*)$' | out-File  -width 1024 .\test.txt

如果他们是自己一行,或者

if they're on their own lines, or

Get-Content -Path .\std_server*.out | Select-String '(cpu=\d{8,}|application=\d*);' | out-File  -width 1024 .\test.txt

如果它们之间用字符分隔(在本例中为';').

if they're delimited by a character (in this case ';').

这篇关于图案和尺寸的配套字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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