将 awk 命令放入文件中

将 awk 命令放入文件中

我有以下bash调用并希望将awk命令放入文件中awk。我想做的是将特定功能从 line 开始并在 function中$0 ~ beg_ere {结束。然后就可以从函数中调用它。display { sub(pn_ere, "") ; print }awkbash

  awk -v ukeys="$keyw" -v beg_ere="$beg_ere" -v pn_ere="$pn_ere" -v end_ere="$end_ere" \
    'function praggr (aggr) {
       resultstr = ""
       for (k in aggr) resultstr = (resultstr " | " aggr[k])
       return resultstr
     }
     $0 ~ beg_ere {
       title=gensub(beg_ere, "\\2", 1, $0);
       subtitle=gensub(beg_ere, "\\3", 1, $0);
       keywords=gensub(beg_ere, "\\4", 1, $0);

       nu = split(ukeys, uaggr, ",");
       nk = split(keywords, kaggr, ",");

       display=0;
       if ( nu > 0 && nk > 0)
        {
         for (i in uaggr)
          {
            umatch=0;
            for (j in kaggr)
             {
               if (uaggr[i] == kaggr[j]) { umatch=1; break; }
             }
             if (umatch == 1) { display=1; }
          }
        }

       else if ( nu > 0 && nk == 0 )
        {
          display=0;
        }

       else if ( nu == 0 )
        {
          display=1;
        }

       if (verbiage)
        {
          print "Title: " title;
          print "Subtitle: " subtitle;
          print "Keywords: " keywords;
          print "uaggr: " praggr(uaggr);
          print "kaggr: " praggr(kaggr);
          print "nu:" nu;
          print "nk:" nk;
        }
       next
     }
     $0 ~ end_ere { display=0 ; print "" }
     display { sub(pn_ere, "") ; print }
    ' "$filename"

答案1

创建一个.awk扩展名为 的新文件,例如script.awk,并将 awk 表达式粘贴到该文件中。然后,您可以在命令中引用该文件。

awk -v ukeys="$keyw" -v beg_ere="$beg_ere" -v pn_ere="$pn_ere" -v end_ere="$end_ere" -f script.awk "$filename"

答案2

我认为不可能同时指定awk -fawk '{code...}'

在 GNU awk (又名gawk)中,您可以使用选项同时读取多个代码-e,请参阅 参考资料gawk --help获取更多详细信息。阅读使用也会很有用AWKPATH(请参阅man gawk阅读有关它的内容)。

为了避免在这种情况下使用 GNU awk,您应该-fawk命令中指定 multiple。

避免使用两个文件您可以使用流程替代 <(command)。例如,您可以使用 -f<(echo code ...)定义函数,并在awk文件内定义其他代码,如下所示:

test.awk:

{ // I'm not sure if the `{` and  `}` should be here or they're not necessary at all
 $0 ~ beg_ere {
       title=gensub(beg_ere, "\\2", 1, $0);
       subtitle=gensub(beg_ere, "\\3", 1, $0);
       keywords=gensub(beg_ere, "\\4", 1, $0);

       nu = split(ukeys, uaggr, ",");
       nk = split(keywords, kaggr, ",");

       display=0;
       if ( nu > 0 && nk > 0)
        {
         for (i in uaggr)
          {
            umatch=0;
            for (j in kaggr)
             {
               if (uaggr[i] == kaggr[j]) { umatch=1; break; }
             }
             if (umatch == 1) { display=1; }
          }
        }

       else if ( nu > 0 && nk == 0 )
        {
          display=0;
        }

       else if ( nu == 0 )
        {
          display=1;
        }

       if ()
        {
          print "Title: " title;
          print "Subtitle: " subtitle;
          print "Keywords: " keywords;
          print "uaggr: " praggr(uaggr);
          print "kaggr: " praggr(kaggr);
          print "nu:" nu;
          print "nk:" nk;
        }
       next
     }
     $0 ~ end_ere { display=0 ; print "" }
     display { sub(pn_ere, "") ; print }
}

通过进程替换,您可以使用如下内容:

<(echo 'function praggr (aggr) {
       resultstr = ""
       for (k in aggr) resultstr = (resultstr " | " aggr[k])
       return resultstr
     }
')

或者为了避免单引号问题,'您可以使用cat << EOF

<(cat <<'EOF' 
function praggr (aggr) {
  resultstr = ""
  for (k in aggr) resultstr = (resultstr " | " aggr[k])
    return resultstr
}
EOF
)

最后,你的awk命令将变成:

awk -v yourvars... -f <(echo 'function praggr (aggr) {
       resultstr = ""
       for (k in aggr) resultstr = (resultstr " | " aggr[k])
       return resultstr
     }
') -f test.awk "$filename"

相关内容