I have a a set of file wat follow the form L2_error_with_G_at_#_#.csv
where the #
symbol could be any number between 1 and 16. I want to rename the files using bash and sed
so that any single digit number in the filename is buffered with a single 0, e.g. the filenames L2_error_with_G_at_2_9.csv
and L2_error_with_G_at_1_16.csv
would be replaced with filenames L2_error_with_G_at_02_09.csv
and L2_error_with_G_at_01_16.csv
. I'm seen solutions to this sort of problem that use the rename
function from pearl. I don't have access to that function as I'm working on a system where I don't have installation privileges. Ideally the solution will use more of the basic features of bash, which is why I suggested a solution using sed
.
EDIT: I do not need to use sed
, I can use anything so long as it's a standard part of bash
Here's what I've tried
for file in L2_error_with_G_at_*.csv
do
new=$(echo "$file" | sed 's/_\([0-9]\)/_0/g; s/_0\([0-9][0-9]\)/_\1/g;');
mv "$file" "$new";
done;