Computer Hope
Software => Computer programming => Topic started by: Z.K. on March 26, 2009, 02:54:54 AM
-
I need to write a batch file that reads a line of text from a text file, deletes the first two characters if they are "\\" and skip the last line.
Now, I could do this in C or perl, but I am not familiar enough with batch file commands only ever having to do very simple commands in a batch file. I suspect I need to use for /F and FindStr commands, but I am unsure how to proceed. Anyone have any suggestions or a good web page or book I can refer to.
:|| ???
-
Now, I could do this in C or perl,
if you can do this in Perl, then use Perl. you can download Perl for windows , right? Using Perl is way much better than batch
-
I need to write a batch file that reads a line of text from a text file, deletes the first two characters if they are "\\" and skip the last line.
Now, I could do this in C or perl, but I am not familiar enough with batch file commands only ever having to do very simple commands in a batch file. I suspect I need to use for /F and FindStr commands, but I am unsure how to proceed. Anyone have any suggestions or a good web page or book I can refer to.
:|| ???
Using Perl is way much better than batch
It is if you know Perl/Awk/Python/Haskell whatever better than you know batch, and if you are allowed to install 3rd party software.
I prefer to answer the original question rather than preach about my preferred hobby language.
@echo off
REM reads a line of text from a text file,
REM deletes the first two characters if they are "\\" and skip the last line.
REM Cut here---------------------------------------------------------------
REM for demo purpose
REM create test file
echo \\Line 1 of 5. >test.txt
echo \\Line 2 of 5. >>test.txt
echo ##Line 3 of 5. >> test.txt
echo \\Line 4 of 5. >>test.txt
echo \\Line 5 of 5. >>test.txt
REM Cut here---------------------------------------------------------------
setlocal enabledelayedexpansion
set stripchars=\\
set filename=test.txt
REM Count lines in file
REM Open file for reading, increment a counter
REM once for each line
REM for demo purpose
echo Counting lines.
set /a lines=0
for /f "delims==" %%L in ( ' type "%filename%" ' ) do (
set /a lines+=1
)
REM for demo purpose
echo File has %lines% lines
echo.
REM store number of last line in numeric variable
set /a lastline=%lines%
REM Process file
set /a line=1
for /f "delims==" %%L in ( ' type "%filename%" ' ) do (
set thisline=%%L
if !line! LSS %lastline% (
set startchars=!thisline:~0,2!
if "!startchars!"=="%stripchars%" (
set outputline=!thisline:~2!
) else (
set outputline=!thisline!
)
echo !outputline!
)
set /a line+=1
)
Counting lines.
File has 5 lines
Line 1 of 5.
Line 2 of 5.
##Line 3 of 5.
Line 4 of 5.
-
It is if you know Perl better than you know batch, and if you are allowed to install 3rd party software.
that's why i asked, if OP can use Perl or not because he did not mention of any constraints. Read his sentence and you will find he knows how to do it in Perl. Your batch file can be easily done with just one line of Perl code.
perl -ne 's/^\\\\//;print unless eof;' file
-
that's why i asked, if OP can use Perl or not because he did not mention of any constraints. Read his sentence and you will find he knows how to do it in Perl. Your batch file can be easily done with just one line of Perl code.
perl -ne 's/^\\\\//;print unless eof;' file
I have to admit you are right about Perl being more compact and concise. Maybe the OP is used to a *nix environment and did not realise that there are Windows ports of Perl available.