Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sed Script Printing new lines

Status
Not open for further replies.

felix001

Technical User
Nov 15, 2008
102
GB
Ive recently written a small script, to reformat the titles of my music files.

The script creates a new filename and then moves the file...

#!/bin/sh

/bin/ls | /bin/egrep -i ".wma|.mp3|.wav" > /dev/null
if [ $? == 0 ]

then
do
for s in `ls | egrep -i ".wma|.mp3|.wav" ` ;
do
dest=$(echo $s | sed 's/^[0-9]*//;s/^-//;s/\_/ /g;s/-/ - /g') ;
/bin/mv -fv $s $dest
done
exit 0
else
echo -e '\E[31mERROR : No music files found'; tput sgr0
exit 1
fi

exit $?

To test the script on just a text file of my file names I have created the following,

#!/bin/sh

cat test.txt | egrep -i ".wma|.mp3|.wav" > /dev/null
if [ $? == 0 ]

then
for s in `cat test.txt | egrep -i ".wma|.mp3|.wav" ` ;
do
dest=$(echo $s | sed 's/^[0-9]*//;s/^-//;s/\_/ /g;s/-/ - /g') ;
echo $dest
done
exit 0
else
echo -e '\E[31mERROR : No music files found'; tput sgr0
exit 1
fi

exit $?

The problem is, is that the output is printing out the blank spaces as new lines, which i presume is down to how the for loop/sed is inputing and outing the text.

Can anyone help ??

 
Good news Ive found a solution, which involves reading the filenames into a while loop rather then a for loop...

Heres the new script...


#!/bin/sh

cat testfile.sh | /bin/egrep -i ".wma|.mp3|.wav" > /dev/null
if [ $? == 0 ]

then
cat testfile.sh | egrep -i ".wma|.mp3|.wav" | while read source
do
dest=$(echo "$source" | sed 's/^[0-9]*//;s/^-//;s/\_/ /g;s/-/ - /g') ;
echo "$dest" "$source"
done
exit 0
else
echo -e '\E[31mERROR : No music files found'; tput sgr0
exit 1
fi

exit $?


the only thing I need now is to add 2 more commands to sed.... which are

If a character is a - and it has letters either side then insert a space
Convert the first letter of each word to a capital.

If anyone can help on the sed bits that would be great...

 
s/\([[:alpha:]]\)-\([[:alpha:]]\)/\1 - \2/g should do the first part.

I'm not sure you can do the capitalisation part in sed.... at least... not easily.

Hmm.. if you're using GNU sed, there's a good solution at the bottom of this thread.

Annihilannic.
 
Thanks for your reply in the end I found a solution, heres the final script....


#!/bin/sh

ls | /bin/egrep -i ".wma|.mp3|.wav" > /dev/null
if [ $? == 0 ]

then
ls | /bin/egrep -i ".wma|.mp3|.wav" | while read source
do
dest=$(echo "$source" | /bin/sed 's/^[0-9]*//;s/^-//;s/\_/ /g;s/^ *//g;;s/.*/\L&/g;s/-/ - /g;s/ / /g;s/\<./\u&/g;s/...$/\L&/') ;
/bin/mv -fv "$source" "$dest"
done
exit 0
else
echo -e "\E[31mERROR : No music files found in `pwd`"; tput sgr0
exit 1
fi

exit $?


Even though the sed part isnt the most elegant, it does the job. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top