简体   繁体   English

使用-replace从csv的单元格中删除带有特殊字符的字符串

[英]using -replace to remove a string with special characters from cells in a csv

I have a CSV file like: 我有一个CSV文件,例如:

"localpath"
"C:\Users\calabresel"
"C:\Users\goslinep"
"C:\Users\deangelisr"
"C:\Users\bannont"
"C:\Users\goodwind"

I am looking for a way to isolate just the username from each field. 我正在寻找一种仅从每个字段隔离用户名的方法。 I will then query the AD to determine if each user is disabled or enabled. 然后,我将查询广告以确定是否已禁用或启用了每个用户。 I haven't been able to figure out how to get just the last piece though. 不过,我还无法弄清楚如何获得最后一块。 My idea was to use -replace to replace the identical string with null like this: 我的想法是使用-replace将相同的字符串替换为null,如下所示:

$txt = import-csv paths1.csv | % {$_.localpath = $_.localpath -replace "C:\Users\", ""}

That came back with invalid regular expression pattern errors though which I assumed was a result of the target string containing special characters (the backslashes). 回来的时候是无效的正则表达式模式错误,尽管我认为这是目标字符串包含特殊字符(反斜杠)的结果。 I then started looking for a way to get powershell to take the \\ literally instead. 然后,我开始寻找一种使Powershell实际使用\\的方法。 That lead me to try this: 那导致我尝试这个:

$txt = import-csv paths1.csv | % {$_.localpath = $_.localpath -replace [Regex]::Escape("C:\\Users\\"), ""}

and this 和这个

$txt = import-csv paths1.csv | % {$_.localpath = $_.localpath -replace "C:\\Users\\", ""}

both of those methods stop the invalid regular expression errors and just return me a fresh line without complaining. 这两种方法都可以停止无效的正则表达式错误,并在没有抱怨的情况下给我换行。 however when I print the $txt variable it is empty... 但是,当我打印$txt变量时,它为空...

I'm certain I am approaching this problem from the wrong angle and/or with improper syntax but I could use some guidance as I just started working with powershell a week ago. 我确定我是从错误的角度和/或使用了不正确的语法来解决此问题,但是一周前刚开始使用Powershell时,我可以使用一些指导。

any help provided would be greatly appreciated. 提供的任何帮助将不胜感激。

The following will import the CSV file and then get the leaf of the path. 以下将导入CSV文件,然后获取路径的叶子。 Ie the user name. 即用户名。

$txt = Import-Csv paths1.csv | ForEach-Object { Split-Path $_.localpath -leaf }

If you still want to use your replace method, just take out the $_.localpath = part and it should work. 如果仍然要使用replace方法,只需取出$_.localpath =部分,它应该可以工作。

$txt = Import-Csv C:\@@Scatch\test.csv | % { $_.localpath -replace "C:\\Users\\", ""}

The reason why you aren't getting anything back into $txt is that you update a property of $_ but don't return $_ . 您没有将任何东西退回到$txt是,您更新了$_的属性,但不返回$_

Assuming that you want to use the regex rather than Split-Path 假设您要使用正则表达式而不是Split-Path

$txt = import-csv C:\temp\test.csv | % {
    $_.localpath = $_.localpath -replace "C:\\Users\\", ""
    $_
}

Or 要么

$txt = import-csv C:\temp\test.csv | % {
    $_.localpath -replace "C:\\Users\\", ""
}

其他解决方案

Get-Content "C:\temp\test.txt" | select @{N="Value";E={$_.split('\')[-1].replace('"', '')}} -Skip 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM