LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 09-14-2009, 03:45 PM   #1
slugzero
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.

(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?
 
Old 09-14-2009, 08:42 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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
 
Old 09-16-2009, 02:04 AM   #3
d.h
LQ Newbie
 
Registered: Sep 2009
Posts: 3

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

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


Reply

Tags
sed


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


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

Main Menu
Advertisement
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 Google+: linuxquestions
Open Source Consulting | Domain Registration