awk:在源代码行 1 中退出

awk: bailing out at source line 1

我希望获得一些彩色日志输出,但不想使用 multitail。

我要执行的命令是:

tail -100f some_file.log | awk '/INFO/ {print "3[32m" [=10=] "3[39m"} /Exception/ {print "3[31m" [=10=]"3[39m"}'

但我收到错误消息:

awk: syntax error at source line 1
 context is
     >>> /INFO <<< / {print "3[32m" [=11=] "3[39m"} /Exception/ {print "3[31m" [=11=]"3[39m"}
    4 missing ]'s
awk: bailing out at source line 1

我完全非专家的猜测是,出于某种原因,它认为终端代码应该关闭 ]'s。我试过转义它们,但没有用,坦率地说,我不确定这是否会增加终端代码。

awk: bailing out at source line 1

如果您使用的是 Solaris/SunOS 系统,请将此脚本开头的 awk 更改为 /usr/xpg4/bin/awk/usr/xpg6/bin/awknawk

安装并切换到 gawk 解决了这个问题。感谢所有提供帮助的回复。

来自Effective Awk Programming, Edition 4:

A Rose by Any Other Name The awk language has evolved over the years. Full details are provided in Appendix A [The Evolution of the awk Language], page 439. The language described in this book is often referred to as "new awk." By analogy, the original version of awk is referred to as "old awk."

On most current systems, when you run the awk utility you get some version of new awk.4 If your system’s standard awk is the old one, you will see something like this if you try the test program:

$ awk 1 /dev/null
error awk: syntax error near line 1
error awk: bailing out near line 1

In this case, you should find a version of new awk, or just install gawk!

Throughout this book, whenever we refer to a language feature that should be available in any complete implementation of POSIX awk, we simply use the term awk. When referring to a feature that is specific to the GNU implementation, we use the term gawk.

4 Only Solaris systems still use an old awk for > the default awk utility. A more modern awk lives in /usr/xpg6/bin on these systems.

此答案已从 Serching a pattern using awk and print next 15 lines from a log file

移出