LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-20-2012, 02:43 PM   #1
Checksumfail
LQ Newbie
 
Registered: Jan 2011
Posts: 12

Rep: Reputation: 0
BASH: Keeping/replacing leading zeros with BC


So I have made a script for copying a file with a name like IMG_2012_00754.tif and making incremental file names, so you can specify how many copies you want etc.

Now the problem is with file names like the above you get:

IMG_2012_00754.tif
IMG_2012_755.tif
IMG_2012_756.tif

For the purpose of this script keeping those leading zeros is important, and the script will be used on many different file name formats so the length of that last field is not a certain length (I can't force a field length of 5)

Is there anyway to stop BC from removing those leading zero's when I +1 to that field? Can I +1 with awk and keep them?
 
Old 07-20-2012, 02:50 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
We'd need to see your actual code to give you exact advice, but in general you can use printf to re-pad the numbers.

printf

Also see here for more on the built-in arithmetic, especially on how to handle zero-padded numbers. You shouldn't need to use any external tools for this.

arithmetic expressions
 
1 members found this post helpful.
Old 07-20-2012, 03:02 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Ok, here's one proof-of-concept solution for you.

Code:
file='IMG_2012_00754.tif'

#extract the last set of digits
oldnum=${file##*_}
oldnum=${oldnum%.*}

#increment number and pad it to the same width as the original
printf -v newnum "%0*d" "${#oldnum}" "$(( 10#$oldnum + 1 ))"

#build the new filename
echo "${file%_*}_${newnum}.tif"

#result from the above:
IMG_2012_00755.tif

Last edited by David the H.; 07-20-2012 at 03:04 PM.
 
1 members found this post helpful.
Old 07-20-2012, 03:29 PM   #4
Checksumfail
LQ Newbie
 
Registered: Jan 2011
Posts: 12

Original Poster
Rep: Reputation: 0
Thankyou so much, I've got it working now
 
  


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
How to add leading zeros to numbers for file names rootaccess Linux - General 20 06-16-2012 07:35 PM
Renaming files (adding leading zeros) General Linux - Software 3 01-09-2011 09:17 AM
Bash Scripting: how-to keep leading 0 on '08' and '09' ? Linville79 Programming 8 01-29-2010 08:53 AM
How to insert leading 0 into a string within bash patje999 Programming 4 10-02-2009 05:29 AM
Bash Maths... Keeping leading zero's lin*x Linux - Newbie 1 08-05-2009 11:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:46 AM.

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