Use subgroups for extracting specific parts of the input line.
Provide your custom input pattern with the command line argument `-i '<pattern>'`
The default value is `^.*?$` which simply matches the whole line.
## Output pattern
The output pattern describes the format in which lines are generated for stdout using data from the input pattern.
The default value is `{0}` which always matches the full input pattern
### Capturing groups
Use the `{<group_index>}` syntax to use a specific capturing group.
-`{0}` always matches the whole line.
-`{1}` matches the first capturing group
-`{2}` matches the second capturing group
- and so on
### Formatting
When referencing capturing groups, you can add a specific format for some data types as well using a simplified printf syntax.
You can use them using this syntax: `{1:%d}`. It will parse the first capturing group into an integer to get rid of leading zeros. Additionally, you can provide a given amount of leading zeros via: `{1:%03d}` for a total length of 3 digits.
The same method can also be applied to `%f` to further format floating point values.
See a full list of formatting options at [Go's fmt spec](https://pkg.go.dev/fmt).
You can provide a mutator using the syntax: `{1:%d:+1}`. It will parse the first capturing group into an integer, adds 1 and then formats the result using the printf format `%d`.
It is possible to add multiple mutators by just concatenating them: `{1:%d:*2+1}`.
Multiple mutators will not follow any order of operations. They are simply applied from left to right!
Furthermore you can reference caputring groups which will be parsed as the same type to apply its value. This is done via the following syntax: `{1:%d:+(2)}`. It will parse the first and second capturing group into integers and adds them.
## Handling unmatched lines
By default, lines which do not match the input pattern will be dropped.
Use `-k` to keep them unchanged. They will be copied into stdout.
Every format process can only apply a single pattern. Use `-k` to keep unmatched lines so the next format instance can apply another input pattern to them
To further automate this, I made my own custom script called `bulkrename` and put it in my `$PATH`
Content:
```sh
#!/usr/bin/env sh
if [ "$3" = "exec" ]; then
command ls | format -i "$1" -o "mv \"{0}\" \"$2\"" | xargs -0 -P 4 sh -c
else
command ls | format -i "$1" -o "mv \"{0}\" \"$2\""
echo
echo "execute commands with 'bulkrename $@ exec'"
fi
```
There are a few things to consider using this script:
- You can't use `-i` and `-o`. The first argument is input, the second is output
- To prevent unwanted file modifications, it will print the `mv` commands it generates to stdout by default. After you checked that all files will be renamed as desired, just add `exec` as its third argument
- It can only move items which are in your working directory. But it can move these files outside of the working directory using relative or absolute paths