gpt4 book ai didi

Powershell 替换 csv 中的一个实例

转载 作者:行者123 更新时间:2023-12-05 01:50:01 25 4
gpt4 key购买 nike

我需要跟踪用户的端口分配。我有一个包含这个的 csv:

USERNAME,GUI,DCS,SRS,LATC,TRSP
joeblow,8536,10631,5157,12528,14560
,8118,10979,5048,12775,14413
,8926,10303,5259,12371,14747
,8351,10560,5004,12049,14530
johndoe,8524,10267,5490,12809,14493
,8194,10191,5311,12275,14201
,8756,10813,5714,12560,14193
,8971,10006,5722,12078,14378
janblow,8410,10470,5999,12123,14610
bettydoe,8611,10448,5884,12040,14923
,8581,10965,5832,12400,14230
,8708,10005,5653,12111,14374
,8493,10016,5464,12827,14115

我需要能够添加用户和删除用户,这将使 csv 看起来像现在一样。我有这段代码的删除部分:

[io.file]::readalltext("c:\scripts\RNcsv.csv").replace("$username","") | Out-File c:\scripts\RNcsv.csv -Encoding ascii -Force

我尝试了上面代码的反向操作,但它不想在该上下文中使用空值。我一直没有找到将 $username 添加到单个记录的方法。准确地说,是第一条名称列为空的记录。所以当 joeshmo 出现时,他最终出现在 joeblow 下面的记录中。这个 csv 表示人们来了又走了。

最佳答案

我会采用面向对象的方法使用 Import-Csv和可重复使用的 function从管道获取输入:

function Add-User {
param(
[Parameter(Mandatory)]
[string] $Identity,

[Parameter(Mandatory, ValueFromPipeline, DontShow)]
[object] $InputObject
)

begin { $processed = $false }
process {
# if the user has already been added or the UserName column is populated
if($processed -or -not [string]::IsNullOrWhiteSpace($InputObject.UserName)) {
# output this object as-is and go to the next object
return $InputObject
}
# if above condition was not met we can assume this is an empty value in the
# UserName column, so set the new Identity to this row
$InputObject.UserName = $Identity
# output this object
$InputObject
# and set this variable to `$true` to skip further updates on the csv
$processed = $true
}
}

将新用户添加到 Csv 将是:

(Import-Csv .\test.csv | Add-User -Identity santiago) | Export-Csv .\test.csv -NoTypeInformation

请注意,由于以上是在单个管道中读取写入到同一文件,因此使用 Grouping operator ( )必须使用 Import-Csv 的所有输出并将对象保存在内存中。没有它,您最终会得到一个空文件。

否则只需将其分成两步(同样,只有在读取和写入同一文件时才需要这样做):

$csv = Import-Csv .\test.csv | Add-User -Identity santiago
$csv | Export-Csv .\test.csv -NoTypeInformation

将此细微修改添加到上面发布的函数中,允许在一个函数调用中添加多个用户。所有学分 iRon想出一个clever and and concise solution .

function Add-User {
param(
[Parameter(Mandatory)]
[string[]] $Identity,

[Parameter(Mandatory, ValueFromPipeline, DontShow)]
[object] $InputObject
)

begin { [System.Collections.Queue] $queue = $Identity }
process {
# if there are no more Identities in Queue or the UserName column is populated
if(-not $queue.Count -or -not [string]::IsNullOrWhiteSpace($InputObject.UserName)) {
# output this object as-is and go to the next object
return $InputObject
}
# if above condition was not met we can assume this is an empty value in the
# UserName column, so dequeue this Identity and set it to this row
$InputObject.UserName = $queue.Dequeue()
# output this object
$InputObject
}
}

(Import-Csv .\test.csv | Add-User -Identity Santiago, 4evernoob, mrX, iRon) | Export-Csv ...

关于Powershell 替换 csv 中的一个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73495232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com