2

次のような .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)

しかし、うまくいきません。私はpowershellに不慣れで、正規表現をあまり使用しないので、おそらく初心者の間違いを犯したでしょう...解決策はありますか?

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 に答える