シナリオ:
アプリケーション ( Wpkg、 command )の出力を再フォーマットしようとしていますwpkg.js /query:a
。私は標準のコマンドラインツールに限定されています。
コマンド出力は、ソフトウェア パッケージのタイトルとそのパラメーターの繰り返しブロックで構成されます。
Mozilla Firefox
ID: mozilla-firefox
Revision: 23.0.1.1
Reboot: false
Execute: -
Priority: 100
Status: Installed
Mozilla Thunderbird
ID: mozilla-thunderbird
Revision: 17.0.8.1
Reboot: false
Execute: -
Priority: 100
Status: Installed
これにより、読みにくい長い出力が作成されます。また、すべての情報は必要ありません。
したがって、ブロックごとに、興味深い情報を 1 つの変数に収集し、それを 1 行に出力して、次のような結果を得ようとしています。
Mozilla Firefox mozilla-firefox 23.0.1.1 Installed
Mozilla Thunderbird mozilla-thunderbird 17.0.8.1 Installed
問題:
それを達成するために、まず次のコードを使用して出力を解析し始めました。
rem Split all lines into two tokens
@for /f "tokens=1,2" %%G IN ('%WPKG_CMD% /query:a') DO @(
rem With first token, check what to do:
rem I can get known strings (Store, Ignore)
rem or I can get package name (NewLine)
@set WpkgListAll_ToDo=NewLine
@if [%%G]==[ID:] set WpkgListAll_ToDo=Store
@if [%%G]==[Revision:] set WpkgListAll_ToDo=Store
@if [%%G]==[Reboot:] set WpkgListAll_ToDo=Ignore
@if [%%G]==[Execute:] set WpkgListAll_ToDo=Ignore
@if [%%G]==[Priority:] set WpkgListAll_ToDo=Ignore
@if [%%G]==[Status:] set WpkgListAll_ToDo=Store
rem Echo for debug purpose
@echo [%%G][%WpkgListAll_ToDo%]
rem Next follows some unimportant code
)
上記のコードから、次のデバッグ出力が完全に期待されます。
[Mozilla][NewLine]
[Revision:][Store]
[Reboot:][Ignore]
[Execute:][Ignore]
[Priority:][Ignore]
[Status:][Store]
[Mozilla][NewLine]
[ID:][Store]
[Revision:][Store]
[Reboot:][Ignore]
[Execute:][Ignore]
[Priority:][Ignore]
[Status:][Store]
私が得るのは...:
[Mozilla][]
[Revision:][]
[Reboot:][]
[Execute:][]
[Priority:][]
[Status:][]
[Mozilla][]
[ID:][]
[Revision:][]
[Reboot:][]
[Execute:][]
[Priority:][]
[Status:][]
...そして、その理由を理解しようと懸命に努力してきましたが、失敗しました。
質問:
出力が失敗する理由と、それを正しく行うために何をすべきかを誰かが説明できますか?