I have this line as one of many in a file
show = "holder"; // [holder: Dispensor,dryer: Hair Dryer,bidet: Bedit,cover: Cover,wall mount: Wall Mount,screwdriver cutout: Screwdriver Cutout, assembly: Assembly,exploded: Exploded,vitamin: Vitamin]
What I want is to generate a shell script file where for each xxx:yyy pair generate the following line:
openscad --quite --export-format --D 'show=xxx' binstl -o xxx.stl $1
The $1 above is for the first parameter of a shell script.
I have tried this code:
match($0, /\[[^\n\]]*\]$/) { # get [xx:yy, aa:bb]
s = substr($0, RSTART+1, RLENGTH-2); # strip the [ and ]
n = split(s, arr,","); # split the pairs
for(x in arr) { # for each pair
match(arr[x], /([^\n:]+)/, b) { # get the first field of the pair
print subsgtr(b[1], RSTART, RLENGTH)
}
}
}
With this result:
gawk: prog:5: match(arr[x], /([^\n:]+)/, b) { # get the first field of the pair
gawk: prog:5: ^ syntax error
I've also tried this:
match($0, /\[[^\n\]]*\]$/) { # get [xx:yy, aa:bb]
s = substr($0, RSTART+1, RLENGTH-2); # strip the [ and ]
n = split(s, arr,","); # split the pairs
for(x in arr) { # for each pair
arr[x] ~ /([^\n:]+)/ print
}
}
gawk: prog:5: arr[x] ~ /([^\n:]+)/ print
gawk: prog:5: ^ syntax error
and this:
match($0, /\[[^\n\]]*\]$/) { # get [xx:yy, aa:bb]
s = substr($0, RSTART+1, RLENGTH-2); # strip the [ and ]
n = split(s, arr,","); # split the pairs
for(x in arr) { # for each pair
arr[x] ~ /([^\n:]+)/ { print }
}
}
gawk: prog:5: arr[x] ~ /([^\n:]+)/ { print }
gawk: prog:5: ^ syntax error
I don't understand why I'm getting the syntax errors. If you show me a different/better solution, if you can, please explain the syntax errors so I can learn more about awk. Thanks!
awk
pattern-action statements are expressed ascondition {action}
. You cannot use the same pattern-action form inside an action. Useif
statements, instead. For instance, replacearr[x] ~ /([^\n:]+)/ print
withif(arr[x] ~ /([^\n:]+)/) print
.For your problem, and with the given example, you could define the input field separator as a regular expression, and skip the first and last fields. Example, with quotes added compared with the expected output (because you have some spaces there), and 2 typos fixed (
quiet
and--export-format binstl
):Note: another approach would be to write a generic bash script in which you use
awk
to feed a loop. In the following example the input file is passed as second parameter ($2
):