|
Answer» I was wondering if anyone had created an insert script that could do the following:
1) Open a file 2) Find a particular line of text in a file. 3) insert either before or after a line (or more than one line from another file). 4) save the file and exit
Any help would be appreciated. Up until now I have been able to work around this problem, but I think it is going to be a big requirement very soon.
Thanks, Ken Hi Ken,
I was looking for an answer to this very same question myself. I was able to find a solution to my problem by editting the append function. Specifically, I needed to insert (or append) a string to the beginning of each line in a .csv file to create a NEW field. Here is what I have:
::append str file line -- appends a string to a SPECIFIC line in a text file :: -- str [in] - string to be appended :: -- file [in] - file name to append the string to :: -- line [in] - line number to append the string to, first line is 1, omit for last line ::$source http://www.dostips.com rem SETLOCAL ENABLEEXTENSIONS rem SETLOCAL ENABLEDELAYEDEXPANSION SETLOCAL set ap=%~1 set f=%~2 set c=%~3 if not defined c ( set c=0 for /f %%a in ('find /v /c ""^<"%f%"') do set c=%%a ) (for /f "tokens=1,* delims=:" %%a in ('findstr /n /v "$$"^<"%f%"') do ( if not "%%a"=="%c%" (echo.%ap%%%b >> "file2.csv") ELSE (echo.%%b >> "file3.csv") ))>"%temp%.\t0815.txt" EXIT /b %ERRORLEVEL%
I call this bat file with the following: append.bat "14," file1.csv 0
Hope it helps. Jacquieif you can afford to install GNU awk (or sed) , all that batch code can be simplified to : Code: [Select]#insert before a line awk '/<My Tag>/ { print "new text" } {print}' file # insert after awk '/<My Tag>/ { print $0; print "new text";NEXT } {print} ' file
|