powershell 替换文本通配符
powershell replace text wildcard
我有一个 csv 导出文件,我可以从中获取一些信息。在第 1 行和第 2 行以下的 4 行中,没有问题。
第 3 行和第 4 行需要删除“2 x”,而且第 4 行我只希望省略逗号(包括逗号)后的任何名称 henrietta。
我想用通配符代替数字
Linda
bobbyjoe
2 x ash
3 x Henrietta,suzyann,waynebruce,parkerpeter
我目前使用如下。只是,我还没有解决部分问题我还没有在网上找到我理解的答案。
$filepath = "c:\mycsv.csv"
$bad2 = "2 x "
$good2 = `b
$bad2 = "3 x "
$good2 = `b
get-content $filepath | % { $_ -replace $bad2 , $good2 } | % { $_ -replace $bad3 , $good3 } | set-content $saveloc\tidied-$date.csv
你熟悉正则表达式吗?请参阅 regex, and have a look at Reference - What does this regex mean?
的标签 wiki
PowerShell 中的 -replace
运算符使用正则表达式替换,因此很容易替换“(任何数字)(space)(文字 x)(space)”:
$bad2 = '\d x '
# or
$bad2 = '[0-9] x '
# better
$bad2 = '^\d x '
get-content $filepath | % { $_ -replace $bad2, $good2 }
更好的版本将匹配锚定到字符串的开头;这就是插入符号 ^
的作用。
正则表达式非常强大,PowerShell 对它们有很好的支持。
我有一个 csv 导出文件,我可以从中获取一些信息。在第 1 行和第 2 行以下的 4 行中,没有问题。
第 3 行和第 4 行需要删除“2 x”,而且第 4 行我只希望省略逗号(包括逗号)后的任何名称 henrietta。
我想用通配符代替数字
Linda
bobbyjoe
2 x ash
3 x Henrietta,suzyann,waynebruce,parkerpeter
我目前使用如下。只是,我还没有解决部分问题我还没有在网上找到我理解的答案。
$filepath = "c:\mycsv.csv"
$bad2 = "2 x "
$good2 = `b
$bad2 = "3 x "
$good2 = `b
get-content $filepath | % { $_ -replace $bad2 , $good2 } | % { $_ -replace $bad3 , $good3 } | set-content $saveloc\tidied-$date.csv
你熟悉正则表达式吗?请参阅 regex, and have a look at Reference - What does this regex mean?
的标签 wikiPowerShell 中的 -replace
运算符使用正则表达式替换,因此很容易替换“(任何数字)(space)(文字 x)(space)”:
$bad2 = '\d x '
# or
$bad2 = '[0-9] x '
# better
$bad2 = '^\d x '
get-content $filepath | % { $_ -replace $bad2, $good2 }
更好的版本将匹配锚定到字符串的开头;这就是插入符号 ^
的作用。
正则表达式非常强大,PowerShell 对它们有很好的支持。