LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using Sed under CMD.exe (windows) (https://www.linuxquestions.org/questions/programming-9/using-sed-under-cmd-exe-windows-253363/)

Burgin 11-10-2004 04:29 PM

Using Sed under CMD.exe (windows)
 
I've been looking for a utility to so a simple search and replace string so I downloaded Sed from http://unxutils.sourceforge.net/.

What I want to do is actually a search and replace on Pathname and I want to pass a CMD environment like %HOMEPATH% or %CD%.

But the problem arises becuase Sed sees the single slashes in those environment as delimeters.

Is there anyway to work around this?

I don't have to use Sed, but I want it to be a small self contained executable. I don't want to install a full blown scripting language like Perl or Python just to do this.

Hko 11-10-2004 04:44 PM

You can use (almost) any delimiter in s commands:

This:
Code:

sed -n 's/^searchstring/foundstring/p' file.txt
..does the same thing as:
Code:

sed -n 's,^searchstring,foundstring,p' file.txt
..or:
Code:

sed -n 's|^searchstring|foundstring|p' file.txt
This doesn't work though:
Code:

sed -n '|searchstring|p'
because this is not an s command. Should be possible to work around this, I guess.

Burgin 11-23-2004 12:52 PM

The problem is that even if you use another delimiter, it still won't treat the single '\' as a literal. It still treats this as some kind of operand.

I guess sed doesn't work with variables that contain a path name.. I guess I have to try something else.

Tinkster 11-23-2004 01:05 PM

You could pipe through tr and replace the slashes (which
to sed indeed are escape characters) with "|" for example,
and then do the search and replace, and tr again afterwards.


Cheers,
Tink

Burgin 11-26-2004 02:14 PM

Thanks, you pointed me in the right direction. I had to figure out what 'tr' was. It turned out to be in the list of WIN 32GNU utilities as well. However, I had to use '?' as the delimiter because it was the only character that was neither a) an operand for the 'tr' or 'sed' nor b) a legal character for a windows file/folder name.

To get this to work, I had to do something like

echo set WINPATH_TR=%WINPATH%|tr '\\' '?' > WINPATH_TR.bat
call WINPATH_TR.bat
del WINPATH_TR.bat

This would then set WINPATH_TR to something like c:?windows?system which I could pass down to sed.

(I'm not familiar with the affero)

Tinkster 11-26-2004 02:17 PM

Congratulations :)


Cheers,
Tink


All times are GMT -5. The time now is 11:48 PM.