LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2020, 01:00 PM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Add leading zero's


I have a simple bash script
Code:
#!/bin/bash
COUNTER=0;
for FF in *.MP2  
  do ((COUNTER++)); 
  FFF=Ep$COUNTER.MP2 
  echo $FFF
done;
That produces this
Code:
charlie@charlie-machine:~/MP2-MP3/Test/LATEST/ThePrisoner$ ls
05241758.MP2  05311758.MP2  06071758.MP2  06141759.MP2 .....12 FILES
charlie@charlie-machine:~/MP2-MP3/Test/LATEST/ThePrisoner$ ./ProcessEpisodes.sh 
18904 (process ID) old priority 0, new priority 19
Ep1.MP2
Ep2.MP2
Ep3.MP2
Ep4.MP2
Ep5.MP2
Ep6.MP2
Ep7.MP2
Ep8.MP2
Ep9.MP2
Ep10.MP2
Ep11.MP2
Ep12.MP2
charlie@charlie-machine:~/MP2-MP3/Test/LATEST/ThePrisoner$
I want to add a leading zero to the output
Code:
Ep01.MP2
Ep02.MP2
Ep03.MP2
Ep04.MP2
Ep05.MP2
Ep06.MP2
Ep07.MP2
Ep08.MP2
Ep09.MP2
Ep10.MP2
Ep11.MP2
Ep12.MP2
It looks like it should be easy, but ........
 
Old 06-21-2020, 01:09 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
yes, printf is your friend. And you can find a lot of good example on the net. Like this: https://stackoverflow.com/questions/...-loop-in-shell
 
Old 06-21-2020, 03:19 PM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
If the point of this exercise is to rename files then why not do it directly?
Code:
rename -n 'our $c++;$_=sprintf "Ep%02d.MP2",$c' *.MP2
Omit -n when you're sure it does the right thing.

Last edited by shruggy; 06-21-2020 at 03:23 PM.
 
Old 06-21-2020, 03:35 PM   #4
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by shruggy View Post
If the point of this exercise is to rename files then why not do it directly?
Code:
rename -n 'our $c++;$_=sprintf "Ep%02d.MP2",$c' *.MP2
Omit -n when you're sure it does the right thing.
Good point, I might just do that, and it will work.

What I'm actually going to do is to do re-encode the MP2 files to mp3 and reduce their size by using a lower bitrate on the audio.

So I would like to do this for the output something like this
Code:
ffmpeg -i 089765.MP2 Ep01.mp3
So if Ep01.mp3 could be constructed on the fly that would be great. Maybe function I guess using printf
 
Old 06-21-2020, 03:58 PM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Code:
#!/bin/bash
for i in *.MP2
do
  printf -vo "Ep%02u.mp3" $((++c))
  ffmpeg -i "$i" "$o"
done
 
Old 06-21-2020, 04:56 PM   #6
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
To the OP. You can loop on that directory of files however you wish.

Basic examples:
Code:
files=(*.mp2)

numFiles="${#files[@]}"

#Using evil eval
for i in $(eval echo {000..$numFiles}); do
    echo "Ep"$i".mp3"
    sleep .5
done
    
#Using seq
for i in $(seq -w 000 $numFiles); do
    echo "Ep"$i".mp3"
    sleep .5
done

#printf
for i in "${!files[@]}"; do
    printf "%03d\\n" "$i"
    sleep .5
done

#The file names
for i in "${files[@]}"; do
    echo "$i"
    sleep .5
done

#The array index number
for i in "${!files[@]}"; do
    echo "$i"
    sleep .5
done
etc.
 
1 members found this post helpful.
Old 06-21-2020, 05:24 PM   #7
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by shruggy View Post
Code:
#!/bin/bash
for i in *.MP2
do
  printf -vo "Ep%02u.mp3" $((++c))
  ffmpeg -i "$i" "$o"
done
Brilliant, thanks very much, but can I ask what do the -vo switches do, and I cannot understand how $o is assigned, I'm guessing -v means variable and o of -vo menas assign it to a variable named o - am I correct.

man printf didn't help.

But once again many thanks
 
Old 06-21-2020, 05:35 PM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by GPGAgent View Post
I'm guessing -v means variable and o of -vo menas assign it to a variable named o - am I correct.
Exactly.

Quote:
Originally Posted by GPGAgent View Post
man printf didn't help.
Don't confuse the /usr/bin/printf command from GNU coreutils with bash builtin printf. The -v option is bash-specific. In POSIX shell that would be
Code:
#!/bin/sh
for i in *.MP2
do
  : $((c+=1))
  ffmpeg -i "$i" $(printf "Ep%02u.mp3" $c)
done

Last edited by shruggy; 06-21-2020 at 05:49 PM.
 
1 members found this post helpful.
Old 06-22-2020, 05:29 AM   #9
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
shruggy and teckk

Thanks guys, your tips and and solutions really help - much appreciated!!

Problem solved
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Add leading zeroes that aren't really leading LXer Syndicated Linux News 0 09-13-2019 12:31 PM
Script is stripping off leading zero wtaicken Programming 5 11-12-2009 07:02 AM
Bash Maths... Keeping leading zero's lin*x Linux - Newbie 1 08-05-2009 11:44 PM
In Python, printing leading zero for hex numbers 0 through f donpar Programming 1 04-15-2009 07:09 PM
Firestarter 'Serious' is non-zero, 'Total' is zero sixerjman Linux - Software 0 08-24-2007 12:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration