Reputation: 343
What do I wrong please? I have a code in file
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)
}
I write:
chmod +x file.awk
./file.awk
to the terminal and I get these mistakes:
./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 }'
Where is the mistake please?
EDIT Similarly script
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)
}
Gives an error:
fatal: function `t' not defined
I would like to write from this input
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
numbers that are in $5 and $6 that are in rows starting 35.
EDIT 2 I eddited the position of f function like
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) }
Upvotes: 2
Views: 69
Reputation: 5252
The canonical(not sure if it's the right word) way to use it is like this:
awk -f file.awk datafile
or the pipe way like this:
cat datafile | awk -f file.awk
The file.awk
, which is same as your trying, is the awk's script file.(name can change).
And the datafile
is the file(s) contains the data you want to dealing with.
And no need to chmod awk file(s) to use it.
Update:
But thanks keith kindly mentioned in comment you can do it like that too.
Put this line:
#/usr/bin/awk -f
at the beginning of your file.awk
.(given your awk
executable is at that location).
After chmod +x file.awk
, you can execute it like this:
./file.awk datafile
Upvotes: 2
Reputation: 52316
Without a shebang line, the shell thinks your script is a shell script. To make it executable as an awk script, you have to use the proper shebang line:
cat <<'EOF' > awkscript
#!/usr/bin/awk -f
BEGIN { print "Hello, world!" }
EOF
chmod +x awkscript
where /usr/bin/awk
is the path to your awk executable (can be found with type awk
). The important bit is the -f
flag.
Now you can run it as a standalone script:
$ ./awkscript
Hello, world!
Upvotes: 2
Reputation: 133610
UPDATE: After chatting with OP(which I mentioned in my comments too) OP needs to change function's name to actual function from t
and it worked then. Thought to update here so all will know it.
There could be 2 possible solutions.
1st: Mention awk
in shellscript and run it as shell script.
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
Give script.ksh
proper execute permissions and run it like you are running.
2nd: Run it as a awk
script by running it like:
awk -f awk_file Input_file
Upvotes: 3