6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PowerShellでCSVデータの中身を書き換える

Last updated at Posted at 2015-01-09

##はじめに
歩引行を生成するサンプルを作って見ました。

行そのものを書き換えるスクリプトのサンプルはよくありますが、
CSVのデータ行を他の行に書き換えるようなサンプルはなかったので作って見ました。

##歩引金額を生成

金額(price)に歩引金額(discount_price)を入れる。
歩引なのでもちろん -1*dicount_priceしています。

$discount.price=-1*$discount.discount_price

の行に注目

discount.csv
id,detail_id,item,price,discount_per,discount_price
0,1,letsnote,2000,8,160
1,1,macbookair,1230,8,98.4
2,1,macbookpro,998,8,79.84
対話的に実行
PS >$discounts=Import-Csv -path discount.csv
PS >$discounts
id             : 0
detail_id      : 1
item           : letsnote
price          : 2000
discount_per   : 8
discount_price : 160
id             : 1
detail_id      : 1
item           : macbookair
price          : 1230
discount_per   : 8
discount_price : 98.4
id             : 2
detail_id      : 1
item           : macbookpro
price          : 998
discount_per   : 8
discount_price : 79.84

PS > foreach($discount in $discounts){$discount.price=-1*$discount.discount_price}
PS > $discounts

id             : 0
detail_id      : 1
item           : letsnote
price          : -160
discount_per   : 8
discount_price : 160
id             : 1
detail_id      : 1
item           : macbookair
price          : -98.4
discount_per   : 8
discount_price : 98.4
id             : 2
detail_id      : 1
item           : macbookpro
price          : -79.84
discount_per   : 8
discount_price : 79.84

##商品名も8%に変更
この要領で、itemも 8% に書き換えましょう。

8%を生成している部分
$discount.item="歩引"+$discount.discount_per+"%"

全体のソースコード

itemを歩引8%に書き換え
PS >$discounts
id             : 0
detail_id      : 1
item           : letsnote
price          : -160
discount_per   : 8
discount_price : 160
id             : 1
detail_id      : 1
item           : macbookair
price          : -98.4
discount_per   : 8
discount_price : 98.4
id             : 2
detail_id      : 1
item           : macbookpro
price          : -79.84
discount_per   : 8
discount_price : 79.84

PS > foreach($discount in $discounts){$discount.item="歩引"+$discount.discount_per+"%"}
PS > $discounts

id             : 0
detail_id      : 1
item           : 歩引8%
price          : -160
discount_per   : 8
discount_price : 160
id             : 1
detail_id      : 1
item           : 8%
price          : -98.4
discount_per   : 8
discount_price : 98.4
id             : 2
detail_id      : 1
item           : 8%
price          : -79.84
discount_per   : 8
discount_price : 79.84

生成した歩引行と実際の売上と合わせる時に、
歩引きが一番下に来るようにdetail_idに998を挿入しておきます。

detail_idに998を挿入
PS >$discounts | Where-Object { $_.detail_id -eq 1 } | ForEach { $_.detail_id = 998 }

これをExport-CsvでCSVデータに再び吐き出せます。

Export-CSV

PS > $discounts | Export-CSV -path discounts.csv -Delimter `t -Encoding UTF8

##参考サイト
csvファイルのインポート、エクスポートについて

WindowsPowerShellでCSVをDBっぽく扱う

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?