質問編集履歴

1

リプレイス失敗の後始末。めんどくせえ

2023/01/17 16:11

投稿

can110
can110

スコア38266

test CHANGED
File without changes
test CHANGED
@@ -1,175 +1,88 @@
1
- ###前提・実現したいこと
1
+ ### 前提・実現したいこと
2
-
3
-
4
2
 
5
3
  PowerShell(Win10x64上)にて`.xml`ファイルを入力、加工し`.tsv`(タブ区切り)出力しています。
6
4
 
5
+ 入力XML(test.xml)
6
+ ```xml
7
+ <?xml version="1.0" encoding="UTF-8"?>
8
+ <results version="2">
9
+ <errors>
10
+ <error id="unreadVariable">
11
+ <location file="func.cpp" line="10"/>
12
+ </error>
13
+ <error id="redundantAssignment">
14
+ <location file="main.cpp" line="36"/>
15
+ <location file="main.cpp" line="34"/>
16
+ </error>
17
+ </errors>
18
+ </results>
19
+ ```
20
+ XMLデータは、`error`要素内に`location`要素が1個以上、複数存在しえます。
21
+ このXMLから`location.file`, `location.line`, `error.id` 列で構成されたTSVを出力したいです。
7
22
 
23
+ ### 試したこと
8
24
 
25
+ 以下のようにいくつか`Select`を試してみましたが、locationの内容が欠けたり、先頭の1文字しか出力できません。
9
- 入力XML(test.xml)
26
+ ```PowerShell
27
+ $xmlRes = [xml](Get-Content test.xml)
10
28
 
29
+ # 方法1 : locationをそのまま出力
11
- ```xml
30
+ # 結果 : 連結して出力される
31
+ $res1 = $xmlRes.results.errors.error | Select @{Name="file"; Expression={$_.location.file}},@{Name="line"; Expression={$_.location.line}},id
12
32
 
33
+ # 方法2 : location[0]を出力
13
- <?xml version="1.0" encoding="UTF-8"?>
34
+ # 結果 : location1個だと、location列データが出力されない。
35
+ $res2 = $xmlRes.results.errors.error | Select @{Name="file"; Expression={$_.location[0].file}},@{Name="line"; Expression={$_.location[0].line}},id
14
36
 
15
- <results version="2">
37
+ # 方法3 : location.file[0], line[0]を出力
38
+ # 結果 : location1個だと、先頭の1文字?しか出力されない。
39
+ $res3 = $xmlRes.results.errors.error | Select @{Name="file"; Expression={$_.location.file[0]}},@{Name="line"; Expression={$_.location.line[0]}},id
16
40
 
17
- <errors>
41
+ echo "res1---"
42
+ echo ($res1 | Select file,line,id)
43
+ $res1 | Export-Csv -Path res1.tsv -NoTypeInformation -Delimiter "`t"
18
44
 
19
- <error id="unreadVariable">
45
+ echo "res2---"
46
+ echo ($res2 | Select file,line,id)
47
+ $res2 | Export-Csv -Path res2.tsv -NoTypeInformation -Delimiter "`t"
20
48
 
21
- <location file="func.cpp" line="10"/>
22
-
23
- </error>
24
-
25
- <error id="redundantAssignment">
26
-
27
- <location file="main.cpp" line="36"/>
28
-
29
- <location file="main.cpp" line="34"/>
30
-
31
- </error>
32
-
33
- </errors>
34
-
35
- </results>
49
+ echo "res3---"
36
-
50
+ echo ($res3 | Select file,line,id)
51
+ $res3 | Export-Csv -Path res3.tsv -NoTypeInformation -Delimiter "`t"
37
52
  ```
38
53
 
54
+ 実行結果
55
+ ```
39
- XMLデータは、`error`要素内に`location`要素が1個以上、複数存在しえます。
56
+ res1---
40
57
 
41
- このXMLから`location.file`, `location.line`, `error.id` 列で構成されたTSVを出力したいです。
42
-
43
-
44
-
45
- ###試したこと
46
-
47
-
48
-
49
- 以下のようにいくつか`Select`を試してみましたが、locationの内容が欠けたり、先頭の1文字しか出力できません。
50
-
51
- ```PowerShell
52
-
53
- $xmlRes = [xml](Get-Content test.xml)
54
-
55
-
56
-
57
- # 方法1 : locationをそのまま出力
58
-
59
- # 結果 : 連結して出力される
60
-
61
- $res1 = $xmlRes.results.errors.error | Select @{Name="file"; Expression={$_.location.file}},@{Name="line"; Expression={$_.location.line}},id
62
-
63
-
64
-
65
- # 方法2 : location[0]を出力
66
-
67
- # 結果 : location1個だと、location列データが出力されない。
68
-
69
- $res2 = $xmlRes.results.errors.error | Select @{Name="file"; Expression={$_.location[0].file}},@{Name="line"; Expression={$_.location[0].line}},id
70
-
71
-
72
-
73
- # 方法3 : location.file[0], line[0]を出力
74
-
75
- # 結果 : location1個だと、先頭の1文字?しか出力されない。
76
-
77
- $res3 = $xmlRes.results.errors.error | Select @{Name="file"; Expression={$_.location.file[0]}},@{Name="line"; Expression={$_.location.line[0]}},id
78
-
79
-
80
-
81
- echo "res1---"
82
-
83
- echo ($res1 | Select file,line,id)
58
+ file line id
84
-
85
- $res1 | Export-Csv -Path res1.tsv -NoTypeInformation -Delimiter "`t"
86
-
87
-
88
-
89
- echo "res2---"
90
-
91
- echo ($res2 | Select file,line,id)
59
+ ---- ---- --
92
-
93
- $res2 | Export-Csv -Path res2.tsv -NoTypeInformation -Delimiter "`t"
94
-
95
-
96
-
97
- echo "res3---"
98
-
99
- echo ($res3 | Select file,line,id)
60
+ func.cpp 10 unreadVariable
100
-
61
+ {main.cpp, main.cpp} {36, 34} redundantAssignment
62
+ res2---
63
+ unreadVariable
101
- $res3 | Export-Csv -Path res3.tsv -NoTypeInformation -Delimiter "`t"
64
+ main.cpp 36 redundantAssignment
102
-
65
+ res3---
66
+ f 1 unreadVariable
67
+ main.cpp 36 redundantAssignment
103
68
  ```
104
69
 
70
+ 欲しい結果は以下のいずれかです。
71
+ ```
72
+ # 結果1 : 先頭のlocationのみを出力
73
+ file line id
74
+ ---- ---- --
75
+ func.cpp 10 unreadVariable
76
+ main.cpp 36 redundantAssignment
105
77
 
106
-
107
- 実行結果
78
+ # 結果2 : locationまで展開して出力
108
-
79
+ file line id
80
+ ---- ---- --
81
+ func.cpp 10 unreadVariable
82
+ main.cpp 36 redundantAssignment
83
+ main.cpp 34 redundantAssignment
109
84
  ```
110
85
 
111
- res1---
112
-
113
-
114
-
115
- file line id
116
-
117
- ---- ---- --
118
-
119
- func.cpp 10 unreadVariable
120
-
121
- {main.cpp, main.cpp} {36, 34} redundantAssignment
122
-
123
- res2---
124
-
125
- unreadVariable
126
-
127
- main.cpp 36 redundantAssignment
128
-
129
- res3---
130
-
131
- f 1 unreadVariable
132
-
133
- main.cpp 36 redundantAssignment
134
-
135
- ```
136
-
137
-
138
-
139
- 欲しい結果は以下のいずれかです。
140
-
141
- ```
142
-
143
- # 結果1 : 先頭のlocationのみを出力
144
-
145
- file line id
146
-
147
- ---- ---- --
148
-
149
- func.cpp 10 unreadVariable
150
-
151
- main.cpp 36 redundantAssignment
152
-
153
-
154
-
155
- # 結果2 : locationまで展開して出力
156
-
157
- file line id
158
-
159
- ---- ---- --
160
-
161
- func.cpp 10 unreadVariable
162
-
163
- main.cpp 36 redundantAssignment
164
-
165
- main.cpp 34 redundantAssignment
166
-
167
- ```
168
-
169
-
170
-
171
- ###補足情報
86
+ ### 補足情報
172
-
173
87
  - 元データは、CUI版`cppcheck`で出力したものです。
174
-
175
88
  - 方法1で作成できる`.tsv`は行列データずれもなく`excel`で開けるので、できればといった感じの質問です。