PowerShell regex仅匹配.csv中每行中的第一个出现

PowerShell regex仅匹配.csv中每行中的第一个出现,regex,powershell,csv,Regex,Powershell,Csv,假设我有一个.csv文件,看起来有点像: "first_foo","first_foo" "first_bar","first_bar" "second_foo","second_foo" "second_bar","second_bar" 使用PS脚本,我想让它看起来像这样: "first","first_foo" "first","first_bar" "second","second_foo" "second","second_bar" $regex=[regex]"first*";

假设我有一个.csv文件,看起来有点像:

"first_foo","first_foo"
"first_bar","first_bar"
"second_foo","second_foo"
"second_bar","second_bar"
使用PS脚本,我想让它看起来像这样:

"first","first_foo"
"first","first_bar"
"second","second_foo"
"second","second_bar"
$regex=[regex]"first*";
$regex.Replace('"first_foo","first_foo"',"first",1)
我在考虑使用这样的东西:

"first","first_foo"
"first","first_bar"
"second","second_foo"
"second","second_bar"
$regex=[regex]"first*";
$regex.Replace('"first_foo","first_foo"',"first",1)
但它不起作用。我是powershell新手,不经常使用正则表达式,所以我可能犯了noob错误。。。
你们有解决方案吗?

看起来内容没有标题,可以在运行时添加标题。此示例替换col1值并从字符串末尾删除“\u foo”或“\u bar”:

Import-Csv test.csv -Header col1,col2 | Foreach-Object {
    $_.col1 = $_.col1 -replace '(_foo|_bar)$'
        $_
}

col1   col2      
----   ----      
first  first_foo 
first  first_bar 
second second_foo
second second_bar

当你说“它不起作用”时,你能详细说明一下吗?它起什么作用?