Page 1 of 1

Can batch file itself have sed like function?

Posted: 01 Feb 2018 12:44
by goodywp
Hi All,

I had one piece code which used sed in batch file. Now due to sed is not a native utilities in windows. When I implement my code in Windows server 2012 R2, I got this issue:
'sed' is not recognized as an internal or external command.
I know there is two options:
either I install sed utility in server 2012 R2, I need to find it out
or change the following code to pure batch file

Could you please help me on the second option if possible?
Only read first and second lines from an old file which must have more then two lines then output into a new file...

Code: Select all

@ECHO OFF
cd C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp

if exist newdata_1.txt (del newdata_1.txt)

sed -n -e 1p -e 2p echo_data_1.txt > newdata_1.txt

Thanks

Re: Can batch file itself have sed like function?

Posted: 01 Feb 2018 14:33
by aGerman
There is no equivalent command in pure batch. Whether or not you can work around depends on the content of your sed scripts that you passed using -e and the content of the text file you used.
But on the other hand sed works without any installation. All you have to do is saving it in the same folder along with your batch script and the DLLs it depends on. Both the binary and the dependencies can be downloaded as zip files. You'll find them in the bin subfolder.
http://gnuwin32.sourceforge.net/packages/sed.htm

Steffen

Re: Can batch file itself have sed like function?

Posted: 02 Feb 2018 12:52
by goodywp
Thanks Steffen!