こんにちは。
とあるCSVファイルがあり、以下のようにUsername、とAssigned_toという二つのコラムがあり、その下に
ユーザ名と、 ”@{display_value=Tom John; link=https://contoso.service-now.com/api/now/table/sys_user/a7bc02d2db48b4184f08b29a689619ed” というような複数の行のデータがあります。
Username Assigned_to
Taro @{display_value=Taro Yamada; link=https://contoso.service-now.com/api/now/table/sys_user/a7bc02d2db48b4184f08b29a689619ed
Hanako @{display_value=Hanako Tanaka; link=https://contoso.service-now.com/api/now/table/sys_user/b7bc02d2db48b4184f08b29a68ahgb22
これで、最後の31文字部分だけを残して、 a7bc02d2db48b4184f08b29a689619ed その前の文字列を全て消して
違うCSVファイルに出力したいのですが、どのようにすればよいかご存じの方、ご教授頂けないでしょうか。色々試していますがうまくできません。
Set the path to CSV file
$csvFilePath = "./test1.csv"
Read the CSV file
$csvData = Import-Csv -Path $csvFilePath
Define a regular expression pattern to match the desired format
$pattern = '@{display_value=.*/(\w{31})}'
Loop through each row in the CSV data
foreach ($row in $csvData) {
Extract the last 31 digits using the regular expression pattern
if ($row.assigned_to -match $pattern) {
$lastThirtyOneDigits = $matches[1]
$row.assigned_to = $lastThirtyOne
}
}
$csvData | Export-Csv -Path ./test2.csv -NoTypeInformation
#------------------------------------------------------------------
Set the path to CSV file
$csvFilePath = "./test1.csv"
Read the CSV file
$csvData = Import-Csv -Path $csvFilePath
$pattern = '@{display_value=.*(\d{31})$'
Loop through each row in the CSV data
foreach ($row in $csvData) {
Extract the last 31 digits using the regular expression pattern
if ($row.URL -match $pattern) {
$lastThirtyOneDigits = $matches[31]
$row.URL = $lastThirtyOne
}
}
$csvData | Export-Csv -Path ./test2.csv -NoTypeInformation
どうぞ宜しくお願い致します

回答3件
あなたの回答
tips
プレビュー