How to insert multiple lines using batch

Microsoft Windows server 2003 enterprise
July 14, 2010 at 16:55:47
Specs: Microsoft Windows Server 2003
Hi! Can anyone help me figure out how to code a batch file that opens a text file and inserts multiple lines after a certain string is found.

For example, current text file contains:

[EXPANDED_CALL_VARS] ; expanded call variable data
; user_ECC_SCALAR1
; user_ECC_SCALAR2
; LastPaymentDate
; LastPaymentAmount
; SSN
; PIN


Now, I want to insert (there are spaces at the beginning of the line) the ffg :

user.LstPrmpt
user.XfrReason
user.ANIMatch
user.EndPtCode

right after '[EXPANDED_CALL_VARS] ; expanded call variable data'

so that it looks like this:

[EXPANDED_CALL_VARS] ; expanded call variable data
user.LstPrmpt
user.XfrReason
user.ANIMatch
user.EndPtCode
; user_ECC_SCALAR1
; user_ECC_SCALAR2
; LastPaymentDate
; LastPaymentAmount
; SSN
; PIN

I am new to batch file coding so any help will really be appreciated. Thanks!!


See More: How to insert multiple lines using batch

Report •


#1
July 14, 2010 at 17:19:12
@echo off & setlocal
for /f "tokens=*" %%a in (file) do (
>>new echo %%a
if "%%a" equ "[EXPANDED_CALL_VARS] ; expanded call variable data" (
>>new echo user.LstPrmpt
>>new echo user.XfrReason
>>new echo user.ANIMatch
>>new echo user.EndPtCode
)
)
:: check results, overwrite main file depending
more new
set /p xx=ok?
if /i xx neq y goto :eof
move /y new file
::------- end
not tested!

Report •

#2
July 14, 2010 at 17:53:28
@nbrane, your batch added the lines but left out the rest.
@mdacuta, make your task easier, use a programming language/tool that is meant for processing files (Perl,gawk,Python/vbscript etc).For example using gawk for windows(see my sig for link), you can use this one liner

C:\test>more file
[EXPANDED_CALL_VARS] ; expanded call variable data
; user_ECC_SCALAR1
; user_ECC_SCALAR2
; LastPaymentDate
; LastPaymentAmount
; SSN
; PIN

C:\test>gawk "/EXPANDED_CALL_VARS/{$0=$0\"\nuser.LstPrmpt\nuser.XfrReason\nuser.ANIMatch\nuser.EndPtCode\"}1" file
[EXPANDED_CALL_VARS] ; expanded call variable data
user.LstPrmpt
user.XfrReason
user.ANIMatch
user.EndPtCode
; user_ECC_SCALAR1
; user_ECC_SCALAR2
; LastPaymentDate
; LastPaymentAmount
; SSN
; PIN

redirect to new file as needed

GNU win32 packages | Gawk


Report •

Related Solutions


Ask Question