我的文件中有数据,其模式如下所示 -
TRSSOEC TRBSOEC ADICOES DELETE TROEC
TRSSOEC TRBSOEC ADICOES INSERT TROEC
TRSSOEC TRBSOEC ADICOES SELECT TROEC
SOBCXYZ SCHEMA1 AGENTES_CARGA INSERT TROEC
SOBCXYZ SCHEMA1 AGENTES_CARGA SELECT TROEC
YASU999 ASDF123 BS_ADICOES_SISCOMEX UPDATE TRBSOEC
USER123 WAREHOU BS_ADICOES_TRIBUTOS_BKP DELETE TRBSOEC
USER123 WAREHOU BS_ADICOES_TRIBUTOS_BKP INSERT TRBSOEC
我想将行与字段 1、2、3 进行比较,如果它们相同,则捕获字段 4 进行显示,如下所示 -
grant DELETE,INSERT,SELECT on TRBSOEC.ADICOES to TRSSOEC;
grant INSERT,SELECT on SCHEMA1.AGENTES_CARGA to SOBCXYZ;
grant UPDATE on ASDF123.BS_ADICOES_SISCOMEX to YASU999;
grant DELETE,INSERT on WAREHOU.BS_ADICOES_TRIBUTOS_BKP to USER123;
基本逻辑是根据字段 1、2、3 进行分组,并打印不同的字段 4 以及其他公共字段。
授予
到目前为止,我在下面尝试过,但不确定如何捕获字段 4,任何指导都会有帮助。
awk '
# Each line
{
if ($1 == prev[1] && $2 == prev[2] && $3 == prev[3])
{
# Capture and use field 4 of last match
if(!nrMatched) { nrMatched = prev[4] }
print "grant " $nrMatched " on " $2"."$3 " to " $1";"
}
else
{
# No match, reset matched flag and just copy record through
nrMatched = 0
}
# Save fields from this line for next comparison
split($0, prev)
}
' test.txt
我会做什么:
awk '
{
key = $1"|"$2"|"$3
if (!(key in ops)) {
groups[key]=$1","$2","$3
}
if (!seen[key"|"$4]) {
ops[key]=ops[key](ops[key] ? "," : "") $4
seen[key"|"$4]=1
}
}
END {
for (key in groups) {
split(groups[key], fields, ",")
print "grant " ops[key] " on " fields[2] "." fields[3] " to " fields[1]";"
}
}' file