回答編集履歴

3 修正:全角空白記述が半角空白になっていた。

Y.H.

Y.H. score 6282

2017/03/03 13:20  投稿

> if文のサイト等で中身を見ましたが分かりませんでした。
どこまでわかって何がわからなかったのでしょうか?
普通にif文かけますよ。
# `"NULL,hoge,2"`⇒`" ,hoge,2"`
# `"NULL,hoge,2"`⇒`" ,hoge,2"`
```
Get-Content "[ファイルパス&ファイル名.csv]" | % {
   if (($_.Split(",")[0] -eq 'NULL') -and ($_.Split(",")[2] -eq '2')){
       " " + "," +
       " " + "," +
       $_.Split(",")[1] + "," +
       $_.Split(",")[2] + ","
   } else {
       $_.Split(",")[0] + "," +
       $_.Split(",")[1] + "," +
       $_.Split(",")[2] + ","
   }
} | Out-File -Encoding default [ファイルパス&ファイル名.csv]
```
# `",hoge,2"`⇒`" ,hoge,2"`
# `",hoge,2"`⇒`" ,hoge,2"`
```
Get-Content "[ファイルパス&ファイル名.csv]" | % {
   if (($_.Split(",")[0] -eq '') -and ($_.Split(",")[2] -eq '2')){
       " " + "," +
       " " + "," +
       $_.Split(",")[1] + "," +
       $_.Split(",")[2] + ","
   } else {
       $_.Split(",")[0] + "," +
       $_.Split(",")[1] + "," +
       $_.Split(",")[2] + ","
   }
} | Out-File -Encoding default [ファイルパス&ファイル名.csv]
```
----
# テスト結果
```
PS C:\tmep> (',hoge,2', 'NULL,hoge,2', 'fuga,hoge,2')|%{
>>    if (($_.Split(",")[0] -eq 'NULL') -and ($_.Split(",")[2] -eq '2')){
>>        " " + "," +
>>        $_.Split(",")[1] + "," +
>>        $_.Split(",")[2] + ","
>>    } elseif (($_.Split(",")[0] -eq '') -and ($_.Split(",")[2] -eq '2')){
>>        " " + "," +
>>        $_.Split(",")[1] + "," +
>>        $_.Split(",")[2] + ","
>>    } else {
>>        $_.Split(",")[0] + "," +
>>        $_.Split(",")[1] + "," +
>>        $_.Split(",")[2] + ","
>>    }
>> }
>>
```
>  ,hoge,2,
>  ,hoge,2,
> fuga,hoge,2,
2 引用範囲修正

Y.H.

Y.H. score 6282

2017/03/03 13:18  投稿

> if文のサイト等で中身を見ましたが分かりませんでした。
 
どこまでわかって何がわからなかったのでしょうか?
普通にif文かけますよ。
# `"NULL,hoge,2"`⇒`" ,hoge,2"`
```
Get-Content "[ファイルパス&ファイル名.csv]" | % {
   if (($_.Split(",")[0] -eq 'NULL') -and ($_.Split(",")[2] -eq '2')){
       " " + "," +
       $_.Split(",")[1] + "," +
       $_.Split(",")[2] + ","
   } else {
       $_.Split(",")[0] + "," +
       $_.Split(",")[1] + "," +
       $_.Split(",")[2] + ","
   }
} | Out-File -Encoding default [ファイルパス&ファイル名.csv]
```
# `",hoge,2"`⇒`" ,hoge,2"`
```
Get-Content "[ファイルパス&ファイル名.csv]" | % {
   if (($_.Split(",")[0] -eq '') -and ($_.Split(",")[2] -eq '2')){
       " " + "," +
       $_.Split(",")[1] + "," +
       $_.Split(",")[2] + ","
   } else {
       $_.Split(",")[0] + "," +
       $_.Split(",")[1] + "," +
       $_.Split(",")[2] + ","
   }
} | Out-File -Encoding default [ファイルパス&ファイル名.csv]
```
----
# テスト結果
```
PS C:\tmep> (',hoge,2', 'NULL,hoge,2', 'fuga,hoge,2')|%{
>>    if (($_.Split(",")[0] -eq 'NULL') -and ($_.Split(",")[2] -eq '2')){
>>        " " + "," +
>>        $_.Split(",")[1] + "," +
>>        $_.Split(",")[2] + ","
>>    } elseif (($_.Split(",")[0] -eq '') -and ($_.Split(",")[2] -eq '2')){
>>        " " + "," +
>>        $_.Split(",")[1] + "," +
>>        $_.Split(",")[2] + ","
>>    } else {
>>        $_.Split(",")[0] + "," +
>>        $_.Split(",")[1] + "," +
>>        $_.Split(",")[2] + ","
>>    }
>> }
>>
```
>  ,hoge,2,
>  ,hoge,2,
> fuga,hoge,2,
1 回答追記

Y.H.

Y.H. score 6282

2017/03/03 12:33  投稿

> if文のサイト等で中身を見ましたが分かりませんでした。
どこまでわかって何がわからなかったのでしょうか?
普通にif文かけますよ。
# `"NULL,hoge,2"`⇒`" ,hoge,2"`
```
Get-Content "[ファイルパス&ファイル名.csv]" | % {
   if (($_.Split(",")[0] -eq 'NULL') -and ($_.Split(",")[2] -eq '2')){
       " " + "," +
       $_.Split(",")[1] + "," +
       $_.Split(",")[2] + ","
   } else {
       $_.Split(",")[0] + "," +
       $_.Split(",")[1] + "," +
       $_.Split(",")[2] + ","
   }
} | Out-File -Encoding default [ファイルパス&ファイル名.csv]
```
# `",hoge,2"`⇒`" ,hoge,2"`
```
Get-Content "[ファイルパス&ファイル名.csv]" | % {
   if (($_.Split(",")[0] -eq '') -and ($_.Split(",")[2] -eq '2')){
       " " + "," +
       $_.Split(",")[1] + "," +
       $_.Split(",")[2] + ","
   } else {
       $_.Split(",")[0] + "," +
       $_.Split(",")[1] + "," +
       $_.Split(",")[2] + ","
   }
} | Out-File -Encoding default [ファイルパス&ファイル名.csv]
```
```
----
# テスト結果
```
PS C:\tmep> (',hoge,2', 'NULL,hoge,2', 'fuga,hoge,2')|%{
>>    if (($_.Split(",")[0] -eq 'NULL') -and ($_.Split(",")[2] -eq '2')){
>>        " " + "," +
>>        $_.Split(",")[1] + "," +
>>        $_.Split(",")[2] + ","
>>    } elseif (($_.Split(",")[0] -eq '') -and ($_.Split(",")[2] -eq '2')){
>>        " " + "," +
>>        $_.Split(",")[1] + "," +
>>        $_.Split(",")[2] + ","
>>    } else {
>>        $_.Split(",")[0] + "," +
>>        $_.Split(",")[1] + "," +
>>        $_.Split(",")[2] + ","
>>    }
>> }
>>
```
>  ,hoge,2,
>  ,hoge,2,
> fuga,hoge,2,

思考するエンジニアのためのQ&Aサイト「teratail」について詳しく知る