2

Let's say I have a .csv file that looks a little like that:

"first_foo","first_foo"
"first_bar","first_bar"
"second_foo","second_foo"
"second_bar","second_bar"

And with a PS script I'd like to make it look like this:

"first","first_foo"
"first","first_bar"
"second","second_foo"
"second","second_bar"

I was thinking about using something like this:

$regex=[regex]"first*";
$regex.Replace('"first_foo","first_foo"',"first",1)

But it doesn't work. I'm new to powershell and don't use regex so often so I probably made noob mistakes... Do you guys have a solution?

4

1 回答 1

1

看起来内容没有标题,您可以在运行时添加它们。此示例替换 col1 值并从字符串末尾删除 '_foo' 或 '_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
于 2012-06-13T10:12:47.700 回答