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) ?
Deleting a data subset from a set
Moderator: DosItHelp
Re: Deleting a data subset from a set
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).
Output:
Hope this helps. (or exchange str1 and str2 with call commands if opposite solution is required)
Saso
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
Re: Deleting a data subset from a set
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
Re: Deleting a data subset from a set
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
Re: Deleting a data subset from a set
Thank you guys! Penpen's approach not using disk seems faster and more elegant. Not sure though, what "~" does in %%~b?