我构建了一个函数来解析Tnsnames.ora文件。我的问题是有一个更好的awk语句/查询,可以给我我想要的结果。
这是我想要优化的语句,因此我将有一个更短的代码段:
awk "BEGIN{found=0}/${entry}/{found=1} {if (found( print }"(
我想摆脱 for 循环并使用 AWK 搜索并找到单个 TNSNAME。 目前,我正在计算 awk 命令的结果,直到我到达最后一个 ( 作为传递的名称。
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)
)
)
这可能是您正在寻找的,具体取决于我在问题下的评论中提出的问题的答案:
如果您的输入记录之间总是有空行,正如您在一条评论中建议的那样,那么您所需要的只是:
$ awk -v key='abc' -v RS= -v ORS='nn' '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>
----