Solved batch insert rows in text file

June 4, 2013 at 05:02:11
Specs: Windows XP, 1 Gb
I have the following general structure of a text file:

…some initial rows (to be skipped)
Row 1;aaa;bbb;ccc;
Row 2;ddd;ddd;eee
Row 3, fff;ggg;hhh
…And so on

After skipping first 10 rows, I need to insert 5 repeated rows in between two already existing ones so that the final output looks like:

Row 1;aaa;bbb;ccc;
Row 1;aaa;bbb;ccc;
Row 1;aaa;bbb;ccc;
Row 1;aaa;bbb;ccc;
Row 1;aaa;bbb;ccc;
Row 1;aaa;bbb;ccc;
Row 2;ddd;ddd;eee
Row 2;ddd;ddd;eee
Row 2;ddd;ddd;eee
Row 2;ddd;ddd;eee
Row 2;ddd;ddd;eee
Row 2;ddd;ddd;eee
Row 3, fff;ggg;hhh
Row 3, fff;ggg;hhh
Row 3, fff;ggg;hhh
Row 3, fff;ggg;hhh
Row 3, fff;ggg;hhh
Row 3, fff;ggg;hhh


I’m stuck on how to properly conceive the script….

@echo off >newfile.txt
for /f "skip=10 tokens=* delims=;" %%a in (file.txt) do (
set lineNUM=1

echo >>newfile.txt
)


Any help?


See More: batch insert rows in text file

Report •

#1
June 4, 2013 at 07:16:52
✔ Best Answer
:: ===== script starts here ===============
::
:: add5.bat 2013-06-04 20:50:38.95
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

for /f "tokens=* skip=10 delims=;" %%a in (myfile) do (
for /L %%i in (1 1 6) do (
echo.%%a
)
)>> NEWFILE
::====== script ends here =================

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


Report •

#2
June 4, 2013 at 08:44:30
thanks a lot M2,
worked out perfectly!

one more question (if that is possible): what about the case I need to keep the first 10 rows in the destination file?
I've been tryng to adapt this code snippet but for some reasons I could not manage a viable solution (some blank lines in between)
....sorry!

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims=;" %%a in (myfile) do (
set /a N+=1
if !N! gtr 10 goto :job_end
echo %%a >> newfile
)
:job_end
endlocal


Report •

#3
June 4, 2013 at 09:19:09
"(some blank lines in between)"


where?

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


Report •

Related Solutions

#4
June 4, 2013 at 09:27:40
within the first 10 lines of the heading of the file....

here is an example of the file I need to manage (in this case the rows to skip are 15)


8057FUB-SW02 for Windows
Firmware: F094-0WJ51268

Station: 0WJ51268 (F094-0WJ51268 S/N F094-0WJ51268)

Date: 22/04/2013 Time: 00.00

Probe EP-3B-01 (Last Calibrated:16.01.12)

Date;Time;TxOn;RMS W.;RMS Lo.;RMS Hi.;Peak W.;Peak Lo.;Peak Hi.;Battery;Temp;Alarms;Mask;Avg;Rate;
;;;V/m;V/m;V/m;V/m;V/m;V/m;V;C°;AWPLCTV;AWPLCTVMS;Period;S/m;

22/04/2013;00:00:00;;0.48;LOW;LOW;0.51;LOW;LOW;4.39;13;.......;....C...S;Rms 6;20;
22/04/2013;0:06;;0.48;LOW;LOW;0.51;LOW;LOW;4.39;13;.......;....C...S;Rms 6;20;


Report •

#5
June 4, 2013 at 10:11:54
:: ===== script starts here ===============
::
:: add5.bat 2013-06-04 20:50:38.95
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

for /f "tokens=* skip=15 delims=;" %%a in (myfile) do (
for /L %%i in (1 1 6) do (
echo.%%a
)
)>> NEWFILE
::====== script ends here =================

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


Report •

#6
June 4, 2013 at 12:18:46
sorry M2,

but it seems to me pretty much the same solution as that posted on beforehand;

in my second request (#2) I was asking for a new solution in order to keep first 15 rows AND also insert new rows (from that point to the end of the file) so that to store final result in a new destination file called NEWFILE

in short, echo "as is" the first 15 rows and then start to insert new rows...

I hope it's a bit clearer now

thank you


Report •

#7
June 5, 2013 at 03:02:15
I read it wrong.

:: ===== script starts here ===============
:: keeps 15 row header
:: lesson learned: it's not tripped by the glank lines - maybe delims=;"
:: add5.bat 2013-06-04 20:50:38.95
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

set N=
for /f "tokens=1* delims=[]" %%a in ('find /v /n "zzzzz" ^< myfile') do (
set/a N+=1
if !N! gtr 15 (
for /L %%i in (1 1 6) do (
echo.%%b
)
) else (
echo.%%b
)
)>> NEWFILE
::====== script ends here =================

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


Report •

#8
June 5, 2013 at 05:22:33
thanks again M2

it's perfect!

here the key is delim= []
...as usual "tiny things rule the world"

best
max


Report •

Ask Question