Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to get rid of all paths that include the current directory, so I did

my_command | sed "s/%CD%//ig"

in the command prompt, but it didn't work -- everything came out just like before.

How would I make it work?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

The problem with your command is that the backslashes are treated as escape characters, not literal backslashes.

You can overcome this by storing the escaped directory name in an auxiliary variable %dir%:

for /f "tokens=*" %a in ('echo %cd%^| sed "s/\\/\\\\/g"') do set dir=%a

(If used in a batch file, replace every %a by %%a.)

You can use sed as usual now:

my_command | sed "s/%dir%//ig"
share|improve this answer
    
D'oh. Use sed to escape its own arguments. Why didn't I think of that! :-) –  Mehrdad Jun 29 '12 at 20:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.