我在我的数据集上尝试了这个小脚本,但出于某种原因我没有得到想要的输出?有人可以看看吗?也许你能弄清楚?另外,如果您可以提供 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
最佳答案
尝试:
awk '/connection/{match($0,/"[^"]*/);VAL=substr($0,RSTART+1,RLENGTH-1);next} /hostPort/ && VAL{match($0,/>.*</);print VAL FS substr($0,RSTART+1,RLENGTH-2)}' Input_file
稍后将添加说明。
EDIT2:以下是相同的解释。
awk '/connection/{ #### Looking for a line which has string connection in it.
match($0,/"[^"]*/); #### Using match function here to match a regex where it starts from " and looks for first occurrence of ".
VAL=substr($0,RSTART+1,RLENGTH-1); #### Now creating a variable named VAL whose value is substring of RSTART and LENGTH, where RLENGTH and RSTART are the default keywords of awk and they will be SET when a REGEX match is found. RSTART will give the index of starting point of match and RLENGTH will give the length of that regex match.
next #### Using next keyword to skip all further statements.
}
/hostPort/ && VAL{ #### Checking here 2 conditions, it checks for a line which has hostport string and value of variable VAL is NOT NULL, if these conditions are TRUE then perform following actions.
match($0,/>.*</); #### using match function of awk to get the srv values so putting here regex so match from >.*< get everything between > to <.
print VAL FS substr($0,RSTART+1,RLENGTH-2) #### printing value of VAL(which got created in previous condition) then printing the substring of RSTART and RLENGTH values here.
}
' Input_file #### Mentioning the Input_file here.
关于linux - Awk 命令无法正常工作,输出错误,sed 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44362965/