我有一个用于解析Tnsnames.ora文件的函数。我的问题是有一个更好的awk语句/查询,它将给我我想要的结果。
这是我要优化的语句,因此我的代码段较短:
感谢“ BEGIN {found = 0} / $ {entry} / {found = 1} {如果(找到)打印}”)
我想摆脱for循环,并使用AWK搜索并找到单个TNSNAME。目前,我正在通过awk命令的结果进行计数,直到到达传递的名称的last)为止。
findTnsname()
{
#!/bin/ksh
#inputs:
# $1: stanza title
# $2: Fully qualified input filename
#$3 set to yes will echo how many were found
tnsnames=$(cat $2)
entries=$(echo "$tnsnames"| grep -i "$1.*="| awk '{print $1}')
eNames=$(echo "$entries" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/,/g')
ecount=$(echo "$entries" | wc -l | awk '{print $1}')
if [ "$3" = "yes" ] ; then
echo "# found ${ecount} entries - ${eNames} "
fi
for entry in $entries
{
#echo "#searching for ${entry}"
tt=$(echo "$tnsnames" | awk "BEGIN{found=0}/${entry}/{found=1} {if (found) print }")
t=0 # t for target, means the stanza has been found
open_parens=0
close_parens=0
if [[ -n $tt ]] ; then
echo "$tt" | while read -r i; do
if(( t == 1 )); then
echo "$i"
newline_ck=$(echo "$i" | egrep "^$" | wc -l | awk '{print $1}')
if(( newline_ck > 0 )); then
t=0
break
elif(( open_parens == close_parens && open_parens != 0 )); then
t=0
else
(( open_parens += $(echo "$i" | awk -F"(" '{print NF-1}') ))
(( close_parens += $(echo "$i" | awk -F")" '{print NF-1}') ))
#echo "# open ${open_parens} close ${close_parens} t ${t}"
fi
else
t=$(echo "$i" | egrep -i "^$entry" | wc | awk '{print $1}')
if(( t == 1 )); then
echo "$i"
fi
fi
done
else
echo "$entry does not exist in $2"
fi
}
}
结果如下所示: findTnsname abc“ $ ORACLE_HOME / network / admin / tnsnames.ora”
ABC_USR.some.company.com =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hosta.some.company.com)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ABC_usr)
)
)
ABCT.some.company.com =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostb.some.company.com)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ABCt)
)
)
样本输入
DEF_USR.some.company.com =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hosta.some.company.com)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = DEF_USR)
)
)
DEFT.some.company.com =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostb.some.company.com)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = DEFT)
)
)
DEF_USR.some.company.com =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostc.some.company.com)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = DEF_USR)
)
)
DEFT.some.company.com =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostd.some.company.com)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = DEFT)
)
)
GHI_USR.some.company.com =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hoste.some.company.com)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = GHI_USR)
)
)
GHIT.some.company.com =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostf.some.company.com)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = GHIT)
)
)
ABC_USR.some.company.com =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hosta.some.company.com)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ABC_usr)
)
)
ABCT.some.company.com =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostb.some.company.com)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ABCt)
)
)
答案 0 :(得分:1)
这可能是您要查找的内容,具体取决于我在问题下方的评论中询问的问题的答案:
如果您的输入记录之间总是有空白行(如您在一条注释中所建议),那么您所需要做的就是:
$ awk -v key='abc' -v RS= -v ORS='\n\n' 'tolower($0)~tolower(key)' file
否则,如果有时有时不如您提供的示例输入中所述:
$ cat tst.awk
!NF { next }
!/[()]/ { prt(); rec="" }
{ rec = rec $0 ORS }
END { prt() }
function prt() {
if ( tolower(rec) ~ tolower(key) ) {
print rec
}
}
$ awk -v key='abc' -f tst.awk file
ABC_USR.some.company.com =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hosta.some.company.com)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ABC_usr)
)
)
ABCT.some.company.com =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostb.some.company.com)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ABCt)
)
)
编辑-要进一步控制在何处搜索哪些字段以及打印内容,可以考虑采取其他措施:
$ cat tst.awk
BEGIN { FS=RS; RS=""; ORS="\n" }
{
delete f
f["NAME"] = $1
sub(/[[:space:]]*=.*/,"",f["NAME"])
for (i=2; i<=NF; i++) {
n = split($i,tmp,/[ =()]+/)
for (j=n-2; j>1; j-=2) {
f[tmp[j]] = tmp[j+1]
}
}
prt()
}
function prt() {
for (tag in f) {
print tag "=<" f[tag] ">"
}
print "----"
}
。
$ awk -f tst.awk file
LOAD_BALANCE=<yes>
HOST=<hosta.some.company.com>
PROTOCOL=<TCP>
NAME=<DEF_USR.some.company.com>
SERVICE_NAME=<DEF_USR>
SERVER=<DEDICATED>
PORT=<1521>
----
LOAD_BALANCE=<yes>
HOST=<hostb.some.company.com>
PROTOCOL=<TCP>
NAME=<DEFT.some.company.com>
SERVICE_NAME=<DEFT>
SERVER=<DEDICATED>
PORT=<1521>
----
LOAD_BALANCE=<yes>
HOST=<hostc.some.company.com>
PROTOCOL=<TCP>
NAME=<DEF_USR.some.company.com>
SERVICE_NAME=<DEF_USR>
SERVER=<DEDICATED>
PORT=<1521>
----
LOAD_BALANCE=<yes>
HOST=<hostd.some.company.com>
PROTOCOL=<TCP>
NAME=<DEFT.some.company.com>
SERVICE_NAME=<DEFT>
SERVER=<DEDICATED>
PORT=<1521>
----
LOAD_BALANCE=<yes>
HOST=<hoste.some.company.com>
PROTOCOL=<TCP>
NAME=<GHI_USR.some.company.com>
SERVICE_NAME=<GHI_USR>
SERVER=<DEDICATED>
PORT=<1521>
----
LOAD_BALANCE=<yes>
HOST=<hostf.some.company.com>
PROTOCOL=<TCP>
NAME=<GHIT.some.company.com>
SERVICE_NAME=<GHIT>
SERVER=<DEDICATED>
PORT=<1521>
----
LOAD_BALANCE=<yes>
HOST=<hosta.some.company.com>
PROTOCOL=<TCP>
NAME=<ABC_USR.some.company.com>
SERVICE_NAME=<ABC_usr>
SERVER=<DEDICATED>
PORT=<1521>
----
LOAD_BALANCE=<yes>
HOST=<hostb.some.company.com>
PROTOCOL=<TCP>
NAME=<ABCT.some.company.com>
SERVICE_NAME=<ABCt>
SERVER=<DEDICATED>
PORT=<1521>
----