Remove Empty lines and copy selected lines to new text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Remove Empty lines and copy selected lines to new text file

#1 Post by zagix » 23 Jan 2018 08:59

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 23 Jan 2018 11:27

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

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

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

#3 Post by SIMMS7400 » 24 Jan 2018 06:12

WOW - I didn't realize %and% %or% is possible. Awesome!

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

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

#4 Post by zagix » 24 Jan 2018 11:11

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#5 Post by aGerman » 24 Jan 2018 11:27

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

Post Reply