我有以下文本行:
.add 7971 [Player: Sunfire (Account: 11309) X: 5839.623047 Y: 600.437439 Z: 650.839722 Map: 571 Selected player: Llubia (GUID: 19369)]
.add 43956 [Player: Sunfire (Account: 11309) X: 5277.887695 Y: 2862.181641 Z: 446.735931 Map: 571 Selected none: (GUID: 0)]
.add 43956 [Player: Sunfire (Account: 11309) X: 5281.407715 Y: 2864.844482 Z: 446.735931 Map: 571 Selected player: Staticbaby (GUID: 19826)]
.add 43956 [Player: Sunfire (Account: 11309) X: 5231.464844 Y: 1437.029175 Z: 648.498535 Map: 571 Selected player: Sunfire (GUID: 15295)]
.add 44077 [Player: Sunfire (Account: 11309) X: 5231.464844 Y: 1437.029175 Z: 648.498535 Map: 571 Selected player: Sunfire (GUID: 15295)]
.add 49285 [Player: Sunfire (Account: 11309) X: 16225.323242 Y: 16252.759766 Z: 12.790466 Map: 1 Selected none: (GUID: 0)]
.add 44115 175 [Player: Elmasguapo (Account: 11309) X: 1659.845093 Y: -4198.589844 Z: 56.382870 Map: 1 Selected none: (GUID: 0)]
.add 34078 [Player: Sunfire (Account: 11309) X: 16227.969727 Y: 16280.081055 Z: 13.175169 Map: 1 Selected none: (GUID: 0)]
.add |cffffffff|Hitem:41427:0:0:0:0:0:0:0:80|h[Fuego de Artificio de Dalaran]|h|r 50 [Player: Sunfire (Account: 11309) X: 16221.392578 Y: 16260.944336 Z: 13.255954 Map: 1 Selected none: (GUID: 0)]
.add |cffffffff|Hitem:45932:0:0:0:0:0:0:0:80|h[Gelatina Negra]|h|r [Player: Sunfire (Account: 11309) X: 5874.347168 Y: 679.056763 Z: 167.483719 Map: 571 Selected player: Assasins (GUID: 19438)]
.add |cffffffff|Hitem:45932:0:0:0:0:0:0:0:80|h[Gelatina Negra]|h|r [Player: Sunfire (Account: 11309) X: 5873.767090 Y: 679.386841 Z: 167.435257 Map: 571 Selected player: Assasins (GUID: 19438)]
.add |cffffffff|Hitem:45932:0:0:0:0:0:0:0:80|h[Gelatina Negra]|h|r [Player: Sunfire (Account: 11309) X: 16226.880859 Y: 16247.247070 Z: 12.286857 Map: 1 Selected player: Irmtarget (GUID: 18521)]
.add |cffffffff|Hitem:45932:0:0:0:0:0:0:0:80|h[Gelatina Negra]|h|r [Player: Sunfire (Account: 11309) X: 16229.297852 Y: 16251.202148 Z: 13.081388 Map: 1 Selected player: Irmtarget (GUID: 18521)]
.add 41600 2 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]
.add 41600 1 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]
.add 40516 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]
.add 44661 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]
.add 40518 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]
.add 44005 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]
.add 45867 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]
.add 45316 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]
我想解析它以便输出如下内容:
第 1 行 例如 - 7971 Llubia
第 3 行 例如 - 43956 Staticbaby
第 9 行 例如 - 45932 Assassins
等等.. 这可以在终端中使用 cut、grep、cat 等命令完成。
更新:这是整个文件:http://paste2.org/p/1744102以此方式解析。
更新 2:如果我暂时不接受答案,请原谅我。我正在等待悬赏选项出现,因为对我来说,像这样的东西值得悬赏。所以我会添加悬赏并将其提供给正确的答案。
答案1
awk '
# ignore lines with "none:" or "player:" in $(NF-2)
$(NF-2) ~ /^(none|player):$/ {
next # read next line
}
{
# remove dots from $2 in every line
gsub(/\./,"",$2)
}
/\|Hitem:/ {
# if it has "|Hitem:" in $2, remove everything
# before and after the colons
sub(/.+\|Hitem:/,"",$2)
sub(/:.+/,"",$2)
print $2,$(NF-2)
next # read next line
}
{
print $2,$(NF-2)
}
' your_file.txt
第 380 行有Selected player: (GUID: 6873)]
。您必须决定如何处理这些行(忽略它们?而是打印 GUID?)。
答案2
这能是你想要的吗?
grep -v 'Selected none' input-file | awk '$2+0 > 0 { print $2, $(NF-2) }'
答案3
使用命令行工具来解析不规则输出实际上并不可行。最好使用 python 等。
例如,如果你有完整的规律性,那么你可以使用如下简单的方法:
awk '{print $2 " " $17}' INPUT
这遗漏了您示例中的“Hitem”行。
这是一个丑陋的黑客,但它考虑到了'Hitem'行:
sed 's/^.*Hitem:\([0-9]*\):0/\1/' INPUT | awk -F: '{print $1 " " $0}' | sed 's/\.add //' | sed 's/^\([0-9]* \).*Selected player: \([A-Za-z]* \).*$/\1 \2/' | egrep -v 'Selected none'
这给了我:
7971 Llubia
43956 Staticbaby
43956 Sunfire
44077 Sunfire
45932 Assasins
45932 Assasins
45932 Irmtarget
45932 Irmtarget
41600 Eifreen
41600 Eifreen
40516 Eifreen
44661 Eifreen
40518 Eifreen
44005 Eifreen
45867 Eifreen
45316 Eifreen
但是,由于您添加的附加信息,这失败了,这就是我在这里添加有关规律性的评论/警告的原因。