Page 1 of 1

Remove Empty lines and copy selected lines to new text file

Posted: 23 Jan 2018 08:59
by zagix
Hi,

In my text file first of all i want to delete all the empty rows/lines.
Then where Level = 1 will be taken as first line and below line 1,2,4,6,7,9 to be copied to new text file.

Sample data
some data xxxxxx

some data xxxxxx
some data xxxxxx
some data xxxxxx

Level = 1
some data here.........1
some data here.........2
some data here.........3 (Delete)
some data here.........4

some data here.........5 (Delete)
some data here.........6

some data here.........7

some data here.........8 (Delete)
some data here.........9

some data xxxxxx
some data xxxxxx
some data xxxxxx

Level = 1
some data here.........1
some data here.........2
some data here.........3 (Delete)
some data here.........4

some data here.........5 (Delete)
some data here.........6

some data here.........7

some data here.........8 (Delete)
some data here.........9

some data xxxxxx
some data xxxxxx
some data xxxxxx


Result
Level = 1
some data here.........1
some data here.........2
some data here.........4
some data here.........6
some data here.........7
some data here.........9
Level = 1
some data here.........1
some data here.........2
some data here.........4
some data here.........6
some data here.........7
some data here.........9

Re: Remove Empty lines and copy selected lines to new text file

Posted: 23 Jan 2018 11:27
by aGerman
This works with your test data:

Code: Select all

@echo off &setlocal
set "infile=test.txt"
set "outfile=test2.txt"

for %%i in ("if=(call;)&(if ","or= (call))&&(if ","then= (call))&if errorlevel 1 ") do set %%i
set "n=0"
set "found="
setlocal EnableDelayedExpansion
<"!infile!" >"!outfile!" (
  for /f %%i in ('type "!infile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "line=" &set /p "line="
    if "!line!"=="Level = 1" (set "n=0" &set "found=1" &echo !line!)
    if defined found %if% !n!==1 %or% !n!==2 %or% !n!==4 %or% !n!==7 %or% !n!==9 %or% !n!==12 %then% echo(!line!
    set /a "n+=1"
  )
)
pause
See viewtopic.php?f=3&t=4308

Steffen

Re: Remove Empty lines and copy selected lines to new text file

Posted: 24 Jan 2018 06:12
by SIMMS7400
WOW - I didn't realize %and% %or% is possible. Awesome!

Re: Remove Empty lines and copy selected lines to new text file

Posted: 24 Jan 2018 11:11
by zagix
Hi,

Thanks aGerman for script, a little help is required for data cleaning.

Like on Line 2 remove space before the data (space) xx

Like on Line 4 of text file, on single line Date = xx/xx/xx Value Date = yy/yy/yy is there delete all before (=) and copy only date and value date, move value date to next line.
xx/xx/xx
yy/yy/yy

Like on Line 6 & 7 of text file Amount = xxxx,x copy only after = xxxx.x leaving before (Amount =) and comma to converted in (.)
xxxx.x

Thanks in advance.

Re: Remove Empty lines and copy selected lines to new text file

Posted: 24 Jan 2018 11:27
by aGerman
Please post real data enclosed in code tags (the </> symbol in the editor window) because I'm afraid I don't understand your requirement.

Steffen