LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 03: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, 03:50 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
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, 04:02 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
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 04:04 PM.
 
1 members found this post helpful.
Old 07-20-2012, 04: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

All times are GMT -5. The time now is 03:01 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration