Issue
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;
Solution
Assumptions:
- all filenames have the same format:
a_b_c_d_e_n1_n2.ext
where ... a..e
andext
do not contain white spacen1/n2
are numbers
One bash
idea:
for fname in L2_error_with_G_at_2_9.csv L2_error_with_G_at_1_16.csv
do
printf -v new "%s_%s_%s_%s_%s_%02d_%02d.%s" ${fname//[._]/ }
echo mv "${fname}" "${new}"
done
This generates:
mv L2_error_with_G_at_2_9.csv L2_error_with_G_at_02_09.csv
mv L2_error_with_G_at_1_16.csv L2_error_with_G_at_01_16.csv
Once the output is verified the echo
can be removed from the code so that on the next run the mv
occurs.
If the list of filenames is in a file:
while read -r fname
do
printf -v new "%s_%s_%s_%s_%s_%02d_%02d.%s" ${fname//[._]/ }
echo mv "${fname}" "${new}"
done < file.list
If the list of filenames is coming from a command (eg, find
or ls
):
while read -r fname
do
printf -v new "%s_%s_%s_%s_%s_%02d_%02d.%s" ${fname//[._]/ }
echo mv "${fname}" "${new}"
done < <(command that generates list of filenames)
Answered By - markp-fuso Answer Checked By - Dawn Plyler (WPSolving Volunteer)