LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   simple parameter expansion (https://www.linuxquestions.org/questions/linux-newbie-8/simple-parameter-expansion-4175476875/)

casperdaghost 09-12-2013 10:41 AM

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


colucix 09-12-2013 10:50 AM

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


pan64 09-12-2013 10:52 AM

see: http://wiki.bash-hackers.org/commands/builtin/printf
for i in {6..11}; do printf "201309%02d.txt.bz2\n" $i; done

casperdaghost 09-12-2013 10:53 AM

amazing - thanks


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