I have a list of keywords in a keywords.txt file. I have another file list.txt with the keywords in the beginning of each line. How can I sort the lines in list.txt to the same order they appear in keywords.txt?
keywords.txt
house
car
tree
woods
mailbox
list.txt
car bbdfbdfbdfbdf
tree gdfgvsgsgs
mailbox gsgsdfsdf
woods gsgsdgsdgsdgsdgsddsd
house gsdgfsdgsdgsdgsdg
final result in list.txt
house gsdgfsdgsdgsdgsdg
car bbdfbdfbdfbdf
tree gdfgvsgsgs
woods gsgsdgsdgsdgsdgsddsd
mailbox gsgsdfsdf
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
$ join -1 2 -2 1 <(cat -n keywords.txt | sort -k2) <(sort list.txt) | sort -k2n | cut -d ' ' -f 1,3-
house gsdgfsdgsdgsdgsdg
car bbdfbdfbdfbdf
tree gdfgvsgsgs
woods gsgsdgsdgsdgsdgsddsd
mailbox gsgsdfsdf
cut --help' for more information.
root@ubuntu:~/Desktop/sort# bash script.bsh
cut: script.bsh: line 1: $: command not found
invalid byte or field list
Try
cut --help' for more information - Blainer 2012-04-04 17:24
Here's a Windows batch file. It's probably not the most efficient, but I think it's nicely readable.
@echo off
for /F "tokens=*" %%A in (keywords.txt) do (
for /F "tokens=*" %%B in ('findstr /i /C:"%%A" list.txt') do (
echo %%B >> sorted.txt
)
)
del list.txt
rename sorted.txt list.txt
This creates a sorted file, then removes the list file and renames the sorted file.
findstr
command in Windows will not locate these lines in your list.txt
file: ZZZYYYJesus-Christ.html
ZZZYYYDeity-Christ.html
ZZZYYYwhy-believe-resurrection.html
ZZZYYYJesus-crucified.html
ZZZYYYJesus-Jew.html
ZZZYYYJesus-myth.html
ZZZYYYnames-Jesus-Christ.html
ZZZYYYstations-cross.html
kiswa 2012-04-04 18:38