从输出中删除空白行

时间:2018-08-29 13:05:50

标签: powershell output

我有这个脚本:

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

我正在尝试解决thisthis问题的解决方案,但没有帮助。

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

  

...不包含名为“ 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

3 个答案:

答案 0 :(得分:4)

这很好。

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

答案 1 :(得分:0)

尤其是| ForEach-Object {$ _。Trim()}解决方案会产生以下错误消息:

...不包含名为“ Trim”的方法。

我猜那是因为您没有使用

// Change the default welcome e-mail
add_filter( 'wp_new_user_notification_email', 'welcome__email', 10, 3 );

function welcome__email( $wp_new_user_notification_email, $user, $blogname ) {

     $wp_new_user_notification_email['subject'] = sprintf(__( 'Company Name | Complete registration' ), $blogname, $user->user_login );

     // Set password link
     $key = get_password_reset_key( $user );

     // Build the email
     $message = sprintf(__('Welcome!')) . "\r\n\r\n";
     $message .= 'By clicking the link below you can activate your account:' . "\r\n";
     $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . "\r\n\r\n";
     $message .= "After setting a password you can login." . "\r\n";

     $wp_new_user_notification_email['message'] = $message;

     // Change header information
     $wp_new_user_notification_email['headers'] = 'From: Company Name <noreply@example.com>'; 

     return $wp_new_user_notification_email;

}

在ForEach之前?

out-string | 

答案 2 :(得分:0)

您也可以使用 -replace "`n",'' 删除空行。这在想要保留空格时特别有用,例如Trim() 方法将删除的缩进行。通常不需要 Out-String

在这个例子中的代码是:

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) -replace "`n",''
    (ping -n 1 10.10.50.201 | Select-String -SimpleMatch "Pinging" -Context 1,2) -replace "`n",''
    timeout 5 > $null
}