Skip to content

Instantly share code, notes, and snippets.

@janxkoci
Created August 28, 2025 13:47
Show Gist options
  • Select an option

  • Save janxkoci/55ebc399ddc8fae44ff02317a4c1036b to your computer and use it in GitHub Desktop.

Select an option

Save janxkoci/55ebc399ddc8fae44ff02317a4c1036b to your computer and use it in GitHub Desktop.
metaprogramming with awk :))
#!/usr/bin/awk -f
## gawk -i inplace -f update_scripts.awk scripts*.r
## some scripts define output variable without spaces
/^my_variable=/ && NF == 1 {
split($0, myvar, "=")
$1 = myvar[1]
$2 = "="
$3 = myvar[2]
}
## add scratch to output paths
$1 == "my_variable" && $3 !~ /paste0/ {
add_scratch = "paste0(Sys.getenv(\"SCRATCHDIR\"), \"/\", " $3 ")"
$3 = add_scratch
}
## print the script
{print}
@janxkoci
Copy link
Author

rewritting R scripts with awk

I had to run hundreds of scripts for my colleague at a large HPC. Her scripts were written for a smaller server, while I needed to use features like scratch drives. To update all the scripts (which were coming in batches by dozens) on demand, I've made a simple awk script to inject a shell variable into her output path (luckily, my colleague made it easy by setting the same variable near the top of each script; except later she changed the assignment style, so I needed to address that too).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment