Solved How to find a specific line&add a prefix ($) to it in batch

January 10, 2014 at 09:27:24
Specs: windows7
I have a input file like below (dots represents blank space)
..1001232..21354
..2004651..123546
..3002121..254665
..4003213..987456
..3012121..254665
..4033213..987456
..and so on
I need to drag drop this file to a bat file which can give me an out put file which will add a "$" sign in the begining of specific 3 digits like in this case 300

..1001232..21354
..2004651..123546
$..3002121..254665
..4003213..987456
$..3005121..254665
..4033213..987456


See More: How to find a specific line&add a prefix ($) to it in batch

Report •

✔ Best Answer
January 11, 2014 at 23:39:05
I could never make semse of findstr.

::====== script starts here ===============
:: prepend $ to lines [space(s)]300
:: rohan300.bat 2014-01-11 15:59:43.30
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims=" %%a in (myfile) do (
call :sub1 %%a
if !T!==300 (echo.$%%a) else (echo.%%a)
) >> NEWFILE
goto :eof

:sub1
for /f "tokens=* delims= " %%i in ('echo.%*') do (
set S=%%i
set T=!S:~0,3!
)
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle



#1
January 10, 2014 at 14:31:18
Consider the following PowerShell:
Get-Content .\in.txt | 
ForEach-Object {
  $_ -replace "^  300", "$  300"
} > .\out.txt 

How To Ask Questions The Smart Way


Report •

#2
January 11, 2014 at 01:16:58
I hit a bump when I read:
a input file [sic]
but decided to go ahead.

When I saw this:
(dots represents blank space)
I got queasy. But decided to write a script that might work despite the ambiguity. I got it mostly done but I hit the wall when 3012 somehow morphed into 3005.

=====================
M2 Golden-Triangle


Report •

#3
January 11, 2014 at 20:28:26
Ha ha M2! Sounds like one of those carnival rides, or bar fights, we all remember so well!
Here's a stab using FINDSTR which never, ever, ever works the way i think it should:
::=== begin batchscript
@echo off & setlocal
(for /f "tokens=*" %%a in (test_in) do (
echo %%a|findstr /b /c:" 300">nul&&echo $ %%a||echo %%a
)
)>test_out
::===== end
I assumed 2 spaces, and also assumed "replace" vs "insert" for the dollar-sign.

message edited by nbrane


Report •

Related Solutions

#4
January 11, 2014 at 23:39:05
✔ Best Answer
I could never make semse of findstr.

::====== script starts here ===============
:: prepend $ to lines [space(s)]300
:: rohan300.bat 2014-01-11 15:59:43.30
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims=" %%a in (myfile) do (
call :sub1 %%a
if !T!==300 (echo.$%%a) else (echo.%%a)
) >> NEWFILE
goto :eof

:sub1
for /f "tokens=* delims= " %%i in ('echo.%*') do (
set S=%%i
set T=!S:~0,3!
)
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

Ask Question