Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-14-2009, 03:45 PM   #1
LQ Newbie
Registered: Sep 2009
Posts: 5

Rep: Reputation: 0
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.


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:

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

Code: Chick Habit.mp3
Current test.bat:

@echo off
REM Set variable "%web_path%"
echo Type your web address below (ie. 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:

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

sed -e "s,%CD2%,http://%web_path%,g" tmp >tmp2
What is "%CD2%"?

C:\>echo %CD2%
"%CD2%" and the path typed in the last sed command are identical. Why doesn't this variable work?
Old 09-14-2009, 08:42 PM   #2
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
C:\mp3\> echo D:\shares\Music\Mixtape (Various Artists)\01 Chick Habit.mp3 | gawk -F"\\" "{print \"\"$NF}" Chick Habit.mp3
Old 09-16-2009, 02:04 AM   #3
LQ Newbie
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
Originally Posted by slugzero
Why doesn't this variable work?
You've already answered this question yourself:
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

sed -e "s,\,\\\\\\,g" <<< "%CD2"
so that you get something like this:
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...)

Last edited by d.h; 09-16-2009 at 04:00 AM. Reason: Correction



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

Similar Threads
Thread Thread Starter Forum Replies Last Post
sed guru required for help with Win32 environment variables slugzero Slackware 11 09-12-2009 04:45 AM
Need 'sed command' guru valnar Linux - General 9 02-27-2007 07:35 AM
linux guru required zeeat Linux - Newbie 5 06-16-2006 04:08 PM
Guru advise required LuggerHouse Linux - Networking 0 05-12-2004 03:24 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration