LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
LinkBack Search this Thread
Old 09-09-2009, 03:52 AM   #1
slugzero
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Rep: Reputation: 0
sed guru required for help with Win32 environment variables


My example (Keep in mind this is in a Windows environment):

Code:
set /p web_path=Web address:
dir /s/b > tmp
sed-3.59 -e s,%CD%,http:\\%web_path%,g tmp >playlist.m3u
I am trying to convert the above dir output to a web friendly MP3 playlist, however, I have discovered sed doesn't parse slashes and backslashes the way I'd like (especially in the above Windows environment variables). Is there a solution to this?

TIA of course for any assistance.
 
Old 09-09-2009, 04:17 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 9,010

Rep: Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353
Welcome to LinuxQuestions!

Please, can you post an example of the output from dir /s/b and the corresponding example of the desired playlist format?
 
Old 09-09-2009, 04:24 AM   #3
agentdcooper
Member
 
Registered: Aug 2005
Distribution: Slackware
Posts: 35

Rep: Reputation: 15
not sure how this is slackware related.... and I am by no means a sed guru - but with sed you can use slashes, and other characters of this type - you just have to escape these characters with a backslash. like so ::

Code:
# cat test.txt
http:\\www.yahoo.com
http://www.netscape.com

# sed -i 's|http:\\\\|http:\/\/|g' test.txt
# cat test.txt
http://www.yahoo.com
http://www.netscape.com
so the sed command above is taking the line "http:\\www.yahoo.com" (well, just the http:// part is what matches) and changing changing it to "http://www.yahoo.com".

I hope that helps you?
 
Old 09-10-2009, 03:26 AM   #4
slugzero
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you for taking a stab at it agentdcooper. It definately got things started.

Here is some more information, colucix: (my current test.bat)

Quote:
@echo off
echo Type your web address below (ie. www.somesite.com or external IP).
set /p web_path=Web address:

echo set web_path2=%web_path% >var.bat
echo set CD2=%CD% >>var.bat

sed-3.59 -e s,\\,/,g var.bat >var2.bat
call var2.bat

dir /s/b > tmp

sed-3.59 -e s,\\,/,g tmp >tmp2 -e "s,%CD2%,%web_path2%,g" tmp2 >tmp3
Here is a sample of tmp2:

Quote:
D:/shares/Music/Mixtape (Various Artists)/01 Chick Habit.mp3
D:/shares/Music/Mixtape (Various Artists)/01 Mr. Self Destruct.mp3
D:/shares/Music/Mixtape (Various Artists)/01 Queen B.mp3
D:/shares/Music/Mixtape (Various Artists)/01 Somewhat Damaged.mp3
D:/shares/Music/Mixtape (Various Artists)/02 - king cobb steelie - below the stars.mp3
D:/shares/Music/Mixtape (Various Artists)/02 - Natural one - The Folk Implosion.mp3
D:/shares/Music/Mixtape (Various Artists)/02 Piggy.mp3
D:/shares/Music/Mixtape (Various Artists)/03 Vagina Mine.mp3
D:/shares/Music/Mixtape (Various Artists)/03 Where Is Everybody-.mp3
D:/shares/Music/Mixtape (Various Artists)/04 Don't You Evah.mp3
D:/shares/Music/Mixtape (Various Artists)/04 Momma Sed.mp3
D:/shares/Music/Mixtape (Various Artists)/04 The Wretched.mp3
D:/shares/Music/Mixtape (Various Artists)/05 - something's burning.mp3
D:/shares/Music/Mixtape (Various Artists)/05 A Million Hearts.mp3
D:/shares/Music/Mixtape (Various Artists)/05 Closer.mp3
D:/shares/Music/Mixtape (Various Artists)/05 Drunk with Power.mp3
D:/shares/Music/Mixtape (Various Artists)/05 kids oracular spectacular mgmt.mp3
D:/shares/Music/Mixtape (Various Artists)/05 We're in This Together.mp3
D:/shares/Music/Mixtape (Various Artists)/06 The Fragile.mp3
D:/shares/Music/Mixtape (Various Artists)/06 The Undertaker.mp3
D:/shares/Music/Mixtape (Various Artists)/06. Pale 3 feat. 12 Rounds - Just Another Day.mp3
D:/shares/Music/Mixtape (Various Artists)/07 lazy eye.mp3
D:/shares/Music/Mixtape (Various Artists)/07 The Becoming.mp3
D:/shares/Music/Mixtape (Various Artists)/07 Trekka.mp3
D:/shares/Music/Mixtape (Various Artists)/07-johnny_hollow-superhero.mp3
D:/shares/Music/Mixtape (Various Artists)/08 Even Deeper.mp3
D:/shares/Music/Mixtape (Various Artists)/08 I'm Looking Forward to Joining You, Finally.mp3
D:/shares/Music/Mixtape (Various Artists)/08 Indigo Children.mp3
D:/shares/Music/Mixtape (Various Artists)/09 Eyes on Fire.mp3
D:/shares/Music/Mixtape (Various Artists)/09 The Big Come Down.mp3
D:/shares/Music/Mixtape (Various Artists)/10 No, You Don't.mp3
D:/shares/Music/Mixtape (Various Artists)/10 Rev. 22-20 [Dry Martini Mix].mp3
D:/shares/Music/Mixtape (Various Artists)/12 Reptile.mp3
D:/shares/Music/Mixtape (Various Artists)/14 - pleasent smell (rethought by trent reznor).mp3
D:/shares/Music/Mixtape (Various Artists)/16 chick habit.mp3
D:/shares/Music/Mixtape (Various Artists)/41 - Pigface - Sweetmeat.mp3
D:/shares/Music/Mixtape (Various Artists)/beck - 01 - loser.mp3
D:/shares/Music/Mixtape (Various Artists)/Cake - Short Skirt Long Jacket.mp3
D:/shares/Music/Mixtape (Various Artists)/detachable penis_king missle.mp3
D:/shares/Music/Mixtape (Various Artists)/Donnie Darko - 17 - Gary Jules - Mad World.mp3
D:/shares/Music/Mixtape (Various Artists)/Donnie Darko Score - 18 - Gary Jules - Mad World (Alternate Version).mp3
D:/shares/Music/Mixtape (Various Artists)/joyous.mp3
D:/shares/Music/Mixtape (Various Artists)/Nine Inch Nails - 08 - Eraser (Polite).mp3
D:/shares/Music/Mixtape (Various Artists)/pleasant smell.mp3
I don't need to post a sample of tmp3 because it is identical to tmp2 . When fiddling w/ the script, I found substituting the variable %CD2% for just something like "Music" *DOES* substitute every instance of "Music" with the variable set as %web_path2%.
 
Old 09-10-2009, 05:30 AM   #5
niels.horn
Senior Member
 
Registered: Mar 2007
Location: Rio de Janeiro - Brazil
Distribution: Slackware64-current
Posts: 1,003

Rep: Reputation: 84
You run sed under Windows and ask the question in the Slackware forum? Oh well...

What is in the var2.bat file, before and after the sed command that changes it?
 
Old 09-10-2009, 09:38 AM   #6
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 12.2 (2.6.27.59), 13.1 (2.6.33.15)
Posts: 2,576

Rep: Reputation: 275Reputation: 275Reputation: 275
Quote:
You run sed under Windows and ask the question in the Slackware forum?
Sure, why not? The OP might be trying to convert playlists. Also, the GNU utilities provides a sed command that works in Windows. I have used the tools myself because the stock Windows has no such tools.
 
Old 09-10-2009, 09:43 AM   #7
niels.horn
Senior Member
 
Registered: Mar 2007
Location: Rio de Janeiro - Brazil
Distribution: Slackware64-current
Posts: 1,003

Rep: Reputation: 84
Quote:
Originally Posted by Woodsman View Post
Sure, why not? The OP might be trying to convert playlists. Also, the GNU utilities provides a sed command that works in Windows. I have used the tools myself because the stock Windows has no such tools.
I use most of the GNU tools under Windows at the office, as I cannot make my mind use "dir" or "type" or remember the right syntax for all the Windows commands.
Actually, I use cygwin for this...

I just was surprised as I have seen many threads being moved from here to other forums as they had no connection to Slackware.
The OP did not state any reason to have posted here, but I'll try to help anyway if I can.
 
Old 09-10-2009, 10:05 AM   #8
slugzero
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Sorry, I could offer my explanation or... (no0b, plain and simple).

var.bat:

set CD2=D:/shares/Music
set web_path2=[my IP]
 
Old 09-10-2009, 02:46 PM   #9
niels.horn
Senior Member
 
Registered: Mar 2007
Location: Rio de Janeiro - Brazil
Distribution: Slackware64-current
Posts: 1,003

Rep: Reputation: 84
Ok, I tested your scripts after installing the Win32 version of sed on my Windows machine and everything just worked fine.
The difference is that my sed version for Windows is 4.2
Maybe some bugs have been resolved...

You could try separating the passes, like:
Code:
sed -e "s,\\,/,g" tmp > tmp2
sed -e "s,%CD2%,%web_path2%,g" tmp2 >tmp3
Your syntax should work though, it worked fine here, on Windows XP with sed 4.2
 
Old 09-12-2009, 01:57 AM   #10
slugzero
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
niels.horn: You actually did that? Thank you! I took your advice and updated to 4.2, but I am still getting the same result. I modified my test.bat, and am closer. Now I can illustrate my issue much more clearly.

Woodsman is correct. I am trying to build a (web) playlist. I can easily dump filenames to file in this format:

Code:
D:\shares\Music\Mixtape (Various Artists)\01 Chick Habit.mp3
What I need help with is modifying that to this format:

Code:
http://25.25.255.255/01 Chick Habit.mp3
Current test.bat:

Code:
@echo off
REM Set variable "%web_path%"
echo Type your web address below (ie. www.somesite.com or external IP).
set /p web_path=Web address:

REM Dump "%CD%" (Windows environment variable for current direcotry) to temp batch for processing with sed
echo set CD2=%CD% >var.bat

REM Replace "\" with "\\"
sed -e "s,\\,\\\\,g" var.bat >var2.bat

REM Set variable "%CD2%"
call var2.bat

REM Dump filenames w/ path to "tmp"
dir /s/b > tmp

REM (Supposed to) Replace every instance of "%CD%" w/ "http://%web_path%" 
sed -e "s,%CD2%,http://%web_path%,g" tmp >tmp2

REM Finally, replace any "\" with "/"
sed -e "s,\\,/,g" tmp2 >tmp3

ren tmp3 playlist.m3u
The only line that doesn't work is "sed -e "s,%CD2%,http://%web_path%,g" tmp >tmp2", however if I replace "%CD2%" with the path directly in the command (using double backslashes), it works fine!

To clarify, this works:

Code:
sed -e "s,D:\\shares\\Music,http://%web_path%,g" tmp >tmp2
This does not:

Code:
sed -e "s,%CD2%,http://%web_path%,g" tmp >tmp2
What is "%CD2%"?
Code:
C:\>echo %CD2%
D:\\shares\\Music
"%CD2%" and the path typed in the last sed command are identical. Why doesn't this variable work?

Last edited by slugzero; 09-12-2009 at 08:28 PM.
 
Old 09-12-2009, 02:31 AM   #11
mRgOBLIN
Slackware Contributor
 
Registered: Jun 2002
Location: New Zealand
Distribution: Slackware
Posts: 961

Rep: Reputation: 208Reputation: 208Reputation: 208
Haven't really read this properly but just a drive-by suggestion... maybe try %%CD2%% if you want to pass "%CD2%" to sed
 
Old 09-12-2009, 03:45 AM   #12
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 2,768

Rep: Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568
Hi together,

Quote:
Originally Posted by niels.horn View Post
I use most of the GNU tools under Windows at the office, as I cannot make my mind use "dir" or "type" or remember the right syntax for all the Windows commands.
Actually, I use cygwin for this..
I'm an Administrator for Windows-networks and I've vim (the windows-version) installed on most servers in order to edit scripts in a convenient way. Also I've installed vim on an USB-Stick, so I can use it on all client-PCs.
And many of my scripting for the windowsworld I do on Linux-machines.

Markus
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need 'sed command' guru valnar Linux - General 9 02-27-2007 06:35 AM
linux guru required zeeat Linux - Newbie 5 06-16-2006 03:08 PM
Strange Connection refused error. Maybe kernel guru required? shy Programming 3 10-27-2004 11:35 AM
Guru advise required LuggerHouse Linux - Networking 0 05-12-2004 02:24 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 06:46 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration