Here is an improved and simplified version of kiswa's answer.
@echo off
(
for /f "usebackq" %%A in ("keywords.txt") do findstr /bl "%%A" list.txt
)>sorted.txt
REM move /y sorted.txt list.txt
The FINDSTR command only matches lines that begin with the keyword, and it forces the search to be a literal search. (FINDSTR could give the wrong result if the /L
option is not specified and the keyword happens to contain a regex meta-character.)
The code to replace the original file with the sorted file is commented out. Simply remove the REM statement to activate the MOVE statement.
As with kiswa's answer, the above will only output lines from list.txt that match a keyword in keywords.txt.
You might have lines in list.txt that do not match a keyword. If you want to preserve those lines at the bottom of the sorted output, then use:
@echo off
(
for /f "usebackq" %%A in ("keywords.txt") do findstr /bli "%%A" "list.txt"
findstr /vblig:"keywords.txt" "list.txt"
)>sorted.txt
::move /y sorted.txt list.txt
Note that the /I
(case insensitive) option must be used because of a FINDSTR bug dealing with multiple literal search strings of different lengths. The /I
option avoids the bug, but it would cause problems if your keywords are case sensitive. See What are the undocumented features and limitations of the Windows FINDSTR command?.
You might have keywords that are missing from list.txt. If you want to include those keywords without any data following them, then use:
@echo off
(
for /f "usebackq" %%A in ("keywords.txt") do findstr /bl "%%A" "list.txt" || echo %%A
)>sorted.txt
::move /y sorted.txt list.txt
Obviously you can combine both techniques to make sure you preserve the union of both files:
@echo off
(
for /f "usebackq" %%A in ("keywords.txt") do findstr /bli "%%A" "list.txt" || echo %%A
findstr /vblig:"keywords.txt" "list.txt"
)>sorted.txt
::move /y sorted.txt list.txt
All of the above assume the keywords do not contain space or tab characters. If they do, then the FOR /F options and FINDSTR options must change:
@echo off
(
for /f "usebackq delims=" %%A in ("keywords.txt") do findstr /bic:"%%A" "list.txt" || echo %%A
findstr /vblig:"keywords.txt" "list.txt"
)>sorted.txt
::move /y sorted.txt list.txt