Rename first N numeric strings in filename

Hello,
I have some video files containing numbers and characters . To tell the problem shortly, due to a limitation, I am unable create a playlist regularly changing on a daily basis with the command shuffle ....So I decided to rename filenames, just a replacement of first five numbers randomly.
myfiles:

87338ArianaGrandebreakupwithyourgirlfriendimboredLH4Y1ZUUx2g.mp4
92593ArianaGrandebreathinkN0iD0pI3o0.mp4
29003ArianaGrandeDangerousWoman9WbCfHutDSE.mp4
29024ArianaGrandeFocuslfwVfwpfp8.mp4
39043ArianaGrandeGodisawomankHLHSlExFis.mp4
for i in `seq 5`
do
A=$( echo "$((10000 + RANDOM % 9999))" )
echo "$A"
done

Number creation is okay with above command..

11111
18367
12008
15249
10059

Expected output:

11111ArianaGrandebreakupwithyourgirlfriendimboredLH4Y1ZUUx2g.mp4
18367ArianaGrandebreathinkN0iD0pI3o0.mp4
12008ArianaGrandeDangerousWoman9WbCfHutDSE.mp4
15249ArianaGrandeFocuslfwVfwpfp8.mp4
10059ArianaGrandeGodisawomankHLHSlExFis.mp4

How can I do it? Is there a command to separate numeric / alphanumeric fields ?

Thank you
Boris

With a recent shell (which you alas fail to mention) try "Parameter expansion / Substring Expansion":

for FN in *.mp4; do echo mv "$FN" "$((10000 + RANDOM % 9999))${FN:5}"; done

If you know for sure its five leading digits, why do you think you need tell numeric from alpha characters?

2 Likes
$ for i in *.mp4
> do
> Num=${i%%[a-zA-Z]*}
> echo mv $i $((10000 + RANDOM % 9999))${i#$Num}
> done
mv 29003ArianaGrandeDangerousWoman9WbCfHutDSE.mp4 12880ArianaGrandeDangerousWoman9WbCfHutDSE.mp4
mv 29024ArianaGrandeFocuslfwVfwpfp8.mp4 17588ArianaGrandeFocuslfwVfwpfp8.mp4
mv 39043ArianaGrandeGodisawomankHLHSlExFis.mp4 19680ArianaGrandeGodisawomankHLHSlExFis.mp4
mv 87338ArianaGrandebreakupwithyourgirlfriendimboredLH4Y1ZUUx2g.mp4 17652ArianaGrandebreakupwithyourgirlfriendimboredLH4Y1ZUUx2g.mp4
mv 92593ArianaGrandebreathinkN0iD0pI3o0.mp4 17909ArianaGrandebreathinkN0iD0pI3o0.mp4

If you want to replace the leading digits no matter how many by exactly 5 random digits, i.e. 10000 - 99999, try ( bash )

for FN in *.mp4; do echo mv "$FN" "$((10000 + 90000 * RANDOM / 32767))${FN#${FN%%[[:alpha:]]*}}"; done

Thank You Both,
I am sorry for my delayed return.
I am a bit busy with different sort of software problems now.

Dear Anbu,
Could you please explain the function of ${i#$Num}
I am trying to understand what you are aiming with that.

Dear Rudic,
Need more time to understand this.
What does FN#$ stand for? Especially #

Thanks
Boris

man bash :

The inner ${FN%%[[:alpha:]]*} removes all chars from the first alpha to string end, then ${FN#...} uses the result to remove the leading digits, no matter how many.

1 Like

Thank you Rudic,
That's what I wanted to do.
I will give both solutions a go in soonest available time.

Kind regards
Boris

Just be careful removing all leading digits if you have songs in your library by 3 Dog Night, 9 Inch Nails, or other artists who have chosen names starting with a number. :wink:

3 Likes

Also to add to Don's reply...

Just an observation.

Do we take it that the leading 5 digits only and NOT the whole filenames themselves determine the positional play.
if so what will happen if two or more end up with an identical 5 digit number from your random number generator?
If you have 9999 music files then this is a possibility...

What I understood from Rudic's post is that no matter what comes after alpha field. When it detects alphanumeric expressions, it will skip out the rest.

I am noob on nginx, as many other fields as an old guy :slight_smile:
As far as I know, Nginx+rtmp reads those videos from directory but always with the same list order in hls mode. I could not have found a solution to shuffle playlist with nginx. With your help, I will rotate the list by changing pre-numbers so list will change.
Also nginx is better than ffmpeg's hls mode with low cpu and ram usage. That's why I prefer this way.

Thank you
Boris

Thank You Anbu and Rudic,

for FN in *.mp4; do mv "$FN" "$((10000 + RANDOM % 9999))${FN:5}; done

File: 2019055SecondsOfSummerLieToMePKcGR3hexig.mp4
Output: 1887255SecondsOfSummerLieToMePKcGR3hexig.mp4
:slight_smile:

Kind regards
Boris