【问题标题】:How can I get only one line for output of command in powershell?如何在 powershell 中只获得一行命令输出?
【发布时间】:2020-12-12 08:39:18
【问题描述】:

在 Linux 中,我使用它来选择特定的行:

ip a | sed -n '3p' | xargs

但我不知道如何在 Powershell 中制作它。

【问题讨论】:

  • 在Powershell中无法轻松获取广播地址

标签: linux powershell command-line


【解决方案1】:

正如 Smorkster 提到的,powershell 在对象中工作。

您可以使用此处所示的点符号来访问对象的属性,然后使用索引

(Get-NetIPAddress).ipaddress[3]

【讨论】:

  • 我真正需要的是从命令输出中提取特定行
  • 好吧,就像我说的 PowerShell 中的输出不是文本。你想解决什么问题?了解这方面的用例会很有帮助。您可以通过多种方式过滤管道对象
  • 我想要的是如果有一个关闭的端口并且在输出命令时返回错误,只取最后一行:with error: Test-NetConnection -InformationLevel "Quiet" 8.8.8.8 -p 81 没有错误:Test-NetConnection -InformationLevel "Quiet" 8.8.8.8 -p 53
  • 基本上省略了这个命令输出的错误,你告诉我我已经改进了这个命令(Test-NetConnection 192.168.1.1 -p 81).TcpTestSucceeded
  • Test-NetConnection 的输出非常烦人!给你:Test-NetConnection -InformationLevel "Quiet" 8.8.8.8 -p 81 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
【解决方案2】:

Powershell(几乎完全)在对象中工作。这样您就可以使用索引来获得某个值/输出。 所以你可以做

( Get-NetIPAddress ).ToString()[3] 

【讨论】:

  • 在我的机器上( Get-NetIPAddress ).ToString()[3] 只给出一个字母t
  • 好吧。不确定输出,但如果你只得到一个字符,输出是一个字符串,所以 PS 索引 3:d 字母。
  • 我真正需要的是从命令输出中提取特定行
猜你喜欢
  • 1970-01-01
  • 2015-06-24
  • 2021-07-10
  • 1970-01-01
  • 2022-10-15
  • 2022-08-14
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
相关资源
最近更新 更多