views:

133

answers:

4
sed 's/^#//g' < kam_account_calls.txt > kam_account_calls1.txt

This command removes # from the start of the line.

Please let me know how to adopt the same functionality in the Windows command shell.

A: 

There is no feature in DOS to do so out of the box, but you can always use one of the many ports of sed for windows, like this one.

Edit answer to comment: As DOS batchfiles only execute DOS commands (either internal commands of CMD or programs), you cannot without adding a tool like sed.

Edit "Can't we use CMD": No, not under DOS - CMD does not have a feature that reads through files and manipulates them. You either have to use an existing program like sed or awk or write one yourself. Sorry. Even Unix/Linux does not have a built in feature, but they package sed and awk as programs out of the box. This just is not the case for DOS.

Obviously, Windows added expansions to do things like this using environment variables (hats off to Johannes Rössel). My DOS know how obviously has grown old with me.

Ralph Rickenbach
can we make some batch file which can do same functionality
Irveen
i dont wanna use sed........can we use cmd to solve our purpose....and do the same thing wat sed is doing
Irveen
Irveen: In the general case: no. But what `sed` does here is sufficiently simple that you *can* in fact do this with `cmd`. See my answer.
Joey
A: 

you can use vbscript

Set objFS=CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
    strLine=objFile.ReadLine
    If Mid(strLine,1,1) = "#" Then
     strLine = Mid(strLine,2)
    End If 
    WScript.Echo strLine
Loop

output

C:\test>more file
# this is a comment
this is one line #
blahb labh

C:\test>cscript /nologo test.vbs
 this is a comment
this is one line #
blahb labh
ghostdog74
+2  A: 

Contrary to some voices in this thread, this can be done with cmd, rather straightforward even:

@echo off
rem enable some niceties we need here
setlocal enableextensions enabledelayedexpansion

rem loop through the file
for /f "delims=" %%x in (kam_account_calls.txt) do call :process %%x

endlocal
goto :eof

rem subroutine to remove # if present:
:process
set line=%*
rem if the first character of the line is #
if [%line:~0,1%]==[#] (
    rem then throw it away
    echo.!line:~1!
) else (
    rem else just output the line
    echo.!line!
)
goto :eof

This will loop through the file and output all lines with a leading # character removed.

It can't be done in plain DOS, that is correct (although I'm not too sure of that, some people have done amazing things there). But nearly no-one who includes DOS in his question actually means DOS but rather the Windows Command Processor cmd.exe.

Limitations:

  • Line length limited to something around 8190 characters.
  • Will trip over special shell characters such as < or >.
  • Environment variables (or pseudo-variables, such as %random% or %cd% will be expanded.

Just bear those in mind and look whether that will be an issue with your text file.

Code and sample file can be found here.

Joey
Does this work for all line lengths? What happens if the environment is full? But congrats, quite nice...
Ralph Rickenbach
Yes, it has a line length limit, both due to `echo` used (around 8190 characters) and using an environment variable (around 32760 characters). Both should suffice for fairly normal text files. I've added the limitations to the answer, though. I'm not exactly sure, however, what you mean with "the environment is full". The environment is stored as a sequence of key=value pairs, separated by U+0000. If you don't have enough memory to hold *one line* of input then I think you have other things to worry about already.
Joey
A: 

Install Cygwin, then you'll be able to use sed directly.

(Not the Windows command shell, I know, more like a command shell on steroids)

pavium