Deleting a data subset from a set

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Deleting a data subset from a set

#1 Post by sambul35 » 08 Jun 2016 20:44

Suppose I have 2 sets of data - numbers or single words separated by space:

a) 2 4 6 7 8 9 11 13 17 22 45 78
b) 2 7 9 11 15 19

What's the shortest code to remove all matching data in set b) from set a) ?

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Deleting a data subset from a set

#2 Post by miskox » 09 Jun 2016 01:41

I think of doing it this way:

1. read the first file and write each field as a seperate record (or a separate file)
2. read the second file and check if whole record exists in the previous file (or if a file exists - if characters would allow creation of a such file).

Code: Select all

@echo off
set str1=2 4 6 7 8 9 11 13 17 22 45 78&rem
set str2=2 7 9 11 15 19&rem

call :doit1 %str2%
call :doit2 %str1%

del *.tmptmp
goto :EOF

:doit1
if "%2"=="" goto :EOF
break>%1.tmptmp
shift
goto :doit1

:doit2
if "%2"=="" goto :EOF
if not exist %1.tmptmp echo %1
shift
goto :doit2


Output:

Code: Select all

4
6
8
13
17
22
45


Hope this helps. (or exchange str1 and str2 with call commands if opposite solution is required)

Saso

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Deleting a data subset from a set

#3 Post by penpen » 09 Jun 2016 03:17

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "A=2 4 6 7 8 9 11 13 17 22 45 78"
set "B=2 7 9 11 15 19"

set "A\B= %A% "
for %%b in (%B%) do set "A\B=!A\B: %%~b = !"
set "A\B=!A\B:~1,-1!"

echo(A = !A!
echo(B = !B!
echo(A\B = !A\B!

endlocal


penpen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Deleting a data subset from a set

#4 Post by Squashman » 09 Jun 2016 10:57

sambul35 wrote:Thank you guys! Penpen's approach not using disk seems faster and more elegant. Not sure though, what "~" does in %%~b?


Read the help for the FOR command.

Code: Select all

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Deleting a data subset from a set

#5 Post by sambul35 » 09 Jun 2016 11:01

Thank you guys! Penpen's approach not using disk seems faster and more elegant. Not sure though, what "~" does in %%~b?

Post Reply