LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed guru required for help with Win32 environment variables (https://www.linuxquestions.org/questions/programming-9/sed-guru-required-for-help-with-win32-environment-variables-755149/)

slugzero 09-14-2009 02:45 PM

sed guru required for help with Win32 environment variables
 
Hi, I posed this question last week, but in the wrong catagory - which is probably why it went unsolved/stale.

(http://www.linuxquestions.org/questi...iables-753788/)

In short, my question pertains to using sed w/ Windows environment variables (ie. %CD%) on a Windows machine.

Sed is not parsing the backslashes correctly in my environment variable.

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? :(

ghostdog74 09-14-2009 07:42 PM

i am not going to ask you to stop using sed, but since I can see distinct delimiter characters i can use, you can switch to use gawk.
Code:

C:\mp3\> echo D:\shares\Music\Mixtape (Various Artists)\01 Chick Habit.mp3 | gawk -F"\\" "{print \"http://1.1.1.1/\"$NF}"
http://1.1.1.1/01 Chick Habit.mp3


d.h 09-16-2009 01:04 AM

Quote:

Originally Posted by slugzero
Why doesn't this variable work?

You've already answered this question yourself:
Quote:

Originally Posted by slugzero
Sed is not parsing the backslashes correctly in my environment variable.

Sed uses backslashes to escape characters, therefore you need to escape them :)

Try:
Code:

sed -e "s,\,\\\\\\,g" <<< "%CD2"
so that you get something like this:
Code:

D:\\\\\shares\\\\\Music
which you then can use in your expression. You have to play with the number of backslashes until you get the wanted result. This is quite tricky and you might need a ridiculous amount of backslashes to solve this...)


All times are GMT -5. The time now is 09:06 AM.