Fedora Linux Support Community & Resources Center
  #1  
Old 1st January 2008, 07:00 PM
Razza Offline
Registered User
 
Join Date: Jul 2007
Posts: 15
sed using varibles for input/output

I am trying to use sed to substitute between two variables (inp and outp). I have got the following to work, except sed appears to treat spaces as a delimeter - can any one help?

export outp=`$inp |sed "/s[.]wav/.mp3"`
Reply With Quote
  #2  
Old 1st January 2008, 10:32 PM
scottro's Avatar
scottro Offline
Retired Community Manager -- Banned from Texas by popular demand.
 
Join Date: Sep 2007
Location: NYC
Posts: 8,142
What about sed '/s[.]wav/.mp3/'

I just tried it that way (echoing my song.wav) and it worked.

You should always be a bit more specific with error messages--my guess, if you're typing it exactly as you did above, is that at least part of the problem is the lack of a / after mp3.

HTH
Reply With Quote
  #3  
Old 1st January 2008, 10:58 PM
lmo Offline
Registered User
 
Join Date: Mar 2007
Posts: 1,047
export outp=`echo $inp | sed "s/[.]wav/.mp3/"`

- the variable inp needed to be echoed instead of executed
- the first / was on the wrong side of s
- the final / was missing
Reply With Quote
  #4  
Old 1st January 2008, 11:15 PM
Razza Offline
Registered User
 
Join Date: Jul 2007
Posts: 15
Quote:
Originally Posted by scottro
What about sed '/s[.]wav/.mp3/'
You should always be a bit more specific with error messages
Hi thanks for your comments - you are correct I omitted the trailing slash, in the post.
I've always found (unfortunately) the more text you enter the quicker people lose interest - see my other posts there are only a few, with no responses

Anyway the backgroud to my project.....
I have all my CD's ripped to disk as wav files. I want to write an app which looks through the (input) wav directory structure, then through the destination mp3 structure, where it finds files on the input and not the output, it uses LAME to create an mp3 version in an identical directory structure to the wav original.

For example:
The source file would be - /data/wav/Visage/The Damned Don't Cry/04-Mind Of A Toy.wav
The destination would be - /data/mp3/Visage/The Damned Don't Cry/04-Mind Of A Toy.mp3
When the input root is set to - /data/wav
and the output root is set to - /data/mp3

The following is my script so far (please excuse all the remarking etc.):
#! /bin/sh
# var init
input_dir_root=
output_dir_root=
bitrate=
razza=0
ray=

while getopts i:o:b: options; do
case "$options" in
i) input_dir_root="$OPTARG" ;;
o) output_dir_root="$OPTARG" ;;
b) bitrate="$OPTARG" ;;
esac
done
shift $(( $OPTIND - 1 ))

echo ================================================== ==========
echo Rays WAV to MP3 LAME wrapper
echo ------------------------------------------------------------
echo Input Root = $input_dir_root
echo Output Root = $output_dir_root
echo Bitrate = $bitrate
echo Tempfile = $input_dir_root/razza.txt
echo ------------------------------------------------------------
echo Files to convert:

### Delete files (infile etc.)
rm /data/infile.txt
rm /data/convfile.txt

### list files that exist in source root.
find $input_dir_root -name *.wav | xargs -0 echo >> /data/infile.txt

#Read /data/infile check destination for file existence, then create list of files to be converted.
#cat /data/infile.txt |while read LINE
#do echo "${LINE}"
#Replace .wav extension with .mp3
#read razza
#sed -e 's/[.]wav/.mp3/' > /data/convfile.txt
#${infile_line}
#[[ -f ${line} ]] && echo "File exists" || echo "File does not exists"
#done

while read LINE
do
echo $LINE
#read razza
#echo -e $LINE/ray2 >> /data/ray
#sed "s/[.]wav/.mp3/" >$ray <$LINE
export ray=`$LINE |sed 's/[.]wav/.mp3/'`
echo -e $ray >> /data/convfile.txt
done </data/infile.txt

echo ================================================== ==========


The contents of /data/infile.txt are:
/data/wav/ABC/Absolutely/03-All Of My Heart.wav
/data/wav/ABC/Absolutely/04-Tears Are Not Enough.wav
/data/wav/ABC/Absolutely/01-Poison Arrow.wav
/data/wav/ABC/Absolutely/02-The Look Of Love.wav
/data/wav/Visage/The Damned Don't Cry/04-Mind Of A Toy.wav
/data/wav/Visage/The Damned Don't Cry/03-Fade To Grey.wav
/data/wav/Visage/The Damned Don't Cry/02-Tar.wav
/data/wav/Visage/The Damned Don't Cry/01-Visage.wav
/data/wav/Visage/Visage/04-Tar.wav
/data/wav/Visage/Visage/02-Blocks On Blocks.wav
/data/wav/Visage/Visage/01-Visage.wav
/data/wav/Visage/Visage/03-The Dancer.wav
/data/wav/Blondie/Atomic/01-Blondie Atomic.wav
/data/wav/Blondie/Atomic/02-Heart Of Glass.wav
/data/wav/Blondie/Atomic/03-Sunday Girl.wav
/data/wav/Blondie/Atomic/04-Call Me.wav


The script output/error messages are:
================================================== =========
Rays WAV to MP3 LAME wrapper
------------------------------------------------------------
Input Root = /data/wav
Output Root = /data/mp3
Bitrate =
Tempfile = /data/wav/razza.txt
------------------------------------------------------------
Files to convert:
/data/wav/ABC/Absolutely/03-All Of My Heart.wav
razza2.sh: line 106: /data/wav/ABC/Absolutely/03-All: No such file or directory
/data/wav/ABC/Absolutely/04-Tears Are Not Enough.wav
razza2.sh: line 106: /data/wav/ABC/Absolutely/04-Tears: No such file or directory
/data/wav/ABC/Absolutely/01-Poison Arrow.wav
razza2.sh: line 106: /data/wav/ABC/Absolutely/01-Poison: No such file or directory
/data/wav/ABC/Absolutely/02-The Look Of Love.wav
razza2.sh: line 106: /data/wav/ABC/Absolutely/02-The: No such file or directory
/data/wav/Visage/The Damned Don't Cry/04-Mind Of A Toy.wav
razza2.sh: line 106: /data/wav/Visage/The: No such file or directory
/data/wav/Visage/The Damned Don't Cry/03-Fade To Grey.wav
razza2.sh: line 106: /data/wav/Visage/The: No such file or directory
/data/wav/Visage/The Damned Don't Cry/02-Tar.wav
razza2.sh: line 106: /data/wav/Visage/The: No such file or directory
/data/wav/Visage/The Damned Don't Cry/01-Visage.wav
razza2.sh: line 106: /data/wav/Visage/The: No such file or directory
/data/wav/Visage/Visage/04-Tar.wav
razza2.sh: line 106: /data/wav/Visage/Visage/04-Tar.wav: cannot execute binary file
/data/wav/Visage/Visage/02-Blocks On Blocks.wav
razza2.sh: line 106: /data/wav/Visage/Visage/02-Blocks: No such file or directory
/data/wav/Visage/Visage/01-Visage.wav
razza2.sh: line 106: /data/wav/Visage/Visage/01-Visage.wav: cannot execute binary file
/data/wav/Visage/Visage/03-The Dancer.wav
razza2.sh: line 106: /data/wav/Visage/Visage/03-The: No such file or directory
/data/wav/Blondie/Atomic/01-Blondie Atomic.wav
razza2.sh: line 106: /data/wav/Blondie/Atomic/01-Blondie: No such file or directory
/data/wav/Blondie/Atomic/02-Heart Of Glass.wav
razza2.sh: line 106: /data/wav/Blondie/Atomic/02-Heart: No such file or directory
/data/wav/Blondie/Atomic/03-Sunday Girl.wav
razza2.sh: line 106: /data/wav/Blondie/Atomic/03-Sunday: No such file or directory
/data/wav/Blondie/Atomic/04-Call Me.wav
razza2.sh: line 106: /data/wav/Blondie/Atomic/04-Call: No such file or directory

================================================== ==========


I actually now think I have a more fundamental problem as the script seems to be trying to push the actual .wav file through sed as I have the error:
razza2.sh: line 106: /data/wav/Visage/Visage/01-Visage.wav: cannot execute binary file
/data/wav/Visage/Visage/03-The Dancer.wav


My sed is still complaining about spaces as evidenced in the following:
razza2.sh: line 106: /data/wav/Visage/Visage/03-The: No such file or directory

against the file:/data/wav/Visage/Visage/03-The Dancer.wav

And /data/convfile.txt contains nothing
Any views?
Reply With Quote
  #5  
Old 1st January 2008, 11:22 PM
Razza Offline
Registered User
 
Join Date: Jul 2007
Posts: 15
Quote:
Originally Posted by lmo
export outp=`echo $inp | sed "s/[.]wav/.mp3/"`

- the variable inp needed to be echoed instead of executed
- the first / was on the wrong side of s
- the final / was missing
I must learn to cut and paste in future, I did have the slashes in the corect places, as you can see from my above post.
I did not have the echo in there, just tested and it works fine!
Thanks so so so much, thats taken me about 5 hours
Reply With Quote
  #6  
Old 2nd January 2008, 12:14 AM
Razza Offline
Registered User
 
Join Date: Jul 2007
Posts: 15
Staying with my theme of "sed using varibles for input/output", why do I get a failure with the following failure with either of these lines of script:
export raya=`echo $LINE |sed 's/'$input_dir_root'/'$output_dir_root'/'`
export raya=`echo $LINE |sed 's/'"$input_dir_root"'/'"$output_dir_root"'/'`


It appears to me that sed is interpreting the slashes within the variables as metacharacters, as opposed to simply part of the string.
$input_dir_root = /data/wav and $output_dir_root = /data/mp3

Th failure/output is see is:
sed: couldn't open file av//data/mp3/: No such file or directory
Reply With Quote
  #7  
Old 3rd January 2008, 04:50 PM
Razza Offline
Registered User
 
Join Date: Jul 2007
Posts: 15
Quote:
Originally Posted by Razza
Staying with my theme of "sed using varibles for input/output", why do I get a failure with the following failure with either of these lines of script:
export raya=`echo $LINE |sed 's/'$input_dir_root'/'$output_dir_root'/'`
export raya=`echo $LINE |sed 's/'"$input_dir_root"'/'"$output_dir_root"'/'`


It appears to me that sed is interpreting the slashes within the variables as metacharacters, as opposed to simply part of the string.
$input_dir_root = /data/wav and $output_dir_root = /data/mp3

Th failure/output is see is:
sed: couldn't open file av//data/mp3/: No such file or directory
Anyone able to assist?
Reply With Quote
  #8  
Old 3rd January 2008, 06:23 PM
lmo Offline
Registered User
 
Join Date: Mar 2007
Posts: 1,047
inp='\/hello\/you\/all\/'
outp='\/good\/bye\/everybody\/'
LINE=/hello/you/all/THISISWHATITIS
echo $LINE | sed -e "s/$inp/$outp/"

prints
/good/bye/everybody/THISISWHATITIS

Also, any spaces that get into the filenames need to get backslashed.
In my opinion, window's concept of spaces in filenames is twisted.

Last edited by lmo; 3rd January 2008 at 06:37 PM.
Reply With Quote
  #9  
Old 3rd January 2008, 06:38 PM
Razza Offline
Registered User
 
Join Date: Jul 2007
Posts: 15
Sorry i'm missing something, how does this affect whats already stored in a variable (in my case a directory path)?
I guess I would have to echo the variable through sed and convert all the slashes to forward and backslashes, but then as i'm pushing it through sed surely I will still have problems?
Reply With Quote
  #10  
Old 3rd January 2008, 06:50 PM
lmo Offline
Registered User
 
Join Date: Mar 2007
Posts: 1,047
As far as I know, sed doesn't have a way to use an alternate delimiter for regular expressions.
Perl can allow you to use a different delimiter for regular expressions.
awk may provide some alternate strategies.
Substitutions are possible in bash.
See man bash and lookup /\{parameter
Then, with backslashing things, the shell eats up as many backslashes as it can so that it is tricky to get enough backslashes into the the string to do what you want it to.
I am not a genius with sed or with getting things slashed out the way you want.
The best I can do is hack at it until it does the right thing, but there are people that can do this kind of stuff without looking.
Reply With Quote
  #11  
Old 4th January 2008, 01:27 AM
scottro's Avatar
scottro Offline
Retired Community Manager -- Banned from Texas by popular demand.
 
Join Date: Sep 2007
Location: NYC
Posts: 8,142
You can use anything as delineator--it takes the first character.

For example

echo this is bad| sed 's/good/bad/'

This also works

echo this is bad|sed 's:good:bad:'

You could even use a letter if you want to really confuse yourself. As long as that letter isn't in the expression. For example

echo this is bad|sed 'sabadagooda'
will give you an error because there's already an a in bad

However
echo this is bad|sed 'scbadcgoodc'
will work.
Obviously, you don't want to use a letter, but you can use a ; : $, etc.
Reply With Quote
  #12  
Old 4th January 2008, 01:53 AM
lmo Offline
Registered User
 
Join Date: Mar 2007
Posts: 1,047
inp=/hello/you/all/
outp=/thats/so/cool/
line=/hello/you/all/THANKS
echo $line | sed s@$inp@$outp@

prints

/thats/so/cool/THANKS

Having fun playing with these:
echo $outp${line#$inp}
echo ${line/$inp/$outp}
... Look Ma, no sed

also prints
/thats/so/cool/THANKS

Last edited by lmo; 4th January 2008 at 03:32 AM.
Reply With Quote
  #13  
Old 4th January 2008, 02:50 AM
scottro's Avatar
scottro Offline
Retired Community Manager -- Banned from Texas by popular demand.
 
Join Date: Sep 2007
Location: NYC
Posts: 8,142
Yes, it can be fun.
Of course, one tries to pick a delinator that is less confusing, but the silly ones are fun.
Reply With Quote
  #14  
Old 4th January 2008, 08:45 AM
Razza Offline
Registered User
 
Join Date: Jul 2007
Posts: 15
But this doesn't help me when the string I have to manipulate contains slashes (i.e. directory paths)?
Reply With Quote
  #15  
Old 4th January 2008, 01:58 PM
scottro's Avatar
scottro Offline
Retired Community Manager -- Banned from Texas by popular demand.
 
Join Date: Sep 2007
Location: NYC
Posts: 8,142
echo this/is/good |sed 's:good:bad:'

this/is/bad

Should work unless I'm missing something.
Reply With Quote
Reply

Tags
input or output, sed, varibles

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
input/output error talkstock888 Hardware & Laptops 4 17th May 2006 10:54 PM
Input/Output error after 'lvreduce' chillz Using Fedora 6 11th November 2005 05:37 AM
cpio input/output error mattma Using Fedora 3 17th November 2004 03:19 PM


Current GMT-time: 22:36 (Thursday, 23-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat