从输出中删除空行 [英] Remove blank lines from the output

查看:104
本文介绍了从输出中删除空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本:

for ( ; $true ; )
{
    Write-Host ""
    Get-Date -UFormat "%Y-%m-%d %H:%M:%S"
    ping -n 1 10.10.50.203 | Select-String -SimpleMatch "Pinging" -Context 1,2
    ping -n 1 10.10.50.201 | Select-String -SimpleMatch "Pinging" -Context 1,2
    timeout 5 > null
}

它产生这个输出:

2018-08-29 14:40:49


> Pinging 10.10.50.203 with 32 bytes of data:
  Request timed out.


> Pinging 10.10.50.201 with 32 bytes of data:
  Reply from 10.10.50.201: bytes=32 time=58ms TTL=126

2018-08-29 14:40:54

> Pinging 10.10.50.203 with 32 bytes of data:
  Request timed out.


> Pinging 10.10.50.201 with 32 bytes of data:
  Reply from 10.10.50.201: bytes=32 time=58ms TTL=126

我们需要没有空行:

2018-08-29 14:40:49
> Pinging 10.10.50.203 with 32 bytes of data:
  Request timed out.
> Pinging 10.10.50.201 with 32 bytes of data:
  Reply from 10.10.50.201: bytes=32 time=58ms TTL=126

2018-08-29 14:40:54
> Pinging 10.10.50.203 with 32 bytes of data:
  Request timed out.
> Pinging 10.10.50.201 with 32 bytes of data:
  Reply from 10.10.50.201: bytes=32 time=58ms TTL=126

我正在尝试来自 this这个 问题,但没有任何帮助.

I was trying solutions from this and this questions but nothing helps.

尤其是<代码>|ForEach-Object { $_.Trim() } 解决方案产生此错误消息:

Especially the | ForEach-Object { $_.Trim() } solution produces this error message:

... 不包含名为Trim"的方法.

... does not contain a method named 'Trim'.

PS 版本:

PS C:\WINDOWS\system32> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17134.165
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.165
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

推荐答案

这很好用.

for ( ; $true ; )
{
    Write-Host ""
    Get-Date -UFormat "%Y-%m-%d %H:%M:%S"
    (ping -n 1 www.google.com | Select-String -SimpleMatch "Pinging" -Context 1,2 | Out-String).Trim()
    (ping -n 1 www.google.co.in | Select-String -SimpleMatch "Pinging" -Context 1,2 | Out-String).Trim()
    timeout 5 > null
}

输出:

2018-08-29 18:42:10
> Pinging www.google.com [216.58.197.36] with 32 bytes of data:
  Reply from 216.58.197.36: bytes=32 time=21ms TTL=53
> Pinging www.google.co.in [216.58.197.35] with 32 bytes of data:
  Reply from 216.58.197.35: bytes=32 time=15ms TTL=57

这篇关于从输出中删除空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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