【发布时间】:2015-12-07 23:49:12
【问题描述】:
我不确定是否可以完成以下任务。我已经完成了它的第一部分,所以我在这里放了一些我想进一步使用的代码。这个问题可能有点长,抱歉。我想编辑一个如下所示的 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<notes version="1">
<labels>
<label id="0" color="80FF80">BadReg</label>
<label id="1" color="FFFF40">GoodReg</label>
<label id="2" color="8000FF">Bluffer</label>
<label id="3" color="10FF10">Fish</label>
<label id="4" color="1080FF">Loose</label>
<label id="5" color="30DBFF">Semi-Loose</label>
<label id="6" color="0000FF">Nit</label>
<label id="7" color="FF9F48">Tight</label>
<label id="8" color="C0C0C0">Undefined</label>
</labels>
<note player="00aquitis00" label="7" update="1429834930"></note>
<note player="01 LUGAR" label="8" update="1429834930"></note>
<note player="07 Star 07" label="4" update="1429834930"></note>
<note player="109185" label="6" update="1429834930">Some notes are here</note>
<note player="1111167" label="8" update="1429834930">And some there...</note>
</notes>
我想在上面的 *.xml 文件中编辑某些玩家的 id 和他们的标签编号。首先我有一个 player.txt 和一个 ReportExport.csv 文件,它们看起来像这样,CSV:
"","Player","VPIP","PFR","Hands","Players"
"2532|100|""Seikei""&5RS","""Seikei""&5RS","20.45","12.50","88","5.65"
"4265|100|#23Mattingly","#23Mattingly","41.94","25.81","33","5.55"
"2748|100|#PachnacyBak","#PachnacyBak","11.54","10.00","52","5.44"
"6|100|eagle189","eagle189","20.60","13.60","73,561","5.55"
文本:
player1 badreg
player2 badreg
player3 bluffer
player4 bluffer
在第一步中,我从 *.txt 和 *.csv 文件中获取数据(我想要使用的数据)。 AWK 代码:
BEGIN {
update = 1429834930 }
FILENAME == "players.txt" {
FS = "\t\t"
playertype = $2;
if (playertype == "badreg") {badregs[$1]++};
if (playertype == "bluffer") {bluffers[$1]++};
if (playertype == "fish") {fishes[$1]++};
if (playertype == "goodreg") {goodregs[$1]++};
next }
(FILENAME == "ReportExport.csv") && (FNR != 1) {
FS = "\",\""
name = $2;
vpip = $3;
pfr = $4;
hands = $5;
avgp = substr($6, 1, (length($6)-1));
if (name == "") {name = "Name"}
gsub(/&/, "\\&", name)
gsub(/</, "\\<", name)
gsub(/>/, "\\>", name)
gsub(/\"\"\"/, "\"\"", name)
gsub(/\"\"/, "\"", name)
gsub(/\"/, "\\"", name)
gsub(/,/, "", hands)
label = 8;
if ((hands >= 100) && ((vpip / 1.40) <= (100 / avgp))) {label = 7} #Tight
if ((hands >= 18) && ((vpip / 1.00) <= (100 / avgp))) {label = 7} #Tight
if ((hands >= 100) && ((vpip / 1.25) <= (100 / avgp))) {label = 6} #Nit
if ((hands >= 24) && ((vpip / 2.20) >= (100 / avgp))) {label = 5} #Semi-Loose
if ((hands >= 15) && ((vpip / 3.00) >= (100 / avgp))) {label = 4} #Loose
if ((hands >= 30) && ((vpip / 2.60) >= (100 / avgp))) {label = 4} #Loose
if ((hands >= 24) && ((vpip / 2.60) >= (100 / avgp)) && ((pfr/vpip) <= 0.2)) {label = 3} #Fish
if (name in fishes) {label = 3} #Fish
if (name in bluffers) {label = 2} #Bluffer
if (name in goodregs) {label = 1} #GoodReg
if (name in badregs) {label = 0} #BadReg
}
在此之后,当我终于找到了适合播放器的标签类型时,我想在 *.xml 文件中进行搜索,如果可以找到该播放器,那么我想更新他的标签号码,如果找不到他,我想在其他玩家旁边打印一行到 *.xml 文件中,如下所示:
printf("\t<note player=\"%s\" label=\"%s\" update=\"%s\"></note>\n", name, label, update)
然后保存 *.xml 文件并在处理 csv 文件时逐行再次执行此操作。在第一种情况下,如果可以找到播放器,重要的是不要替换 *.xml 文件中的整行,只需更新标签,因为某些播放器还可以包含一些注释(如示例代码中)如果我们更换整条生产线,就会迷路。所以真正的问题是,这些替换可以首先完成吗?如果他们可以的话,如果有人能告诉我我应该在哪里继续,我将不胜感激:)当然,如果有更好的方法而不是逐行保存和搜索,我很想听听。
【问题讨论】:
-
必须用awk来完成吗?我认为xsl transformation 更好
-
学习使用难吗?理论上它将如何完成这项工作?如果不是太难,我很感兴趣。
-
如果您的
awk脚本不适合页面,则说明您使用了错误的工具来完成任务。