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.
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.
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.
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
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:
<
or >
.%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.
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)