LinuxQuestions.org
Review your favorite Linux distribution.
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 09-12-2013, 11:41 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
simple parameter expansion


how would you guys go about adding a zero to the beginning if the integer in the parameter expansion is one digit, and ignoring it if the parameter expansion is 2 digits.


Code:
[casper@casper]$  for i in {6..11}; do  echo "201309${i}.txt.bz2"; done
2013096.txt.bz2
2013097.txt.bz2
2013098.txt.bz2
2013099.txt.bz2
20130910.txt.bz2
20130911.txt.bz2

i need to expand the range to :
Code:
20130906.txt.bz2
20130907.txt.bz2
20130908.txt.bz2
20130909.txt.bz2
20130910.txt.bz2
20130911.txt.bz2
 
Old 09-12-2013, 11:50 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Recent versions of bash accept zeroes in front of the first number:
Code:
$ echo {06..11}
06 07 08 09 10 11
Otherwise you can try printf and use the format specifiers at your pleasure:
Code:
for i in {6..11}
do
  echo 201309$(printf "%02d" $i).txt.bz2
done
In this specific case, you can manage dates using the date command and its format specifications:
Code:
day=20130906
while [[ $day -le 20130911 ]]
do
  echo ${day}.txt.bz2
  day=$(date -d "$day 1 day" +%Y%m%d)
done
 
1 members found this post helpful.
Old 09-12-2013, 11:52 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,121

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
see: http://wiki.bash-hackers.org/commands/builtin/printf
for i in {6..11}; do printf "201309%02d.txt.bz2\n" $i; done
 
1 members found this post helpful.
Old 09-12-2013, 11:53 AM   #4
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
amazing - thanks
 
  


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
BASH:Rewrite a function using parameter expansion instead of changing IFS Mes9 Programming 7 09-26-2012 03:50 PM
[SOLVED] Bash: extended regex pattern 'NOT' disabled inside parameter expansion? romagnolo Linux - General 4 02-22-2012 05:39 PM
Does bash shell parameter expansion run the risk of overflowing command line buffer? david1941 Linux - Software 1 04-28-2009 11:13 PM
LXer: Bash Parameter Expansion LXer Syndicated Linux News 0 10-02-2008 04:40 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 05:54 AM


All times are GMT -5. The time now is 07:12 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