Rename first N numeric strings in filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename first N numeric strings in filename
# 1  
Old 04-11-2019
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:
Code:
87338ArianaGrandebreakupwithyourgirlfriendimboredLH4Y1ZUUx2g.mp4
92593ArianaGrandebreathinkN0iD0pI3o0.mp4
29003ArianaGrandeDangerousWoman9WbCfHutDSE.mp4
29024ArianaGrandeFocuslfwVfwpfp8.mp4
39043ArianaGrandeGodisawomankHLHSlExFis.mp4

Code:
for i in `seq 5`
do
A=$( echo "$((10000 + RANDOM % 9999))" )
echo "$A"
done

Number creation is okay with above command..
Code:
11111
18367
12008
15249
10059

Expected output:
Code:
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
# 2  
Old 04-11-2019
With a recent shell (which you alas fail to mention) try "Parameter expansion / Substring Expansion":
Code:
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?
These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 04-11-2019
Code:
$ 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

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

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

# 5  
Old 04-11-2019
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
# 6  
Old 04-11-2019
man bash:
Quote:
${parameter#word}
${parameter##word}
Remove matching prefix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of
parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ``#'' case) or the longest matching pat”
tern (the ``##'' case) deleted.

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.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 04-11-2019
Thank you Rudic,
That's what I wanted to do.
I will give both solutions a go in soonest available time.

Kind regards
Boris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add edited numeric strings in awk

I am using awk to sum up all amounts and at end print total. input: 10,250.00 20,103.15 expected output: 30353.15 code: {subtot=+$1} END{print subtot} The problem I encounter is it stops at commas and returns 30 as answer And if I use this code: {subtot=($1+0);subtot=+$1}... (9 Replies)
Discussion started by: paresh n doshi
9 Replies

2. Shell Programming and Scripting

Sort log files based on numeric value in the filename

Hi, I have a list of log files as follows: name_date_0001_ID0.log name_date_0001_ID2.log name_date_0001_ID1.log name_date_0002_ID2.log name_date_0004_ID0.log name_date_0005_ID0.log name_date_0021_ID0.log name_date_0025_ID0.log .......................................... (4 Replies)
Discussion started by: alex2005
4 Replies

3. UNIX for Dummies Questions & Answers

Listing numeric strings alone

i have a file called 'test.txt' which contains the below data. abc123 123445 123abc 23224q From the above data, i want to display the line which contains only numeric . So i have tried the below commands with sed sed -n '/^$/p' test.txt sed '/^$/!d' test.txt But it... (4 Replies)
Discussion started by: pandeesh
4 Replies

4. UNIX for Advanced & Expert Users

query display number lines or records present in file only numeric value -without filename

Hi all Thanks in advance........... Please help me for this issue............ I have a file it has 11 records . I used the command like .... >$ wc -l file 11 file I'm getting output like 11 file (no.of records along with filename) here my requirement is, I want to display only... (3 Replies)
Discussion started by: ksrivani
3 Replies

5. UNIX for Dummies Questions & Answers

sort files by numeric filename

dear all, i have .dat files named as: 34.dat 2.dat 16.dat 107.dat i would like to sort them by their filenames as: 2.dat 16.dat 34.dat 107.dat i have tried numerous combinations of sort and ls command (in vain) to obtain : 107.dat 16.dat 2.dat 34.dat (1 Reply)
Discussion started by: chen.xiao.po
1 Replies

6. Shell Programming and Scripting

Rename FileName in the Directory

In the Directory all the Files are following format. Filename_yyyymmdd_numbers.txt eg. file_name_20120106_015802.txt . I want to write the Shell script to rename all the file to file_name.txt.in the directory. Thanks Mani (5 Replies)
Discussion started by: gavemani
5 Replies

7. UNIX for Dummies Questions & Answers

rename filename

Hi, I am pretty new to this. I have a condition where in I want to replace all files within a folder. All filenames with character "abc" would need to replaced with "xyz". eg: helloabcworld-->helloxyzworld helloworld-->helloworld ... ... Thanks in advance. (6 Replies)
Discussion started by: sakets_2000
6 Replies

8. Shell Programming and Scripting

Grep and rename the filename

Hi All, Can you please help me. The situation is like this. There are many different file name in this directory. I have to grep all the file that the name start with "PTWO" and rename it to COM with the current date. This is the script that I have done and it hit an... (16 Replies)
Discussion started by: badbunny9316
16 Replies

9. Shell Programming and Scripting

rename multiple filename.45267.txt to >> filename.txt

i have several thousand files and in subdirs that are named file.46634.txt budget.75346.pdf etc i want to remove the number but retain the extension. it is always a 5 digit. thanks. (6 Replies)
Discussion started by: jason7
6 Replies

10. UNIX for Dummies Questions & Answers

Wanted to eliminate numeric part from a filename

Hi, I have a filename called XYZ12345.txt.I just want to eliminate numeric and have only XYZ.txt. How can i do it ? Regards, Sona. (8 Replies)
Discussion started by: Sona
8 Replies
Login or Register to Ask a Question