【问题标题】:powershell delete everything after character for every line in a filepowershell 删除文件中每一行的字符后的所有内容
【发布时间】:2015-03-03 21:24:21
【问题描述】:

我有一个文件,如下所示,我想使用 powershell 删除文件中每一行中第一个“

"10.0.0.1","pc1","Microsoft Windows Server 2008 SP2 Microsoft Windows 7 SP0 - SP1、Windows Server 2008 SP1 或 Windows 8 "

"10.0.0.2","pc2","Microsoft Windows Server 2008 SP2 Microsoft Windows 7 SP0 - SP1、Windows Server 2008 SP1 或 Windows 8 "

"10.0.0.3","pc3","Microsoft Windows Server 2008 SP2 Microsoft Windows 7 SP0 - SP1、Windows Server 2008 SP1 或 Windows 8 "

【问题讨论】:

  • 你在正确的轨道上。
  • 我打算在文件上做一个获取内容,然后在 $file 中做一个 foreach $line,但我不确定如何让它在第一个“
  • 你可以这样做:$a="test<bla>";$a.Substring(0,$a.IndexOf("<")) 参考:Substring,IndexOf。

标签: powershell


【解决方案1】:

以下是如何在某个分隔符处截断行的示例:

$line = '"10.0.0.1","pc1","Microsoft Windows Server 2008 SP2 <100%-accuracy> Microsoft Windows 7 SP0 - SP1, Windows Server 2008 SP1, or Windows 8 <100%-accuracy>"'

$linetruncated = $line.Substring(0,$line.IndexOf('<'))

【讨论】:

  • 谢谢你,我的朋友,因为我没有 15 个代表,所以我不会投票给你,但这很有效!这就是我最终得到的 $a = get-content =\scanreport.csv FOREACH ($line in $a) { $line.Substring(0,$line.IndexOf('
  • 您应该可以标记为答案。投票是在有多个答案来决定哪个是首选答案时进行的。谢谢。感谢 Matt 帮助我的答案看起来更好。
猜你喜欢
  • 2020-07-19
  • 2015-10-23
  • 2012-10-19
  • 1970-01-01
  • 2019-10-21
  • 2017-06-03
  • 2014-06-15
  • 1970-01-01
  • 2011-08-03
相关资源
最近更新 更多