Created
August 28, 2025 13:47
-
-
Save janxkoci/55ebc399ddc8fae44ff02317a4c1036b to your computer and use it in GitHub Desktop.
metaprogramming with awk :))
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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} |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).