If you want to use gawk , regex matching will really come in handy. I find this pipe-based solution a little better than worrying about loop designs.
ls -1 | \ gawk 'match($0, /.../, a) { printf ... | "sh" } \ END { close("sh") }'
For readability, I replaced the regex and mv with ellipses.
- Line 1 lists all the file names in the current directory, one line at a time and the channels that correspond to the gawk command.
- Line 2 starts the regex matching by assigning the captured groups to the array variable
a . The action converts this to our desired command with printf , which itself is sent to sh for execution. - Line 3 closes the shell, which was implicitly open when we started to bind things to it.
So, you just fill in the syntax of regular expressions and commands (borrowing from mklement0 ). For example ( LIVE CODE WARNING ):
ls -1 | \ gawk 'match($0, /^([0-9]+)\._([^.]+)_S([^.]+)\.(.+)$/, a) { printf "mv %s %s_s0%se%s.%s\n",a[0],a[2],a[3],a[1],a[4] | "sh" } \ END { close("sh") }'
To view this command (as it should), you can simply delete | "sh" | "sh" from the second line.
Javadocmd
source share