我在我的數據集上試過這個小腳本,出於某種原因,我沒有得到所需的輸出結果?有人可以看看嗎?也許你可以弄明白嗎?另外如果你可以提供SED命令解決方案。awk命令不能正常工作,輸出錯誤,sed命令?
腳本
awk -v RS= -F '<connection name="|<hostPort>' '
{
sub(/".*/, "", $2)
split($3, tokens, /[:<]/)
printf "%-6s %s %s\n", $2, tokens[1], tokens[2]
}
'
輸入
<hostPort>srv1:33333</hostPort>
<hostPort>srv2:33333</hostPort>
<connection name="boing_ny__Primary__" transport="tcp">
<hostPort>srv1:33333</hostPort>
<connection name="boing_ny__Backup__" transport="tcp">
<hostPort>srv2:33333</hostPort>
<connection name="boy_ny__Primary__" transport="tcp">
<hostPort>srv1:6666</hostPort>
<connection name="boy_ny__Backup__" transport="tcp">
<hostPort>srv2:6666</hostPort>
<connection name="song_ny__Primary__" transport="tcp">
<hostPort>srv1:55555</hostPort>
<connection name="song_ny__Backup__" transport="tcp">
<hostPort>srv2:55555</hostPort>
<connection name="bob_ny__Primary__" transport="tcp">
<hostPort>srv3:33333</hostPort>
<connection name="bob_ny__Backup__" transport="tcp">
<hostPort>srv4:33333</hostPort>
<hostPort>srv1:4444</hostPort>
<hostPort>srv2:4444</hostPort>
<hostPort>srv1:4444</hostPort>
電流輸出
srv1:33333</hostPort>
srv2 33333
期望的輸出
boing_ny__Primary__ srv1 33333
boing_ny__Backup__ srv2 33333
boy_ny__Primary__ srv1 6666
boy_ny__Backup__ srv2 6666
song_ny__Primary__ srv1 55555
song_ny__Backup__ srv2 55555
bob_ny__Primary__ srv3 33333
bob_ny__Backup__ srv4 33333
使用XML解析器不AWK/SED – anubhava
.UP給你測試/使用/投擲'AWK -v RS = ''「{$ 1 = $ 1 ;匹配($ 0,/ connection name =「([^」] +)。*(。*)/,a); gsub(/:/,「」,a [2]);} length(a [1 ]){print a [1],a [2]}' –
請用散文形容你的小腳本的目的。這將幫助讀者理解代碼(或其中的錯誤),並可能幫助您找到問題。使用各種角度來理解你自己的代碼和它的不當行爲。它類似於https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Yunnosch