Solved Batch to edit lines from text file

August 13, 2016 at 09:22:48
Specs: Windows XP
I need help to edit my text file with Batch. My typical file is below.
--- text start ---
100# Cover
Vertical
Flat Size: x 14+1/4
9+7/8
0 x 0 x 0
CustomerName
BranchName
06/15/2016
15
101/2
73.356
0
83967_616201
4 pager w/ pocket
Job# 83967
--- text end ---
I would like to edit line 3 and 4.
Flat Size: x 14+1/4
9+7/8
to make single line
9+7/8 x 14+1/4 with no Flat Size:
also remove Job# from last line
and update file. I have Windows 7.
Thank you.

message edited by minimoto


See More: Batch to edit lines from text file

Report •


#1
August 13, 2016 at 19:42:34
Might work:
::====== start batch-script
@echo off & setlocal enabledelayedexpansion
:: source filename
set fil=minimoto
:: temp work-filename - make sure it's content-expendable or change the name
set temp=temp1
set out=
set o2=
for /f "tokens=1,2 delims=:" %%a in (%fil%) do (
call :aa "%%a" "%%b"
)
find /v "Job#"<%temp%>%fil%
goto :eof

:aa
if not defined out (
if %2 equ "" >>%temp% echo %~1
set out=%~2
) else (
if not defined o2 (set o2=%~1) else (
>>%temp% echo !o2!!out!
set out=
set o2=
)
)
::------- end batch-script
kind of klunky, but worked in my limited testing.

message edited by nbrane


Report •

#2
August 14, 2016 at 08:17:34
✔ Best Answer
::====== script starts here ===============
::
:: minimoto.bat 2016-08-14 13:31:54.00
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

set N=
set T=

:main
for /f "tokens=* delims= " %%a in (myfile) do (
set/a N+=1
if !N! neq 3 if !N! neq 4 (
set S=%%a
set S=!S:Job# =!
echo.!S!
)
if !N! equ 3 (set T=%%a && set T=!T:Flat Size:=!)
if !N! equ 4 set T=%%a !T! && echo.!T!
) >> NEWFILE
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

#3
August 15, 2016 at 07:56:45
M2 and nbrane,
Thank you for quick response, and I have tried both,M2 batch worked fine and I got result I wanted.
Thanks again for both and you guys are great asset for batch community.

Best,

-- minimoto


Report •
Related Solutions


Ask Question