本文介绍了"BEGIN块必须具有动作部分". awk脚本中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
#!/bin/sh
filename=$(/usr/bin/find -name "INSTANCE-*.log")
echo "filename is " $filename
awk '
BEGIN
{
print "Processing file: " filename
}
{
if($0 ~ /Starting/)
{
print "The bill import has been Started on "$1 " " $2
}
}' $filename > report.txt
当我执行它时,出现以下错误:
When I execute it I get the following error:
我的BEGIN
块具有print
语句,因此它具有动作部分.我在这里想念什么?
My BEGIN
block has a print
statement so it has an action part. What am I missing here?
推荐答案
之所以会这样,是因为您的大括号位于下一行.
This happens because your opening curly brace is in the next line.
所以您需要做的是这样写BEGIN { ...
:
So what you need to do is to write BEGIN { ...
like this:
BEGIN {
print "Processing file: " filename
}
还请注意,可以将主块重写为:
Note also that the main block can be rewritten to:
/Starting/ {print "The bill import has been Started on "$1 " " $2}
也就是说,if ()
和$0
是隐式的,因此可以跳过.
That is, if ()
and $0
are implicit so they can be skipped.
这篇关于"BEGIN块必须具有动作部分". awk脚本中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!