BashScripts.org
http://bashscripts.org/forum/

String array concatenation
http://bashscripts.org/forum/viewtopic.php?f=16&t=844
Page 1 of 1

Author:  jocasa [ Tue Jul 21, 2009 12:14 pm ]
Post subject:  String array concatenation

Hi, I have a problem with concatenating two array values to form a single string. Here's the code:

Code:
old_IFS=$IFS
IFS=$'\n'

performer=(`grep "PERFORMER" "$2" | sed -e 's/PERFORMER//g' -e 's/\"//g' -e 's/^ *//g'`)
title=(`grep "TITLE" "$2" | sed -e 's/TITLE//g' -e 's/\"//g' -e 's/^ *//g' -e 's/$/\.mp3/g' -e 's/\r//g'`)

IFS=$old_IFS

echo "${performer[2]}"
echo "${title[2]}"

final_title="${performer[2]} - ${title[2]}"
echo "$final_title"


The script reads the title and performer fields from a cue sheet ($2), makes changes to them via sed and stores them in two arrays. I want to construct the filename as "performer - title", but the resulting string appears like this when I execute the script:

Quote:
Francesco Durante (this is the performer[2])
Magnificat - Et misericordia (Solo Soprano).mp3 (this is the title[2])
- Magnificat - Et misericordia (Solo Soprano).mp3 (this is the resulting concatenation)


It looks like the title string has overlapped the performer string. Do you have any idea of how to make this work?

Thanks! :)

Author:  Watael [ Tue Jul 21, 2009 1:27 pm ]
Post subject:  Re: String array concatenation

Hi,

Could you give some input (cue sheet) sample, please.

Author:  jocasa [ Tue Jul 21, 2009 1:40 pm ]
Post subject:  Re: String array concatenation

Yes, here it is the beggining of the cue sheet:

PERFORMER "Thomas Hengelbrock"
TITLE "Deutsche Harmonia Mundi - 50 years"
FILE "Deutsche Harmonia Mundi 01.ape" WAVE
TRACK 01 AUDIO
TITLE "Magnificat - Magnificat anima mea (coro)"
PERFORMER "Francesco Durante"
INDEX 01 00:00:00
TRACK 02 AUDIO
TITLE "Magnificat - Et misericordia (Solo Soprano)"
PERFORMER "Francesco Durante"
INDEX 01 02:35:40
TRACK 03 AUDIO
TITLE "Magnificat - Fecit potentiam (Coro)"
PERFORMER "Francesco Durante"
INDEX 01 04:26:60
[...]

Author:  Watael [ Tue Jul 21, 2009 3:02 pm ]
Post subject:  Re: String array concatenation

As arrays take space to separate items, you need to explicitly tell what datas are assigned to a "row".

Here's what I did
Code:
#!/bin/bash
#set -x

data="jocasa-20090721.dat"

while read line
do
   if [[ ${line%% *} =~ PERFORMER ]]
    then
      performer[i++]=$(sed -n 's/^PERFORMER "\([^"]*\)"/\1/p' <<<"$line")
   elif [[ ${line%% *} =~ TITLE ]]
    then
      title[j++]=$(sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p' <<<"$line")
   fi
done < $data


echo "${performer[2]} - ${title[2]}"

that returns:
Francesco Durante - Magnificat - Et misericordia (Solo Soprano).mp3

Author:  jocasa [ Tue Jul 21, 2009 3:07 pm ]
Post subject:  Re: String array concatenation

Thank you man, but now I have another trouble: understanding your code!
I think I'll read it and read until I get it.
(I'm a newby, you see...) :))

Author:  jocasa [ Tue Jul 21, 2009 3:15 pm ]
Post subject:  Re: String array concatenation

Hi again.
I have just tried your code and sadly I got the same problem than before.
Could it be a misconfiguration of something in my machine?

Author:  Watael [ Tue Jul 21, 2009 4:04 pm ]
Post subject:  Re: String array concatenation

what does return:
Code:
[[ $SHELL == /bin/bash ]] && $SHELL --version || echo "bash is not the active shell"

how do you launch the script?

You can try a slightly different way:
Code:
#!/bin/bash

#set -x

data="jocasa-20090721.dat"

while read line
do
   set $line
   if [[ $1 == PERFORMER ]]
    then
      performer[i++]=$(sed -n 's/^PERFORMER "\([^"]*\)"/\1/p' <<< "$line")
   elif [[ $1 == TITLE ]]
    then
      title[j++]=$(sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p' <<< "$line")
   fi
done < $data


echo "${performer[2]} - ${title[2]}"


In case it doesn't work, uncomment the 'set -x' line, that would give some hints on what doesn't work.

Author:  jocasa [ Wed Jul 22, 2009 1:10 am ]
Post subject:  Re: String array concatenation

Hi, this is the output of the command:

Code:
GNU bash, version 3.2.39(1)-release (i586-suse-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.


It still doesn't work properly. Everything seems right when looking at the output of the script with the command "set -x". The problem seems to be the way two string arrays concatenate, because the rest of the script runs ok.

Thanks for your help!

Author:  Watael [ Wed Jul 22, 2009 3:28 am ]
Post subject:  Re: String array concatenation

Well, we have the same bash version, and as such it should work entirely.
Assuming you made your script executable, do
Code:
./jocasa-20090721.sh > jocasa.log 2>&1
with the input sample and set -x uncommented, and paste the log file here: we will see where it hurts.

Author:  fredrik.eriksson [ Wed Jul 22, 2009 9:10 am ]
Post subject:  Re: String array concatenation

Hi,

Watael is on the right track, I would do it with a for loop thou.
Code:
#!/bin/bash
file=$1
IFS="
"
for i in $(cat $file); do
   test_line=$(echo $i | cut -d' ' -f1)
   case $test_line in
      PERFORMER)
            song[0]=$(echo $i | cut -d'"' -f2)
            echo_it=0
         ;;
      TITLE)
            song[1]=$(echo $i | cut -d'"' -f2)
            echo_it=1
         ;;
   esac
   if [ $echo_it -eq 1 ]; then
      echo "${song[0]} - ${song[1]}.mp3";
      run=0
   fi
done


This might be somewhat easier to understand.
Basically what it does is looping throu using for and IFS (as you've done in your first post.).
IFS does not need to be reset if you're running it in a script, scripts run in it's own subshell so all exports gets unset when it exists.
Code:
[email protected]:~> cat demo.sh
#!/bin/bash
IFS="
"
[email protected]:~> sh demo.sh
[email protected]:~> echo \"$IFS\"
" "


Also, IFS doesn't always like \n... this is because there's so many ways of doing a carriage return... \r is another method or if you run windows \r\n.

To get back to the topic. $test_line just cut's out the first (-f1) "column" separated by a space character (-d' ' in the cut command).
Next we run it thru case since it's easier to pattern match and extend it imo :)
The next cut just cut's out everything beyond the first quotation mark until the next one.

Hope this gives you a clue :)

ps. If you chmod +x a script with a shebang then it will execute with that if it's runned with ./script.sh, this works for whatever executor you want to use (ex sh, perl, php and so on) ds.

Best regards
Fredrik Eriksson

Author:  jocasa [ Wed Jul 22, 2009 10:56 am ]
Post subject:  Re: String array concatenation

Ok, first I post here what Watael asked me to run (sorry, it's a long listing):

Code:
+ read line
+ set PERFORMER '"Thomas' 'Hengelbrock"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Thomas Hengelbrock
'
+ read line
+ set TITLE '"Deutsche' Harmonia Mundi - 50 'years"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Deutsche Harmonia Mundi - 50 years.mp3
'
+ read line
+ set FILE '"Deutsche' Harmonia Mundi '01.ape"' $'WAVE\r'
+ [[ FILE == PERFORMER ]]
+ [[ FILE == TITLE ]]
+ read line
+ set TRACK 01 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Magnificat' - Magnificat anima mea '(coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Magnificat - Magnificat anima mea (coro).mp3
'
+ read line
+ set PERFORMER '"Francesco' 'Durante"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Francesco Durante
'
+ read line
+ set INDEX 01 $'00:00:00\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 02 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Magnificat' - Et misericordia '(Solo' 'Soprano)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Magnificat - Et misericordia (Solo Soprano).mp3
'
+ read line
+ set PERFORMER '"Francesco' 'Durante"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Francesco Durante
'
+ read line
+ set INDEX 01 $'02:35:40\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 03 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Magnificat' - Fecit potentiam '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Magnificat - Fecit potentiam (Coro).mp3
'
+ read line
+ set PERFORMER '"Francesco' 'Durante"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Francesco Durante
'
+ read line
+ set INDEX 01 $'04:26:60\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 04 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Magnificat' - Deposuit potentes '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Magnificat - Deposuit potentes (Coro).mp3
'
+ read line
+ set PERFORMER '"Francesco' 'Durante"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Francesco Durante
'
+ read line
+ set INDEX 00 $'04:56:24\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set INDEX 01 $'04:56:25\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 05 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Magnificat' - Suscepit Israel '(Duetto' 'Tenore/Basso)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Magnificat - Suscepit Israel (Duetto Tenore/Basso).mp3
'
+ read line
+ set PERFORMER '"Francesco' 'Durante"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Francesco Durante
'
+ read line
+ set INDEX 01 $'06:48:37\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 06 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Magnificat' - Sicut locutus est '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Magnificat - Sicut locutus est (Coro).mp3
'
+ read line
+ set PERFORMER '"Francesco' 'Durante"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Francesco Durante
'
+ read line
+ set INDEX 01 $'08:51:55\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 07 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Magnificat' - Gloria Patri '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Magnificat - Gloria Patri (Coro).mp3
'
+ read line
+ set PERFORMER '"Francesco' 'Durante"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Francesco Durante
'
+ read line
+ set INDEX 01 $'09:25:17\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 08 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Magnificat' - Sicut erat in principio '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Magnificat - Sicut erat in principio (Coro).mp3
'
+ read line
+ set PERFORMER '"Francesco' 'Durante"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Francesco Durante
'
+ read line
+ set INDEX 01 $'10:10:05\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 09 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Stabat' Mater - Stabat Mater '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Stabat Mater - Stabat Mater (Coro).mp3
'
+ read line
+ set PERFORMER '"Emanuele' 'd'\''Astorga"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Emanuele d'\''Astorga
'
+ read line
+ set INDEX 00 $'12:06:37\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set INDEX 01 $'12:11:50\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 10 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Stabat' Mater - O quam tristis '(Terzetto' 'Soprano/Tenore/Basso)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Stabat Mater - O quam tristis (Terzetto Soprano/Tenore/Basso).mp3
'
+ read line
+ set PERFORMER '"Emanuele' 'd'\''Astorga"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Emanuele d'\''Astorga
'
+ read line
+ set INDEX 01 $'17:33:65\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 11 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Stabat' Mater - Quis est homo '(Duetto' Alto/Soprano, Duetto 'Tenore/Basso)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Stabat Mater - Quis est homo (Duetto Alto/Soprano, Duetto Tenore/Basso).mp3
'
+ read line
+ set PERFORMER '"Emanuele' 'd'\''Astorga"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Emanuele d'\''Astorga
'
+ read line
+ set INDEX 01 $'20:35:22\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 12 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Stabat' Mater - Eja mater '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Stabat Mater - Eja mater (Coro).mp3
'
+ read line
+ set PERFORMER '"Emanuele' 'd'\''Astorga"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Emanuele d'\''Astorga
'
+ read line
+ set INDEX 01 $'24:13:12\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 13 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Stabat' Mater - Sancta Mater '(Solo' 'soprano)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Stabat Mater - Sancta Mater (Solo soprano).mp3
'
+ read line
+ set PERFORMER '"Emanuele' 'd'\''Astorga"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Emanuele d'\''Astorga
'
+ read line
+ set INDEX 01 $'27:20:30\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 14 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Stabat' Mater - Fac me tecum '(Duetto' 'Alto/Tenore)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Stabat Mater - Fac me tecum (Duetto Alto/Tenore).mp3
'
+ read line
+ set PERFORMER '"Emanuele' 'd'\''Astorga"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Emanuele d'\''Astorga
'
+ read line
+ set INDEX 01 $'30:36:30\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 15 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Stabat' Mater - Virgo virginum '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Stabat Mater - Virgo virginum (Coro).mp3
'
+ read line
+ set PERFORMER '"Emanuele' 'd'\''Astorga"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Emanuele d'\''Astorga
'
+ read line
+ set INDEX 01 $'32:58:47\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 16 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Stabat' Mater - Fac me plagis '(Solo' 'Basso)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Stabat Mater - Fac me plagis (Solo Basso).mp3
'
+ read line
+ set PERFORMER '"Emanuele' 'd'\''Astorga"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Emanuele d'\''Astorga
'
+ read line
+ set INDEX 01 $'35:49:65\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 17 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Stabat' Mater - Christe cum sit '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Stabat Mater - Christe cum sit (Coro).mp3
'
+ read line
+ set PERFORMER '"Emanuele' 'd'\''Astorga"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Emanuele d'\''Astorga
'
+ read line
+ set INDEX 01 $'37:27:37\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 18 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Confitebor' tibi Domine - Confitebor '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Confitebor tibi Domine - Confitebor (Coro).mp3
'
+ read line
+ set PERFORMER '"Giovanni' Battista 'Pergolesi"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Giovanni Battista Pergolesi
'
+ read line
+ set INDEX 00 $'42:05:62\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set INDEX 01 $'42:11:50\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 19 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Confitebor' tibi Domine - Confessio '(Solo' 'Soprano)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Confitebor tibi Domine - Confessio (Solo Soprano).mp3
'
+ read line
+ set PERFORMER '"Giovanni' Battista 'Pergolesi"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Giovanni Battista Pergolesi
'
+ read line
+ set INDEX 01 $'44:13:45\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 20 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Confitebor' tibi Domine - Fidelia omnia '(Solo' 'Soprano)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Confitebor tibi Domine - Fidelia omnia (Solo Soprano).mp3
'
+ read line
+ set PERFORMER '"Giovanni' Battista 'Pergolesi"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Giovanni Battista Pergolesi
'
+ read line
+ set INDEX 01 $'48:49:52\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 21 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Confitebor' tibi Domine - Redemptionem misit '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Confitebor tibi Domine - Redemptionem misit (Coro).mp3
'
+ read line
+ set PERFORMER '"Giovanni' Battista 'Pergolesi"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Giovanni Battista Pergolesi
'
+ read line
+ set INDEX 01 $'50:55:22\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 22 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Confitebor' tibi Domine - Sanctum et terribile '(Solo' 'Alto)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Confitebor tibi Domine - Sanctum et terribile (Solo Alto).mp3
'
+ read line
+ set PERFORMER '"Giovanni' Battista 'Pergolesi"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Giovanni Battista Pergolesi
'
+ read line
+ set INDEX 01 $'51:43:32\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 23 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Confitebor' tibi Domine - Intellectus bonus '(Solo' 'Alto)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Confitebor tibi Domine - Intellectus bonus (Solo Alto).mp3
'
+ read line
+ set PERFORMER '"Giovanni' Battista 'Pergolesi"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Giovanni Battista Pergolesi
'
+ read line
+ set INDEX 01 $'54:08:47\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 24 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Confitebor' tibi Domine - Gloria Patri '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Confitebor tibi Domine - Gloria Patri (Coro).mp3
'
+ read line
+ set PERFORMER '"Giovanni' Battista 'Pergolesi"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Giovanni Battista Pergolesi
'
+ read line
+ set INDEX 01 $'55:27:05\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ set TRACK 25 $'AUDIO\r'
+ [[ TRACK == PERFORMER ]]
+ [[ TRACK == TITLE ]]
+ read line
+ set TITLE '"Confitebor' tibi Domine - Sicut erat '(Coro)"
'
+ [[ TITLE == PERFORMER ]]
+ [[ TITLE == TITLE ]]
++ sed -n 's/^TITLE "\([^"]*\)"/\1.mp3/p'
+ title[j++]='Confitebor tibi Domine - Sicut erat (Coro).mp3
'
+ read line
+ set PERFORMER '"Giovanni' Battista 'Pergolesi"
'
+ [[ PERFORMER == PERFORMER ]]
++ sed -n 's/^PERFORMER "\([^"]*\)"/\1/p'
+ performer[i++]='Giovanni Battista Pergolesi
'
+ read line
+ set INDEX 01 $'56:22:27\r'
+ [[ INDEX == PERFORMER ]]
+ [[ INDEX == TITLE ]]
+ read line
+ echo 'Francesco Durante
- Magnificat - Et misericordia (Solo Soprano).mp3
'
Francesco Durante
- Magnificat - Et misericordia (Solo Soprano).mp3
+ exit 0


Second, I've run what Fredrik suggested and the result I get is this:

Code:
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
[...]


It works perfectly when making the filename but it seems the script only processes the first entry of the cue sheet (or my computer is crazy and makes strange things if it works to you :) )

Thank you guys for your interest!

Author:  jocasa [ Wed Jul 22, 2009 11:04 am ]
Post subject:  Re: String array concatenation

I think I should post the cue sheet extract again, this time with the correct formatting. I think the fact that there are some spaces before each field may make a difference.

Code:
PERFORMER "Thomas Hengelbrock"
TITLE "Deutsche Harmonia Mundi - 50 years"
FILE "Deutsche Harmonia Mundi 01.ape" WAVE
  TRACK 01 AUDIO
    TITLE "Magnificat - Magnificat anima mea (coro)"
    PERFORMER "Francesco Durante"
    INDEX 01 00:00:00
  TRACK 02 AUDIO
    TITLE "Magnificat - Et misericordia (Solo Soprano)"
    PERFORMER "Francesco Durante"
    INDEX 01 02:35:40
  TRACK 03 AUDIO
    TITLE "Magnificat - Fecit potentiam (Coro)"
    PERFORMER "Francesco Durante"
    INDEX 01 04:26:60
  TRACK 04 AUDIO
    TITLE "Magnificat - Deposuit potentes (Coro)"
    PERFORMER "Francesco Durante"
    INDEX 00 04:56:24
    INDEX 01 04:56:25
  [...]

Author:  fredrik.eriksson [ Wed Jul 22, 2009 2:58 pm ]
Post subject:  Re: String array concatenation

It makes a world of difference.

Space is a character too, can't ignore them.

Code:
[email protected]:~> sh test.sh test.txt
Thomas Hengelbrock - Deutsche Harmonia Mundi - 50 years.mp3
Francesco Durante - Magnificat - Magnificat anima mea (coro).mp3
Francesco Durante - Magnificat - Et misericordia (Solo Soprano).mp3
Francesco Durante - Magnificat - Fecit potentiam (Coro).mp3
Francesco Durante - Magnificat - Deposuit potentes (Coro).mp3
[email protected]:~> cat test.sh
#!/bin/bash
file=$1
IFS="
"
echo_it=0
for i in $(cat $file); do
   test_line=$(echo $i | sed -e "s/^\s\+//" | cut -d' ' -f1)
   case $test_line in
      PERFORMER)
            song[0]=$(echo $i | cut -d'"' -f2)
            (( echo_it++ ))
         ;;
      TITLE)
            song[1]=$(echo $i | cut -d'"' -f2)
            (( echo_it++ ))
         ;;
   esac
   if [ $echo_it -eq 2 ]; then
      echo "${song[0]} - ${song[1]}.mp3";
      echo_it=0
   fi
done


With a slight modification my method still works :)
What I've added is a more fool proof way of finding title and performer, since these obviously come in different order. Also I added a sed to $test_line to remove all whitespace characters that resides from the beginning of the line to the first non-whitespace character.

Best regards
Fredrik Eriksson

Author:  jocasa [ Wed Jul 22, 2009 4:22 pm ]
Post subject:  Re: String array concatenation

Yea, man. This time it works perfectly for me. :) Nevertheless the mystery remains of why the other ways to solve the problem don't work.

I must admit I don't fully understand the code, but anyway I'm learning and I think I'll get it very soon. I'm following the Advanced Bash-Scripting Guide (http://tldp.org/LDP/abs/html/). The idea of the script is to automate the conversion from an ape or flac format to a set of mp3s using the cue sheet. If you're interested in it I will pass it to you when I finish it.

Thanks!

Author:  Watael [ Wed Jul 22, 2009 9:04 pm ]
Post subject:  Re: String array concatenation

K'berk: converting flac to mp3 is non sense!!! I regret now being helpfull:)

Author:  fredrik.eriksson [ Thu Jul 23, 2009 12:42 am ]
Post subject:  Re: String array concatenation

jocasa wrote:
Yea, man. This time it works perfectly for me. :) Nevertheless the mystery remains of why the other ways to solve the problem don't work.

I must admit I don't fully understand the code, but anyway I'm learning and I think I'll get it very soon. I'm following the Advanced Bash-Scripting Guide (http://tldp.org/LDP/abs/html/). The idea of the script is to automate the conversion from an ape or flac format to a set of mp3s using the cue sheet. If you're interested in it I will pass it to you when I finish it.

Thanks!


The first one doesn't work because it doesn't take into account the spaces as my first draft didn't either.
The second one is the same thing.

This could be the source of all the trouble.
If sed doesn't find a match it returns the entire unedited string.

As for the code, if you read tldp.org's guide you should quite quickly understand what I've done :)

Best regards
Fredrik Eriksson

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com