LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-08-2011, 05:57 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
"001" and "0012" converted to "01", "12". How?


Bash 3.1.7

Hi:
I have the following two examples of parameter substitution:
Code:
    old1="${old%:*}"     #strips off everything from the final ":" to the end.  
    old2="${old##*:}"    #strips off everything from the beginning to the final ":".
Now, let me have
Code:
    CONT=1
    while [ 1 ]
    do
         echo FILE"$CONT".txt
         ((CONT = CONT + 1))
    done
That would output
FILE1
FILE2
...
FILE9
FILE10
...

But suppose what I really want the script to output is:
FILE01
FILE02
...
FILE09
FILE10
...

I think the first two examples could help. First, I would do
CONT="00$CONT"
This would give these values for CONT:
001
002
...
009
0010

Next, it is a matter of keeping the last two caracters of CONT and strip the rest off. But how?

Last edited by stf92; 07-08-2011 at 05:59 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 07-08-2011, 06:04 PM   #2
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
There is a command printf which will allow an easy formatting:
Code:
$ printf "FILE%03d\n" 42
 
2 members found this post helpful.
Old 07-08-2011, 06:21 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Reuti's solution is nice and clean. I unnecessarily started writing nested while loops
 
Old 07-08-2011, 06:27 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
+1 to Reuti, hadn't thought that it can be so simple.
 
Old 07-08-2011, 06:31 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by stf92 View Post
Bash 3.1.7

Hi:
I have the following two examples of parameter substitution:
Code:
    old1="${old%:*}"     #strips off everything from the final ":" to the end.  
    old2="${old##*:}"    #strips off everything from the beginning to the final ":".
Now, let me have
Code:
    CONT=1
    while [ 1 ]
    do
         echo FILE"$CONT".txt
         ((CONT = CONT + 1))
    done
That would output
FILE1
FILE2
...
FILE9
FILE10
...

But suppose what I really want the script to output is:
FILE01
FILE02
...
FILE09
FILE10
...

I think the first two examples could help. First, I would do
CONT="00$CONT"
This would give these values for CONT:
001
002
...
009
0010

Next, it is a matter of keeping the last two caracters of CONT and strip the rest off. But how?
And an alternative solution:
Code:
for i in {001..099};do echo FILE$i; done
Cheers,
Tink
 
1 members found this post helpful.
Old 07-08-2011, 06:37 PM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
$ printf "%02d\n" 1
01
$

OK. The 0 after % pads with zeroes. d is the decimal radix spec. Now

$ printf "%02d\n" 012
10
$

012, because of the leading zero, is interpreted in octal! How is this?

@Tinkster: I think I'd give the whole script:
Code:
#!/bin/bash
#                       
#                 
# This script takes a .cue file as input and the corresponding .flac,
# producing a set of .wav files. I suggest to keep the .cue and this 
# file in the same dir. The .cue file is CD5\ Nos.17\,18\,19\,20\,21.cue.
# The .flac is CD5 Nos.17,18,19,20,21.flac.

# WARNING: erase all .wav previously written by this script before running.
#          flac writes the .wav as read only. It it finds a .wav file of
#          the same name from a previous run, it will try to overwrite it.
#          But the file will be read only.

file=$1
infile=$1".cue"
FLACFILE=$1".flac"
FIRST=1
CONT=01

while read A1 A2 A3            # TRACK 08 AUDIO
do
if [ "$A1" == "TRACK" ] 
then 
        read A1 A2             # TITLE "Piano ..........."
        read A1 A2 A3          # INDEX 00 40:18:43 (mm:ss:ff)
                               # We must transform mm:ss:ff into mm:ss.ss
                               # where ss.ss is secons and decimal fraction
        old="$new"
        new="$A3"              # 40:18:43
        if [ $FIRST == 1 ]
        then
                FIRST=0
        else
                old1="${old%:*}"     #strips off everything from the final ":" to the end.
                new1="${new%:*}"
                old2="${old##*:}"    #strips off everything from the beginning to the final ":".
                new2="${new##*:}"
                (("old2 = 10#$old2 * 100 / 75"))      # ff -> ss/75
                (("new2 = 10#$new2 * 100 / 75"))

                old1=$old1"."$old2                    # old1= mm:ss.ss 
                new1=$new1"."$new2

                WAVFILE="$file"$CONT".wav"
                echo "$old1  $new1 $WAVFILE"
#               flac -f --decode --skip=$old1 --until=$new1 -o "$WAVFILE" "$FLACFILE"
                ((CONT = CONT + 1))
        fi
fi

done < "$infile"
WAVFILE="$file"$CONT".wav"
echo "$new1 $WAVFILE"
#flac -f --decode --skip=$new1 -o "$WAVFILE" "$FLACFILE"
exit
Thanks.

Last edited by stf92; 07-08-2011 at 06:46 PM.
 
Old 07-08-2011, 06:47 PM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by stf92 View Post
$ printf "%02d\n" 1
01
$

OK. The 0 after % pads with zeroes. d is the decimal radix spec. Now

$ printf "%02d\n" 012
10
$

012, because of the leading zero, is interpreted in octal! How is this?


Where did you take 012 from? Wouldn't you need:

Code:
printf "FILE%02d\n" "$CONT"
or

Code:
printf "FILE%03d\n" "$CONT"

Last edited by sycamorex; 07-08-2011 at 06:49 PM. Reason: nevermind
 
Old 07-08-2011, 06:55 PM   #8
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Yes, that exactly what I would need. And at present I am reading http://www.gnu.org/software/libc/man...rmatted-Output

I know I will find here what I need. The info and man pages for printf merely say it is similar to the C printf function, point out the differences.

EDIT: Here is the full code with due modifications plus its output:
Code:
semoi@darkstar:~/BEETHOVEN/Wilhelm Backhaus - The 32 Beethoven Piano Sonatas/CD5/CD5_done$ cat t18.sh
#!/bin/bash
#                       
#                 
# This script takes a .cue file as input and the corresponding .flac,
# producing a set of .wav files. I suggest to keep the .cue and this 
# file in the same dir. The .cue file is CD5\ Nos.17\,18\,19\,20\,21.cue.
# The .flac is CD5 Nos.17,18,19,20,21.flac.

# WARNING: erase all .wav previously written by this script before running.
#          flac writes the .wav as read only. It it finds a .wav file of
#          the same name from a previous run, it will try to overwrite it.
#          But the file will be read only.

file=$1
infile=$1".cue"
FLACFILE=$1".flac"
FIRST=1
CONT=01

while read A1 A2 A3            # TRACK 08 AUDIO
do
if [ "$A1" == "TRACK" ] 
then 
        read A1 A2             # TITLE "Piano ..........."
        read A1 A2 A3          # INDEX 00 40:18:43 (mm:ss:ff)
                               # We must transform mm:ss:ff into mm:ss.ss
                               # where ss.ss is secons and decimal fraction
        old="$new"
        new="$A3"              # 40:18:43
        if [ $FIRST == 1 ]
        then
                FIRST=0
        else
                old1="${old%:*}"     #strips off everything from the final ":" to the end.
                new1="${new%:*}"
                old2="${old##*:}"    #strips off everything from the beginning to the final ":".
                new2="${new##*:}"
                (("old2 = 10#$old2 * 100 / 75"))      # ff -> ss/75
                (("new2 = 10#$new2 * 100 / 75"))

                old1=$old1"."$old2                    # old1= mm:ss.ss 
                new1=$new1"."$new2

                printf -v CONT2 "%002d" $CONT
                WAVFILE="$file"$CONT2".wav"
                echo "$old1  $new1 $WAVFILE"
#               flac -f --decode --skip=$old1 --until=$new1 -o "$WAVFILE" "$FLACFILE"
                ((CONT = CONT + 1))
        fi
fi

done < "$infile"
printf -v CONT2 "%002d" $CONT
WAVFILE="$file"$CONT2".wav"
echo "$new1 $WAVFILE"
#flac -f --decode --skip=$new1 -o "$WAVFILE" "$FLACFILE"
exit

semoi@darkstar:~/BEETHOVEN/Wilhelm Backhaus - The 32 Beethoven Piano Sonatas/CD5/CD5_done$ ./t18.sh CD5\ Nos.17\,18\,19\,20\,21     
00:00.0  08:28.13 CD5 Nos.17,18,19,20,2101.wav
08:28.13  14:45.26 CD5 Nos.17,18,19,20,2102.wav
14:45.26  21:13.97 CD5 Nos.17,18,19,20,2103.wav
21:13.97  27:35.57 CD5 Nos.17,18,19,20,2104.wav
27:35.57  31:21.33 CD5 Nos.17,18,19,20,2105.wav
31:21.33  35:28.26 CD5 Nos.17,18,19,20,2106.wav
35:28.26  40:18.0 CD5 Nos.17,18,19,20,2107.wav
40:18.0  43:53.40 CD5 Nos.17,18,19,20,2108.wav
43:53.40  47:21.77 CD5 Nos.17,18,19,20,2109.wav
47:21.77  50:27.84 CD5 Nos.17,18,19,20,2110.wav
50:27.84  53:40.97 CD5 Nos.17,18,19,20,2111.wav
53:40.97  62:36.89 CD5 Nos.17,18,19,20,2112.wav
62:36.89  65:53.17 CD5 Nos.17,18,19,20,2113.wav
65:53.17 CD5 Nos.17,18,19,20,2114.wav
semoi@darkstar:~/BEETHOVEN/Wilhelm Backhaus - The 32 Beethoven Piano Sonatas/CD5/CD5_done

Last edited by stf92; 07-08-2011 at 07:48 PM.
 
  


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 can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 07:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM

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

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