Skip to content

Instantly share code, notes, and snippets.

@novelistparty
Created October 12, 2015 18:14
Show Gist options
  • Save novelistparty/b18c377059b152ba1439 to your computer and use it in GitHub Desktop.
Save novelistparty/b18c377059b152ba1439 to your computer and use it in GitHub Desktop.
The first awk file I made for someone else
#!/usr/bin/awk -f
# AWK can do cool things!
# x(58,strt:strt+7) = z(42:184:42+184*7);
# should map to:
# for (it = 0; it < 8; it++) x[58 + C * it] = z[42 + C * it];
BEGIN {
FS="[()]" # makes things within parentheses a field
}
# Only do this for lines that contain "x("
/x\(/ {
# $1 is " x"
# $3 is " = z"
# parse the contents of "x(...)" and "z(...)"
nx = split($2, x_indices,",")
nz = split($4, z_indices,":")
# Uncomment this loop to examine how it split $2
# for (i = 1; i <= nx; i++) {
# print "x"i , x_indices[i]
# }
# Uncomment this loop to examine how it split $4
# for (i = 1; i <= nz; i++) {
# print "z"i , z_indices[i]
# }
printf("for (it = 0; it < 8; it++) x[%d + 144 * it] = z[%d + 184 * it];\n", x_indices[1], z_indices[1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment