我正在嘗試用逗號替換所有空格以將我的檔案用作 CSV 輸入,這是一個示例輸入:
[Royal Gauntlets of Silvermoon] (1) Senhna 2500g
[Chestguard of the Vanquished Hero] (1) Neithia 3000g
[Chestguard of the Vanquished Hero] (1) Buddafly 3000g
這是我的預期輸出:
[Royal Gauntlets of Silvermoon],(1),Senhna,2500g,
[Chestguard of the Vanquished Hero],(1),Neithia,3000g,
[Chestguard of the Vanquished Hero],(1),Buddafly,3000g,
tr ' ' ',' <input >output 有效,但也替換了括號中的空格
我知道我可以用 awk 做到這一點,但我不確定如何做到這一點。謝謝!
uj5u.com熱心網友回復:
將 GNU awk 用于FPAT:
$ awk -v FPAT='[^ ]*|[[][^]] ]' -v OFS=',' '{$1=$1}1' file
[Royal Gauntlets of Silvermoon],(1),Senhna,2500g
[Chestguard of the Vanquished Hero],(1),Neithia,3000g
[Chestguard of the Vanquished Hero],(1),Buddafly,3000g
uj5u.com熱心網友回復:
如果第一個欄位是唯一的方括號,另一種解決方案
$ awk -F']' '{gsub(" ",",",$2); print $1 FS $2}' file
[Royal Gauntlets of Silvermoon],(1),Senhna,2500g
[Chestguard of the Vanquished Hero],(1),Neithia,3000g
[Chestguard of the Vanquished Hero],(1),Buddafly,3000g
在右方括號處分隔行,在第二部分用逗號替換單個空格并加入。
uj5u.com熱心網友回復:
如果sed是一個選項
sed 's/\(\[[^]]*]\|([^)]*)\|[a-z]*\) \|$/\1,/g' file
[Royal Gauntlets of Silvermoon],(1),Senhna,2500g,
[Chestguard of the Vanquished Hero],(1),Neithia,3000g,
[Chestguard of the Vanquished Hero],(1),Buddafly,3000g,
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/410227.html
標籤:
