首页 > 解决方案 > 编译出了点问题

问题描述

请问我有什么问题吗?我在文件中有一个代码

文件.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

解决方案


更新:与 OP(我在评论中也提到过)聊天后,OP 需要将函数名称更改为实际函数,t然后它就可以工作了。想在这里更新,所以所有人都会知道。



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

第一个:在 shellscript 中提及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

推荐阅读