【问题标题】:Removing text in a string between two characters using Powershell使用Powershell删除两个字符之间的字符串中的文本
【发布时间】:2015-06-23 00:37:27
【问题描述】:

我有一个运行和收集信息并将其放入 .csv 文件的 powershell 脚本。信息示例如下所示,每行以唯一的服务器名称开头,后跟包含一对 ( ) 的随机唯一标识符。

"GDR01W01SQ004 (e785754f-eeb1)","1","4","63","NY-TER-PLN-P-5N"
"GDR01L02D001 (4b889a4d-d281)","4","12","129","CO-FDP-STE-NP-5N"

我有第二个 powershell 脚本,它运行并获取此 .csv 文件及其信息并将其格式化为带有标题和适当间距的报告。

有人可以帮我删除 ( ) 和 ( ) 之间的文字吗?

我希望每行的条目如下所示:

"GDR01W01SQ004","1","4","63","NY-TER-PLN-P-5N"

非常感谢您!

这是我一直在使用的脚本。

####################PowerCLI Check####################

# Verify whether the PowerCLI module is loaded, if not load it.
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PsSnapin VMware.VimAutomation.Core -ErrorAction Stop
}




################### Run Time Section ####################
#This script should be run from a server that has DNS records for all entries in vcenters.txt
$file = get-Content c:\reports\vcenter\vcenters.txt  

foreach ( $server in $file) { 
Connect-VIserver -Server $server

Get-VM | select Name, NumCpu, MemoryGB, ProvisionedSpaceGB, Notes | Out-Null

} 


# Command for Custom Annotations.
Get-VM | select Name, NumCpu, MemoryGB, ProvisionedSpaceGB, Notes -expandproperty customfields  | Export-Csv -path “c:\reports\vcenter\vcenall.csv” -NoTypeInformation

# Takes vcenall.csv and sorts only the Name and Notes columns and selects all but the custom fields. Capacity Reporting script caprep.ps1 runs against this csv.
Import-csv c:\reports\vcenter\vcenall.csv | Sort-Object Notes, Name | Select-Object Name, NumCpu, MemoryGB, ProvisionedSpaceGB, Notes |Export-csv capacity.csv -NoTypeInformation

#Used to remove domain from server name
(Get-Content capacity.csv) | ForEach-Object { $_ -replace ".domain.local", "" } | Set-Content capacity.csv


# Takes vcenall.csv and sorts only the Notes column and selects only the Name and Notes columns. Nimsoft comparison script nimcomp.ps1 runs against this csv.
Import-csv c:\reports\vcenter\vcenall.csv | Sort-Object Notes | Select-Object Name, Notes | Export-csv nimsoft.csv -NoTypeInformation

# Takes vcenall.csv and sorts only the Name columns and exports all fields. Backup/Restore comparison script bure.ps1 runs against this csv.
Import-csv c:\reports\vcenter\vcenall.csv | Sort-Object Name | Export-csv bure.csv -NoTypeInformation

【问题讨论】:

  • 你能告诉我们你的脚本吗?
  • 即使我在黑暗中拍摄,因为我不太了解 powershell,但我确信正则表达式可以工作。这是一个链接,它显示了如何从一对 () 中提取内容,您可以使用该 stackoverflow.com/questions/9113231/…
  • 您的 csv 数据是否包含标题?

标签: string powershell csv text replace


【解决方案1】:

我认为您需要添加更多信息,但只需使用您已经尝试过的方法

Import-Csv C:\temp\test.csv -Header server,1,2,3,4 | ForEach-Object{
    $_.server  = (($_.server).split("(")[0]).Trim()
    $_
} 

我们导入 csv 数据并分配一个标题。如果你已经有一个,那么这个参数可以省略。

然后我们将每一行数据作为一个对象进行检查。通过按空格拆分 server 数据来更改它。如果此数据用于服务器名称,则可以安全地假设第一个空格之前的所有内容都是服务器名称。这种方法取决于那里的空间。我们也可以对( 使用相同的逻辑,但如果空间是保证,这将更容易。

所以我们更新server,然后使用$_ 将数据发送回管道。

样本输出

server        1 2  3   4               
------        - -  -   -               
GDR01W01SQ004 1 4  63  NY-TER-PLN-P-5N 
GDR01L02D001  4 12 129 CO-FDP-STE-NP-5N

基于 cmets 编辑

由于它是服务器显示名称,我将逻辑更改为基于“(”进行拆分。同时使用Split() 方法而不是-split 运算符。

【讨论】:

  • @Jones537 很高兴它有帮助。我也更新了以满足您的需求。检查更新。如果您认为它解决了您的问题,也可以考虑将其标记为答案。
  • 成功了!!太感谢了!!我对其进行了一些更改,因为我发现我有一个带有空格的服务器显示名称,所以我首先将其更改为 ( ; 它完美地清理了所有内容:(Get-Content vcenall.csv) | % { $_ -replace "(",";" } | Set-Content vcenall.csvImport-Csv C:\reports\vcenter\vcenall.csv | ForEach-Object{ $_.Name = ($_.Name -split ";")[0] $_ } | Export-csv vcenall.csv -NoTypeInformation
猜你喜欢
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 2015-08-05
  • 1970-01-01
  • 2016-07-01
  • 2015-11-10
  • 2016-08-13
相关资源
最近更新 更多