Awk 汇编出了问题

Awk 汇编出了问题,awk,Awk,请问我做错了什么?我有一个代码在文件中 file.awk: BEGIN { CONVFMT="%0.17f" } /D Format/ { in_f_format=0; next } /F Format/ { in_f_format=1; next } in_f_format != 1 { next } !($1 ~ /^[1-9]/) { next } $1 == 11 { prt(180,3.141592653589); next } $1 == 15 { prt(100,1); next

请问我做错了什么?我有一个代码在文件中

file.awk:

BEGIN { CONVFMT="%0.17f" }
/D Format/ { in_f_format=0; next }
/F Format/ { in_f_format=1; next }
in_f_format != 1 { next }
!($1 ~ /^[1-9]/) { next }
$1 == 11 { prt(180,3.141592653589); next }
$1 == 15 { prt(100,1); next }
$1 == 20 { prt(10,1); next }
$1 == 26 { next }
{ prt(1,1) }

function prt(mult, div) {
print trunc($5 * mult / div) ORS trunc($6 * mult / div)
}

function trunc(n, s) {
s=index(n,".")
return (s ? substr(n,1,s+6) : n)
}
我写道:

chmod +x file.awk 
./file.awk
到了终点站,我犯了以下错误:

./file.awk: řádek 1: BEGIN: příkaz nenalezen
./file.awk: řádek 2: /D: Adresář nebo soubor neexistuje
./file.awk: řádek 2: next: příkaz nenalezen
./file.awk: řádek 3: /F: Adresář nebo soubor neexistuje
./file.awk: řádek 3: next: příkaz nenalezen
./file.awk: řádek 4: in_f_format: příkaz nenalezen
./file.awk: řádek 5: chyba syntaxe poblíž neočekávaného tokenu „{“
./file.awk: řádek 5: `!($1 ~ /^[1-9]/) { next }'
请问错在哪里

编辑 类似的脚本

BEGIN { CONVFMT="%0.17f" }
/D Format/ { in_f_format=0; next }
/F Format/ { in_f_format=1; next }
in_f_format != 1 { next }
!($1 ~ /^[1-9]/) { next }
$1 == 35 { print t($5), t($6) }

function trunc(n, s) {
s=index(n,".")
return (s ? substr(n,1,s+6) : n)
}
给出一个错误:

fatal: function `t' not defined
我想用这个输入来写

                      Input-Output in F Format

No.  Curve    Input Param.        Correction     Output Param.    Standard Deviation
26      0  56850.9056460000     -0.0017608883  56850.9038851117      0.0016647171
35      1      0.2277000000      0.0011369754      0.2288369754      0.0014780395
35      2      0.2294000000      0.0000417158      0.2294417158      0.0008601513
35      3      0.2277000000      0.0007425066      0.2284425066      0.0022555311
35      4      0.2298000000     -0.0000518690      0.2297481310      0.0010186846
35      5      0.2295000000      0.0000793572      0.2295793572      0.0014667137
35      6      0.2300000000      0.0000752449      0.2300752449      0.0006258864
35      7      0.2307000000     -0.0001442591      0.2305557409      0.0002837569
35      8      0.2275000000      0.0007358355      0.2282358355      0.0007609550
35      9      0.2292000000      0.0003447650      0.2295447650      0.0007148005
35     10      0.2302000000     -0.0001854710      0.2300145290      0.0006320668
35     11      0.2308000000     -0.0002064324      0.2305935676      0.0008911070
35     12      0.2299000000     -0.0000202967      0.2298797033      0.0002328860
35     13      0.2298000000      0.0000464629      0.2298464629      0.0011609539
35     14      0.2307000000     -0.0003654521      0.2303345479      0.0006827961
35     15      0.2294000000      0.0002157908      0.2296157908      0.0003253584


                      Input-Output in D Format
以35开头的行中的$5和$6中的数字

编辑2 我把f函数的位置改成

BEGIN { CONVFMT="%0.17f" }
function trunc(n, s) {
s=index(n,".")
return (s ? substr(n,1,s+6) : n)
}
/D Format/ { in_f_format=0; next }
/F Format/ { in_f_format=1; next }
in_f_format != 1 { next }
!($1 ~ /^[1-9]/) { next }
$1 == 35 { print t($5), t($6) }
使用它的规范(不确定它是否是正确的词)方式如下:

awk -f file.awk datafile
cat datafile | awk -f file.awk
./file.awk datafile
或者像这样的管道:

awk -f file.awk datafile
cat datafile | awk -f file.awk
./file.awk datafile
文件.awk
,与您的尝试相同,是awk的脚本文件。(名称可以更改)。
数据文件
是包含要处理的数据的文件

并且无需chmod awk文件即可使用它

更新:
但是谢谢keith在评论中友好地提到你也可以这样做。
这句话是:

#/usr/bin/awk -f
文件.awk
的开头(假定
awk
可执行文件位于该位置)。
chmod+x file.awk
之后,您可以这样执行它:

awk -f file.awk datafile
cat datafile | awk -f file.awk
./file.awk datafile
更新:与OP聊天后(我在评论中也提到过),OP需要将函数名从
t
更改为实际函数,然后它就工作了。我想在这里更新一下,让大家都知道



可能有两种可能的解决方案

1st:在shell脚本中提及
awk
,并将其作为shell脚本运行

cat script.ksh
awk 'BEGIN { CONVFMT="%0.17f" }
/D Format/ { in_f_format=0; next }
/F Format/ { in_f_format=1; next }
in_f_format != 1 { next }
!($1 ~ /^[1-9]/) { next }
$1 == 11 { prt(180,3.141592653589); next }
$1 == 15 { prt(100,1); next }
$1 == 20 { prt(10,1); next }
$1 == 26 { next }
{ prt(1,1) }

function prt(mult, div) {
print trunc($5 * mult / div) ORS trunc($6 * mult / div)
}

function trunc(n, s) {
s=index(n,".")
return (s ? substr(n,1,s+6) : n)
}' Input_file
给予
script.ksh
适当的执行权限,并像运行一样运行它

2nd:将其作为
awk
脚本运行,方法如下:

awk -f awk_file Input_file

如果没有shebang行,shell会认为您的脚本是shell脚本。要做到这一点,您必须使用正确的shebang线:

cat <<'EOF' > awkscript
#!/usr/bin/awk -f

BEGIN { print "Hello, world!" }
EOF
chmod +x awkscript

让我们来。或者您可以添加
#/usr/bin/awk-f
位于脚本顶部(以及
chmod+x
)。这样,用户就不必关心它是awk脚本,而不是shell、python或已编译的可执行文件。(假设
/usr/bin/awk
是解释器的位置。)@KeithThompson哇,太棒了!我将稍后尝试并更新我的答案,非常感谢:)FWIW我永远不会使用shebang调用awk解释器,因为这会剥夺您将shell脚本参数分为awk变量和awk参数的能力,例如,
shellscript foo bar
->
awk-v var=“$1”'awkscript'”$2
。当
shellscript
使用awk shebang时,尝试执行简单而常见的任务!不过,如果使用shebang方法有什么真正的好处,我洗耳恭听。。。