0

I have a text file with book name and chapter in the bible. I would like to sort the lines so that

  1. lines are first sorted according to the order between their books, the lexical order between the book names, or ideally, the order of the books in the bible:

    Genesis
    Exodus
    Leviticus
    Numbers
    Deuteronomy
    Joshua
    Judges
    Ruth
    1st Samuel
    2nd Samuel
    1st Kings
    2nd Kings
    1st Chronicles
    2nd Chronicles
    Ezra
    Nehemiah
    Esther
    Job
    Psalms
    Proverbs
    Ecclesiastes
    Song of Solomon
    Isaiah
    Jeremiah
    Lamentations
    Ezekiel
    Daniel
    Hosea
    Joel
    Amos
    Obadiah
    Jonah
    Micah
    Nahum
    Habakkuk
    Zephaniah
    Haggai
    Zechariah
    Malachi
    Matthew
    Mark
    Luke
    John
    Acts
    Romans
    1st Corinthians
    2nd Corinthians
    Galatians
    Ephesians
    Philippians
    Colossians
    1st Thessalonians
    2nd Thessalonians
    1st Timothy
    2nd Timothy
    Titus
    Philemon
    Hebrews
    James
    1st Peter
    2nd Peter
    1st John
    2nd John
    3rd John
    Jude
    Revelation
    
  2. the lines for the same book are grouped together, and sorted numerically according to their verse numbers.

    Understanding Bible Verse Number References says:

    The basic pattern is the name of the Book, the chapter number followed by a colon, and the verse number. For example “Genesis 1:3” refers to the book of Genesis, the first chapter, and the third verse.

I have tried sort with and without -n, and both can't achieve the goals.

    Genesis 1:1–2:25 
    Matthew 1:1–2:12 
    Psalm 1:1-6 
    Proverbs 1:1-6 
    Genesis 3:1–4:26 
    Matthew 2:13–3:6 
    Psalm 2:1-12 
    Proverbs 1:7-9 
    Genesis 5:1–7:24 
    Matthew 3:7–4:11 
    Psalm 3:1-8 
    Proverbs 1:10-19 
    Genesis 8:1–10:32 
    Matthew 4:12-25 
    Psalm 4:1-8 
    Proverbs 1:20-23 
    Genesis 11:1–13:4 
    Matthew 5:1-26 
    Psalm 5:1-12 
    Proverbs 1:24-28 
    Genesis 13:5–15:21 
    Matthew 5:27-48 
    Psalm 6:1-10 
    Proverbs 1:29-33 
    Genesis 16:1–18:15 
    Matthew 6:1-24 
    Psalm 7:1-17 
    Proverbs 2:1-5 
    Genesis 18:16–19:38 
    Matthew 6:25–7:14 
    Psalm 8:1-9 
    Proverbs 2:6-15 
    Genesis 20:1–22:24 
    Matthew 7:15-29 
    Psalm 9:1-12 
    Proverbs 2:16-22 
    Genesis 23:1–24:51 
    Matthew 8:1-17 
    Psalm 9:13-20 
    Proverbs 3:1-6 
    Genesis 24:52–26:16 
    Matthew 8:18-34 
    Psalm 10:1-15 
    Proverbs 3:7-8 
    Genesis 26:17–27:46 
    Matthew 9:1-17 
    Psalm 10:16-18 
    Proverbs 3:9-10 
    Genesis 28:1–29:35 
    Matthew 9:18-38 
    Psalm 11:1-7 
    Proverbs 3:11-12 
    Genesis 30:1–31:16 
    Matthew 10:1-23 
    Psalm 12:1-8 
    Proverbs 3:13-15 
    Genesis 31:17–32:12 
    Matthew 10:24–11:6 
    Psalm 13:1-6 
    Proverbs 3:16-18 
    Genesis 32:13–34:31 
    Matthew 11:7-30 
    Psalm 14:1-7 
    Proverbs 3:19-20 
    Genesis 35:1–36:43 
    Matthew 12:1-21 
    Psalm 15:1-5 
    Proverbs 3:21-26 
    Genesis 37:1–38:30 
    Matthew 12:22-45 
    Psalm 16:1-11 
    Proverbs 3:27-32 
    Genesis 39:1–41:16 
    Matthew 12:46–13:23 
    Psalm 17:1-15 
    Proverbs 3:33-35 
    Genesis 41:17–42:17 
    Matthew 13:24-46 
    Psalm 18:1-15 
    Proverbs 4:1-6 
    Genesis 42:18–43:34 
    Matthew 13:47–14:12 
    Psalm 18:16-36 
    Proverbs 4:7-10 
    Genesis 44:1–45:28 
    Matthew 14:13-36 
    Psalm 18:37-50 
    Proverbs 4:11-13 
    Genesis 46:1–47:31 
    Matthew 15:1-28 
    Psalm 19:1-14 
    Proverbs 4:14-19 
    Genesis 48:1–49:33 
    Matthew 15:29–16:12 
    Psalm 20:1-9 
    Proverbs 4:20-27 
    Matthew 16:13–17:9 
    Psalm 21:1-13 
    Proverbs 5:1-6 
    Exodus 2:11–3:22 
    Matthew 17:10-27 
    Psalm 22:1-18 
    Proverbs 5:7-14 
    Exodus 4:1–5:21 
    Matthew 18:1-20 
    Psalm 22:19-31 
    Proverbs 5:15-21 
    Exodus 5:22–7:25 
    Matthew 18:21–19:12 
    Psalm 23:1-6 
    Proverbs 5:22-23 
    Exodus 8:1–9:35 
    Matthew 19:13-30 
    Psalm 24:1-10 
    Proverbs 6:1-5 
    Exodus 10:1–12:13 
    Matthew 20:1-28 
    Proverbs 6:6-11 
    Exodus 12:14–13:16 
    Matthew 20:29–21:22 
    Psalm 25:16-22 
    Proverbs 6:12-15 
    Exodus 13:17–15:18 
    Matthew 21:23-46 
    Psalm 26:1-12 
    Proverbs 6:16-19 
    Exodus 15:19–17:7 
    Matthew 22:1-33 
    Psalm 27:1-6 
    Proverbs 6:20-26 
    Exodus 17:8–19:15 
    Matthew 22:34–23:12 
    Psalm 27:7-14 
    Proverbs 6:27-35 
    Exodus 19:16–21:21 
    Matthew 23:13-39 
    Psalm 28:1-9 
    Proverbs 7:1-5 
    Exodus 21:22–23:13 
    Matthew 24:1-28 
    Psalm 29:1-11 
    Proverbs 7:6-23 
    Exodus 23:14–25:40 
    Matthew 24:29-51 
    Psalm 30:1-12 
    Proverbs 7:24-27 
    Exodus 26:1–27:21 
    Matthew 25:1-30 
    Psalm 31:1-8 
    Proverbs 8:1-11 
    Exodus 28:1-43 
    Matthew 25:31–26:13 
    Psalm 31:9-18 
    Proverbs 8:12-13 
    Exodus 29:1–30:10 
    Matthew 26:14-46 
    Psalm 31:19-24 
    Proverbs 8:14-26 
    Exodus 30:11–31:18 
    Matthew 26:47-68 
    Psalm 32:1-11 
    Proverbs 8:27-32 
    Exodus 32:1–33:23 
    Matthew 26:69–27:14 
    Psalm 33:1-11 
    Proverbs 8:33-36 
    Exodus 34:1–35:9 
    Matthew 27:15-31 
    Psalm 33:12-22 
    Proverbs 9:1-6 
    Exodus 35:10–36:38 
    Matthew 27:32-66 
    Proverbs 9:7-8 
    Exodus 37:1–38:31 
    Matthew 28:1-20 
    Psalm 34:11-22 
    Proverbs 9:9-10 
    Exodus 39:1–40:38 
    Mark 1:1-28 
    Psalm 35:1-16 
    Proverbs 9:11-12 
    Leviticus 1:1–3:17 
    Mark 1:29–2:12 
    Psalm 35:17-28 
    Proverbs 9:13-18 
    Leviticus 4:1–5:19 
    Mark 2:13–3:6 
    Psalm 36:1-12 
    Proverbs 10:1-2 
    Leviticus 6:1–7:27 
    Mark 3:7-30 
    Proverbs 10:3-4 
    Leviticus 7:28–9:6 
    Mark 3:31–4:25 
    Psalm 37:12-29 
    Leviticus 9:7–10:20 
    Mark 4:26–5:20 
    Psalm 37:30-40 
    Proverbs 10:6-7 
    Leviticus 11:1–12:8 
    Mark 5:21-43 
    Psalm 38:1-22 
    Proverbs 10:8-9 
    Leviticus 13:1-59 
    Mark 6:1-29 
    Psalm 39:1-13 
    Leviticus 14:1-57 
    Mark 6:30-56 
    Psalm 40:1-10 
    Proverbs 10:11-12 
    Leviticus 15:1–16:28 
    Mark 7:1-23 
    Psalm 40:11-17 
    Proverbs 10:13-14 
    Leviticus 16:29–18:30 
    Mark 7:24–8:10 
    Psalm 41:1-13 
    Proverbs 10:15-16 
    Leviticus 19:1–20:21 
    Mark 8:11-38 
    Psalm 42:1-11 
    Leviticus 20:22–22:16 
    Mark 9:1-29 
    Psalm 43:1-5 
    Leviticus 22:17–23:44 
    Mark 9:30–10:12 
    Psalm 44:1-8 
    Leviticus 24:1–25:46 
    Mark 10:13-31 
    Psalm 44:9-26 
    Proverbs 10:20-21 
    Leviticus 25:47–27:13 
    Mark 10:32-52 
    Psalm 45:1-17 
    Mark 11:1-25 
    Psalm 46:1-11 
    Numbers 2:1–3:51 
    Mark 11:27–12:17 
    Psalm 47:1-9 
    Proverbs 10:24-25 
    Numbers 4:1–5:31 
    Mark 12:18-37 
    Psalm 48:1-14 
    Numbers 6:1–7:89 
    Mark 12:38–13:13 
    Psalm 49:1-20 
    Proverbs 10:27-28 
    Numbers 8:1–9:23 
    Mark 13:14-37 
    Psalm 50:1-23 
    Proverbs 10:29-30 
    Numbers 10:1–11:23 
    Mark 14:1-21 
    Psalm 51:1-19 
    Proverbs 10:31-32 
    Numbers 11:24–13:33 
    Mark 14:22-52 
    Psalm 52:1-9 
    Proverbs 11:1-3 
    Numbers 14:1–15:16 
    Mark 14:53-72 
    Psalm 53:1-6 
    Numbers 15:17–16:40 
    Mark 15:1-47 
    Psalm 54:1-7 
    Proverbs 11:5-6 
    Numbers 16:41–18:32 
    Mark 16:1-20 
    Psalm 55:1-23 
    Numbers 19:1–20:29 
    Luke 1:1-25 
    Psalm 56:1-13 
    Numbers 21:1–22:20 
    Luke 1:26-56 
    Psalm 57:1-11 
    Proverbs 11:9-11 
    Numbers 22:21–23:30 
    Luke 1:57-80 
    Psalm 58:1-11 
    Proverbs 11:12-13 
    Numbers 24:1–25:18 
    Luke 2:1-35 
    Psalm 59:1-17 
    Numbers 26:1-51 
    Luke 2:36-52 
    Psalm 60:1-12 
    Numbers 26:52–28:15 
    Luke 3:1-22 
    Psalm 61:1-8 
    Proverbs 11:16-17 
    Numbers 28:16–29:40 
    Luke 3:23-38 
    Psalm 62:1-12 
    Proverbs 11:18-19 
    Numbers 30:1–31:54 
    Luke 4:1-30 
    Psalm 63:1-11 
    Proverbs 11:20-21 
    Numbers 32:1–33:39 
    Luke 4:31–5:11 
    Psalm 64:1-10 
    Numbers 33:40–35:34 
    Luke 5:12-28 
    Psalm 65:1-13 
    Luke 5:29–6:11 
    Psalm 66:1-20 
    Proverbs 11:24-26 
    Deuteronomy 2:1–3:29 
    Luke 6:12-38 
    Psalm 67:1-7 
    Deuteronomy 4:1-49 
    Luke 6:39–7:10 
    Psalm 68:1-18 
    Deuteronomy 5:1–6:25 
    Luke 7:11-35 
    Psalm 68:19-35 
    Proverbs 11:29-31 
    Deuteronomy 7:1–8:20 
    Luke 7:36–8:3 
    Psalm 69:1-18 
    Deuteronomy 9:1–10:22 
    Luke 8:4-21 
    Psalm 69:19-36 
    Proverbs 12:2-3 
    Deuteronomy 11:1–12:32 
    Luke 8:22-39 
    Psalm 70:1-5 
    Deuteronomy 13:1–15:23 
    Luke 8:40–9:6 
    Psalm 71:1-24 
    Proverbs 12:5-7 
    Deuteronomy 16:1–17:20 
    Luke 9:7-27 
    Psalm 72:1-20 
    Proverbs 12:8-9 
    Deuteronomy 18:1–20:20 
    Luke 9:28-50 
    Psalm 73:1-28 
    Deuteronomy 21:1–22:30 
    Luke 9:51–10:12 
    Psalm 74:1-23 
    Deuteronomy 23:1–25:19 
    Luke 10:13-37 
    Psalm 75:1-10 
    Proverbs 12:12-14 
    Deuteronomy 26:1–27:26 
    Luke 10:38–11:13 
    Psalm 76:1-12 
    Proverbs 12:15-17 
    Deuteronomy 28:1-68 
    Luke 11:14-36 
    Psalm 77:1-20 
    Deuteronomy 29:1–30:20 
    Luke 11:37–12:7 
    Psalm 78:1-31 
    Proverbs 12:19-20 
    Deuteronomy 31:1–32:27 
    Luke 12:8-34 
    Psalm 78:32-55 
    Proverbs 12:21-23 
    Deuteronomy 32:28-52 
    Luke 12:35-59 
    Psalm 78:56-64 
    Deuteronomy 33:1-29 
    Luke 13:1-21 
    Psalm 78:65-72 
    Luke 13:22–14:6 
    Psalm 79:1-13 
    Joshua 3:1–4:24 
    Luke 14:7-35 
    Psalm 80:1-19 
    Proverbs 12:27-28 
    Joshua 5:1–7:15 
    Luke 15:1-32 
    Psalm 81:1-16 
    Joshua 7:16–9:2 
    Luke 16:1-18 
    Psalm 82:1-8 
    Proverbs 13:2-3 
    Joshua 9:3–10:43 
    Luke 16:19–17:10 
    Psalm 83:1-18 
    Joshua 11:1–12:24 
    Luke 17:11-37 
    Psalm 84:1-12 
    Proverbs 13:5-6 
    Joshua 13:1–14:15 
    Luke 18:1-17 
    Psalm 85:1-13 
    Proverbs 13:7-8 
    Joshua 15:1-63 
    Luke 18:18-43 
    Psalm 86:1-17 
    Proverbs 13:9-10 
    Joshua 16:1–18:28 
    Luke 19:1-27 
    Psalm 87:1-7 
    Joshua 19:1–20:9 
    Luke 19:28-48 
    Psalm 88:1-18 
    Proverbs 13:12-14 
    Joshua 21:1–22:20 
    Luke 20:1-26 
    Psalm 89:1-13 
    Proverbs 13:15-16 
    Joshua 22:21–23:16 
    Luke 20:27-47 
    Psalm 89:14-37 
    Proverbs 13:17-19 
    Joshua 24:1-33 
    Luke 21:1-28 
    Psalm 89:38-52 
    Proverbs 13:20-23 
    Judges 1:1–2:9 
    Luke 21:29–22:13 
    Psalms 90:1–91:16 
    Proverbs 13:24-25 
    Judges 2:10–3:31 
    Luke 22:14-34 
    Psalms 92:1–93:5 
    Proverbs 14:1-2 
    Judges 4:1–5:31 
    Luke 22:35-53 
    Psalm 94:1-23 
    Proverbs 14:3-4 
    Judges 6:1-40 
    Luke 22:54–23:12 
    Psalms 95:1–96:13 
    Proverbs 14:5-6 
    Judges 7:1–8:17 
    Luke 23:13-43 
    Psalms 97:1–98:9 
    Proverbs 14:7-8 
    Judges 8:18–9:21 
    Luke 23:44–24:12 
    Psalm 99:1-9 
    Proverbs 14:9-10 
    Judges 9:22–10:18 
    Luke 24:13-53 
    Psalm 100:1-5 
    Proverbs 14:11-12 
    Judges 11:1–12:15 
    John 1:1-28 
    Psalm 101:1-8 
    Proverbs 14:13-14 
    Judges 13:1–14:20 
    John 1:29-51 
    Psalm 102:1-28 
    Proverbs 14:15-16 
    Judges 15:1–16:31 
    John 2:1-25 
    Psalm 103:1-22 
    Proverbs 14:17-19 
    Judges 17:1–18:31 
    John 3:1-21 
    Psalm 104:1-23 
    Proverbs 14:20-21 
    Judges 19:1–20:48 
    John 3:22–4:3 
    Psalm 104:24-35 
    Proverbs 14:22-24 
    John 4:4-42 
    Psalm 105:1-15 
    Ruth 2:1–4:22 
    John 4:43-54 
    Psalm 105:16-36 
    Proverbs 14:26-27 
    1 Samuel 1:1–2:21 
    John 5:1-23 
    Psalm 105:37-45 
    Proverbs 14:28-29 
    1 Samuel 2:22–4:22 
    John 5:24-47 
    Psalm 106:1-12 
    Proverbs 14:30-31 
    1 Samuel 5:1–7:17 
    John 6:1-21 
    Psalm 106:13-31 
    Proverbs 14:32-33 
    1 Samuel 8:1–9:27 
    John 6:22-42 
    Psalm 106:32-48 
    Proverbs 14:34-35 
    1 Samuel 10:1–11:15 
    John 6:43-71 
    Psalm 107:1-43 
    Proverbs 15:1-3 
    1 Samuel 12:1–13:23 
    John 7:1-30 
    Psalm 108:1-13 
    1 Samuel 14:1-52 
    John 7:31-53 
    Psalm 109:1-31 
    Proverbs 15:5-7 
    1 Samuel 15:1–16:23 
    John 8:1-20 
    Psalm 110:1-7 
    Proverbs 15:8-10 
    1 Samuel 17:1–18:4 
    John 8:21-30 
    1 Samuel 18:5–19:24 
    John 8:31-59 
    Proverbs 15:12-14 
    1 Samuel 20:1–21:15 
    John 9:1-41 
    Psalms 113:1–114:8 
    Proverbs 15:15-17 
    1 Samuel 22:1–23:29 
    John 10:1-21 
    Psalm 115:1-18 
    Proverbs 15:18-19 
    1 Samuel 24:1–25:44 
    John 10:22-42 
    Psalm 116:1-19 
    Proverbs 15:20-21 
    1 Samuel 26:1–28:25 
    John 11:1-54 
    Psalm 117:1-2 
    Proverbs 15:22-23 
    1 Samuel 29:1–31:13 
    John 11:55–12:19 
    Psalm 118:1-18 
    Proverbs 15:24-26 
    2 Samuel 1:1–2:11 
    John 12:20-50 
    Psalm 118:19-29 
    Proverbs 15:27-28 
    2 Samuel 2:12–3:39 
    John 13:1-30 
    Proverbs 15:29-30 
    2 Samuel 4:1–6:23 
    John 13:31–14:14 
    Psalm 119:17-32 
    Proverbs 15:31-32 
    2 Samuel 7:1–8:18 
    John 14:15-31 
    Psalm 119:33-48 
    2 Samuel 9:1–11:27 
    John 15:1-27 
    Psalm 119:49-64 
    Proverbs 16:1-3 
    2 Samuel 12:1-31 
    John 16:1-33 
    Psalm 119:65-80 
    Proverbs 16:4-5 
    2 Samuel 13:1-39 
    John 17:1-26 
    Psalm 119:81-96 
    Proverbs 16:6-7 
    2 Samuel 14:1–15:22 
    John 18:1-24 
    Psalm 119:97-112 
    Proverbs 16:8-9 
    2 Samuel 15:23–16:23 
    John 18:25–19:22 
    Psalm 119:113-128 
    Proverbs 16:10-11 
    2 Samuel 17:1-29 
    John 19:23-42 
    Psalm 119:129-152 
    Proverbs 16:12-13 
    2 Samuel 18:1–19:10 
    John 20:1-31 
    Psalm 119:153-176 
    Proverbs 16:14-15 
    2 Samuel 19:11–20:13 
    John 21:1-25 
    Psalm 120:1-7 
    Proverbs 16:16-17 
    2 Samuel 20:14–21:22 
    Acts 1:1-26 
    Psalm 121:1-8 
    2 Samuel 22:1–23:23 
    Acts 2:1-47 
    Psalm 122:1-9 
    Proverbs 16:19-20 
    2 Samuel 23:24–24:25 
    Acts 3:1-26 
    Psalm 123:1-4 
    Proverbs 16:21-23 
    1 Kings 1:1-53 
    Acts 4:1-37 
    Psalm 124:1-8 
    1 Kings 2:1–3:2 
    Acts 5:1-42 
    Psalm 125:1-5 
    1 Kings 3:3–4:34 
    Acts 6:1-15 
    Psalm 126:1-6 
    Proverbs 16:26-27 
    1 Kings 5:1–6:38 
    Acts 7:1-29 
    Psalm 127:1-5 
    Proverbs 16:28-30 
    1 Kings 7:1-51 
    Acts 7:30-50 
    Psalm 128:1-6 
    Proverbs 16:31-33 
    1 Kings 8:1-66 
    Acts 7:51–8:13 
    Psalm 129:1-8 
    1 Kings 9:1–10:29 
    Acts 8:14-40 
    Psalm 130:1-8 
    Proverbs 17:2-3 
    1 Kings 11:1–12:19 
    Acts 9:1-25 
    Psalm 131:1-3 
    Proverbs 17:4-5 
    1 Kings 12:20–13:34 
    Acts 9:26-43 
    Psalm 132:1-18 
    1 Kings 14:1–15:24 
    Acts 10:1-23 
    Psalm 133:1-3 
    Proverbs 17:7-8 
    1 Kings 15:25–17:24 
    Acts 10:24-48 
    Psalm 134:1-3 
    Proverbs 17:9-11 
    1 Kings 18:1-46 
    Acts 11:1-30 
    Psalm 135:1-21 
    Proverbs 17:12-13 
    1 Kings 19:1-21 
    Acts 12:1-23 
    Psalm 136:1-26 
    Proverbs 17:14-15 
    1 Kings 20:1–21:29 
    Acts 12:24–13:15 
    Psalm 137:1-9 
    1 Kings 22:1-53 
    Acts 13:16-41 
    Psalm 138:1-8 
    Proverbs 17:17-18 
    2 Kings 1:1–2:25 
    Acts 13:42–14:7 
    Psalm 139:1-24 
    Proverbs 17:19-21 
    2 Kings 3:1–4:17 
    Acts 14:8-28 
    Psalm 140:1-13 
    2 Kings 4:18–5:27 
    Acts 15:1-35 
    Psalm 141:1-10 
    2 Kings 6:1–7:20 
    Acts 15:36–16:15 
    Psalm 142:1-7 
    Proverbs 17:24-25 
    2 Kings 8:1–9:13 
    Acts 16:16-40 
    Psalm 143:1-12 
    2 Kings 9:14–10:31 
    Acts 17:1-34 
    Psalm 144:1-15 
    Proverbs 17:27-28 
    2 Kings 10:32–12:21 
    Acts 18:1-22 
    2 Kings 13:1–14:29 
    Acts 18:23–19:12 
    Psalm 146:1-10 
    Proverbs 18:2-3 
    2 Kings 15:1–16:20 
    Acts 19:13-41 
    Psalm 147:1-20 
    Proverbs 18:4-5 
    2 Kings 17:1–18:12 
    Acts 20:1-38 
    Psalm 148:1-14 
    Proverbs 18:6-7 
    2 Kings 18:13–19:37 
    Acts 21:1-17 
    Psalm 149:1-9 
    2 Kings 20:1–22:2 
    Acts 21:18-36 
    Psalm 150:1-6 
    Proverbs 18:9-10 
    2 Kings 22:3–23:30 
    Acts 21:37–22:16 
    Psalm 1:1-6 
    Proverbs 18:11-12 
    2 Kings 23:31–25:30 
    Acts 22:17–23:10 
    Psalm 2:1-12 
    1 Chronicles 1:1–2:17 
    Acts 23:11-35 
    Psalm 3:1-8 
    Proverbs 18:14-15 
    1 Chronicles 2:18–4:4 
    Acts 24:1-27 
    Psalm 4:1-8 
    Proverbs 18:16-18 
    1 Chronicles 4:5–5:17 
    Acts 25:1-27 
    Psalm 5:1-12 
    1 Chronicles 5:18–6:81 
    Acts 26:1-32 
    Psalm 6:1-10 
    Proverbs 18:20-21 
    1 Chronicles 7:1–8:40 
    Acts 27:1-20 
    Psalm 7:1-17 
    1 Chronicles 9:1–10:14 
    Acts 27:21-44 
    Psalm 8:1-9 
    Proverbs 18:23-24 
    1 Chronicles 11:1–12:18 
    Acts 28:1-31 
    Psalm 9:1-12 
    Proverbs 19:1-3 
    1 Chronicles 12:19–14:17 
    Romans 1:1-17 
    Psalm 9:13-20 
    Proverbs 19:4-5 
    1 Chronicles 15:1–16:36 
    Romans 1:18-32 
    Psalm 10:1-15 
    Proverbs 19:6-7 
    1 Chronicles 16:37–18:17 
    Romans 2:1-24 
    Psalm 10:16-18 
    Proverbs 19:8-9 
    1 Chronicles 19:1–21:30 
    Romans 2:25–3:8 
    Psalm 11:1-7 
    Proverbs 19:10-12 
    1 Chronicles 22:1–23:32 
    Romans 3:9-31 
    Psalm 12:1-8 
    Proverbs 19:13-14 
    1 Chronicles 24:1–26:11 
    Romans 4:1-12 
    Psalm 13:1-6 
    Proverbs 19:15-16 
    1 Chronicles 26:12–27:34 
    Romans 4:13–5:5 
    Psalm 14:1-7 
    1 Chronicles 28:1–29:30 
    Romans 5:6-21 
    Psalm 15:1-5 
    Proverbs 19:18-19 
    2 Chronicles 1:1–3:17 
    Romans 6:1-23 
    Psalm 16:1-11 
    Proverbs 19:20-21 
    2 Chronicles 4:1–6:11 
    Romans 7:1-13 
    Psalm 17:1-15 
    Proverbs 19:22-23 
    2 Chronicles 6:12–8:10 
    Romans 7:14–8:8 
    Psalm 18:1-15 
    Proverbs 19:24-25 
    2 Chronicles 8:11–10:19 
    Romans 8:9-25 
    Psalm 18:16-36 
    2 Chronicles 11:1–13:22 
    Romans 8:26-39 
    Psalm 18:37-50 
    Proverbs 19:27-29 
    2 Chronicles 14:1–16:14 
    Romans 9:1-24 
    Psalm 19:1-14 
    2 Chronicles 17:1–18:34 
    Romans 9:25–10:13 
    Psalm 20:1-9 
    Proverbs 20:2-3 
    2 Chronicles 19:1–20:37 
    Romans 10:14–11:12 
    Psalm 21:1-13 
    Proverbs 20:4-6 
    2 Chronicles 21:1–23:21 
    Romans 11:13-36 
    Psalm 22:1-18 
    2 Chronicles 24:1–25:28 
    Romans 12:1-21 
    Psalm 22:19-31 
    Proverbs 20:8-10 
    2 Chronicles 26:1–28:27 
    Romans 13:1-14 
    Psalm 23:1-6 
    2 Chronicles 29:1-36 
    Romans 14:1-23 
    Psalm 24:1-10 
    2 Chronicles 30:1–31:21 
    Romans 15:1-22 
    Proverbs 20:13-15 
    2 Chronicles 32:1–33:13 
    Romans 15:23–16:9 
    Psalm 25:16-22 
    Proverbs 20:16-18 
    2 Chronicles 33:14–34:33 
    Romans 16:10-27 
    Psalm 26:1-12 
    2 Chronicles 35:1–36:23 
    1 Corinthians 1:1-17 
    Psalm 27:1-6 
    Proverbs 20:20-21 
    Ezra 1:1–2:70 
    1 Corinthians 1:18–2:5 
    Psalm 27:7-14 
    Proverbs 20:22-23 
    Ezra 3:1–4:23 
    1 Corinthians 2:6–3:4 
    Psalm 28:1-9 
    Proverbs 20:24-25 
    Ezra 4:24–6:22 
    1 Corinthians 3:5-23 
    Psalm 29:1-11 
    Proverbs 20:26-27 
    Ezra 7:1–8:20 
    1 Corinthians 4:1-21 
    Psalm 30:1-12 
    Proverbs 20:28-30 
    Ezra 8:21–9:15 
    1 Corinthians 5:1-13 
    Psalm 31:1-8 
    Proverbs 21:1-2 
    Ezra 10:1-44 
    1 Corinthians 6:1-20 
    Psalm 31:9-18 
    Nehemiah 1:1–3:14 
    1 Corinthians 7:1-24 
    Psalm 31:19-24 
    Nehemiah 3:15–5:13 
    1 Corinthians 7:25-40 
    Psalm 32:1-11 
    Proverbs 21:5-7 
    1 Corinthians 8:1-13 
    Psalm 33:1-11 
    Proverbs 21:8-10 
    1 Corinthians 9:1-18 
    Psalm 33:12-22 
    Proverbs 21:11-12 
    Nehemiah 9:22–10:39 
    1 Corinthians 9:19–10:13 
    Nehemiah 11:1–12:26 
    1 Corinthians 10:14-33 
    Psalm 34:11-22 
    Proverbs 21:14-16 
    Nehemiah 12:27–13:31 
    1 Corinthians 11:1-16 
    Psalm 35:1-16 
    Proverbs 21:17-18 
    Esther 1:1–3:15 
    1 Corinthians 11:17-34 
    Psalm 35:17-28 
    Proverbs 21:19-20 
    Esther 4:1–7:10 
    1 Corinthians 12:1-26 
    Psalm 36:1-12 
    Proverbs 21:21-22 
    Esther 8:1–10:3 
    1 Corinthians 12:27–13:13 
    Proverbs 21:23-24 
    Job 1:1–3:26 
    1 Corinthians 14:1-17 
    Psalm 37:12-29 
    Proverbs 21:25-26 
    Job 4:1–7:21 
    1 Corinthians 14:18-40 
    Psalm 37:30-40 
    Job 8:1–11:20 
    1 Corinthians 15:1-28 
    Psalm 38:1-22 
    Proverbs 21:28-29 
    Job 12:1–15:35 
    1 Corinthians 15:29-58 
    Psalm 39:1-13 
    Proverbs 21:30-31 
    Job 16:1–19:29 
    1 Corinthians 16:1-24 
    Psalm 40:1-10 
    Job 20:1–22:30 
    2 Corinthians 1:1-11 
    Psalm 40:11-17 
    Proverbs 22:2-4 
    Job 23:1–27:23 
    2 Corinthians 1:12–2:11 
    Psalm 41:1-13 
    Proverbs 22:5-6 
    Job 28:1–30:31 
    2 Corinthians 2:12-17 
    Psalm 42:1-11 
    Job 31:1–33:33 
    2 Corinthians 3:1-18 
    Psalm 43:1-5 
    Proverbs 22:8-9 
    Job 34:1–36:33 
    2 Corinthians 4:1-12 
    Psalm 44:1-8 
    Proverbs 22:10-12 
    Job 37:1–39:30 
    2 Corinthians 4:13–5:10 
    Psalm 44:9-26 
    Job 40:1–42:17 
    2 Corinthians 5:11-21 
    Psalm 45:1-17 
    Ecclesiastes 1:1–3:22 
    2 Corinthians 6:1-13 
    Psalm 46:1-11 
    Ecclesiastes 4:1–6:12 
    2 Corinthians 6:14–7:7 
    Psalm 47:1-9 
    Ecclesiastes 7:1–9:18 
    2 Corinthians 7:8-16 
    Psalm 48:1-14 
    Proverbs 22:17-19 
    Ecclesiastes 10:1–12:14 
    2 Corinthians 8:1-15 
    Psalm 49:1-20 
    Proverbs 22:20-21 
    2 Corinthians 8:16-24 
    Psalm 50:1-23 
    Proverbs 22:22-23 
    2 Corinthians 9:1-15 
    Psalm 51:1-19 
    Proverbs 22:24-25 
    Isaiah 1:1–2:22 
    2 Corinthians 10:1-18 
    Psalm 52:1-9 
    Proverbs 22:26-27 
    Isaiah 3:1–5:30 
    2 Corinthians 11:1-15 
    Psalm 53:1-6 
    Proverbs 22:28-29 
    Isaiah 6:1–7:25 
    2 Corinthians 11:16-33 
    Psalm 54:1-7 
    Proverbs 23:1-3 
    Isaiah 8:1–9:21 
    2 Corinthians 12:1-10 
    Psalm 55:1-23 
    Proverbs 23:4-5 
    Isaiah 10:1–11:16 
    2 Corinthians 12:11-21 
    Psalm 56:1-13 
    Proverbs 23:6-8 
    Isaiah 12:1–14:32 
    2 Corinthians 13:1-14 
    Psalm 57:1-11 
    Proverbs 23:9-11 
    Isaiah 15:1–18:7 
    Galatians 1:1-24 
    Psalm 58:1-11 
    Isaiah 19:1–21:17 
    Galatians 2:1-16 
    Psalm 59:1-17 
    Proverbs 23:13-14 
    Isaiah 22:1–24:23 
    Galatians 2:17–3:9 
    Psalm 60:1-12 
    Proverbs 23:15-16 
    Isaiah 25:1–28:13 
    Galatians 3:10-22 
    Psalm 61:1-8 
    Proverbs 23:17-18 
    Isaiah 28:14–30:11 
    Galatians 3:23–4:31 
    Psalm 62:1-12 
    Proverbs 23:19-21 
    Isaiah 30:12–33:9 
    Galatians 5:1-12 
    Psalm 63:1-11 
    Isaiah 33:10–36:22 
    Galatians 5:13-26 
    Psalm 64:1-10 
    Isaiah 37:1–38:22 
    Galatians 6:1-18 
    Psalm 65:1-13 
    Isaiah 39:1–41:16 
    Ephesians 1:1-23 
    Psalm 66:1-20 
    Proverbs 23:25-28 
    Isaiah 41:17–43:13 
    Ephesians 2:1-22 
    Psalm 67:1-7 
    Proverbs 23:29-35 
    Isaiah 43:14–45:10 
    Ephesians 3:1-21 
    Psalm 68:1-18 
    Proverbs 24:1-2 
    Isaiah 45:11–48:11 
    Ephesians 4:1-16 
    Psalm 68:19-35 
    Proverbs 24:3-4 
    Isaiah 48:12–50:11 
    Ephesians 4:17-32 
    Psalm 69:1-18 
    Proverbs 24:5-6 
    Isaiah 51:1–53:12 
    Ephesians 5:1-33 
    Psalm 69:19-36 
    Isaiah 54:1–57:14 
    Ephesians 6:1-24 
    Psalm 70:1-5 
    Isaiah 57:15–59:21 
    Philippians 1:1-26 
    Psalm 71:1-24 
    Proverbs 24:9-10 
    Isaiah 60:1–62:5 
    Philippians 1:27–2:18 
    Psalm 72:1-20 
    Proverbs 24:11-12 
    Isaiah 62:6–65:25 
    Philippians 2:19–3:4 
    Psalm 73:1-28 
    Proverbs 24:13-14 
    Isaiah 66:1-24 
    Philippians 3:5-21 
    Psalm 74:1-23 
    Proverbs 24:15-16 
    Jeremiah 1:1–2:30 
    Philippians 4:1-23 
    Psalm 75:1-10 
    Proverbs 24:17-20 
    Jeremiah 2:31–4:18 
    Colossians 1:1-17 
    Psalm 76:1-12 
    Proverbs 24:21-22 
    Jeremiah 4:19–6:15 
    Colossians 1:18–2:7 
    Psalm 77:1-20 
    Proverbs 24:23-25 
    Jeremiah 6:16–8:7 
    Colossians 2:8-23 
    Psalm 78:1-31 
    Jeremiah 8:8–9:26 
    Colossians 3:1-17 
    Psalm 78:32-55 
    Jeremiah 10:1–11:23 
    Colossians 3:18–4:18 
    Psalm 78:56-72 
    Proverbs 24:28-29 
    Jeremiah 12:1–14:10 
    1 Thessalonians 1:1–2:8 
    Psalm 79:1-13 
    Proverbs 24:30-34 
    Jeremiah 14:11–16:15 
    1 Thessalonians 2:9–3:13 
    Psalm 80:1-19 
    Proverbs 25:1-5 
    Jeremiah 16:16–18:23 
    1 Thessalonians 4:1–5:3 
    Psalm 81:1-16 
    Proverbs 25:6-8 
    Jeremiah 19:1–21:14 
    1 Thessalonians 5:4-28 
    Psalm 82:1-8 
    Proverbs 25:9-10 
    Jeremiah 22:1–23:20 
    2 Thessalonians 1:1-12 
    Psalm 83:1-18 
    Proverbs 25:11-14 
    Jeremiah 23:21–25:38 
    2 Thessalonians 2:1-17 
    Psalm 84:1-12 
    Jeremiah 26:1–27:22 
    2 Thessalonians 3:1-18 
    Psalm 85:1-13 
    Jeremiah 28:1–29:32 
    1 Timothy 1:1-20 
    Psalm 86:1-17 
    Jeremiah 30:1–31:26 
    1 Timothy 2:1-15 
    Psalm 87:1-7 
    Proverbs 25:18-19 
    Jeremiah 31:27–32:44 
    1 Timothy 3:1-16 
    Psalm 88:1-18 
    Proverbs 25:20-22 
    Jeremiah 33:1–34:22 
    1 Timothy 4:1-16 
    Psalm 89:1-13 
    Proverbs 25:23-24 
    Jeremiah 35:1–36:32 
    1 Timothy 5:1-25 
    Psalm 89:14-37 
    Proverbs 25:25-27 
    Jeremiah 37:1–38:28 
    1 Timothy 6:1-21 
    Psalm 89:38-52 
    Jeremiah 39:1–41:18 
    2 Timothy 1:1-18 
    Psalms 90:1–91:16 
    Proverbs 26:1-2 
    Jeremiah 42:1–44:23 
    2 Timothy 2:1-21 
    Psalms 92:1–93:5 
    Proverbs 26:3-5 
    Jeremiah 44:24–47:7 
    2 Timothy 2:22–3:17 
    Psalm 94:1-23 
    Proverbs 26:6-8 
    Jeremiah 48:1–49:22 
    2 Timothy 4:1-22 
    Psalms 95:1–96:13 
    Proverbs 26:9-12 
    Jeremiah 49:23–50:46 
    Titus 1:1-16 
    Psalms 97:1–98:9 
    Proverbs 26:13-16 
    Jeremiah 51:1-53 
    Titus 2:1-15 
    Psalm 99:1-9 
    Jeremiah 51:54–52:34 
    Titus 3:1-15 
    Psalm 100:1-5 
    Proverbs 26:18-19 
    Philemon 1:1-25 
    Psalm 101:1-8 
    Lamentations 3:1-66 
    Hebrews 1:1-14 
    Psalm 102:1-28 
    Proverbs 26:21-22 
    Lamentations 4:1–5:22 
    Hebrews 2:1-18 
    Psalm 103:1-22 
    Ezekiel 1:1–3:15 
    Hebrews 3:1-19 
    Psalm 104:1-23 
    Proverbs 26:24-26 
    Ezekiel 3:16–6:14 
    Hebrews 4:1-16 
    Psalm 104:24-35 
    Ezekiel 7:1–9:11 
    Hebrews 5:1-14 
    Psalm 105:1-15 
    Ezekiel 10:1–11:25 
    Hebrews 6:1-20 
    Psalm 105:16-36 
    Proverbs 27:1-2 
    Ezekiel 12:1–14:11 
    Hebrews 7:1-17 
    Psalm 105:37-45 
    Ezekiel 14:12–16:41 
    Hebrews 7:18-28 
    Psalm 106:1-12 
    Proverbs 27:4-6 
    Ezekiel 16:42–17:24 
    Hebrews 8:1-13 
    Psalm 106:13-31 
    Proverbs 27:7-9 
    Ezekiel 18:1–19:14 
    Hebrews 9:1-10 
    Psalm 106:32-48 
    Ezekiel 20:1-49 
    Hebrews 9:11-28 
    Psalm 107:1-43 
    Ezekiel 21:1–22:31 
    Hebrews 10:1-17 
    Psalm 108:1-13 
    Ezekiel 23:1-49 
    Hebrews 10:18-39 
    Psalm 109:1-31 
    Ezekiel 24:1–26:21 
    Hebrews 11:1-16 
    Psalm 110:1-7 
    Ezekiel 27:1–28:26 
    Hebrews 11:17-31 
    Proverbs 27:15-16 
    Ezekiel 29:1–30:26 
    Hebrews 11:32–12:13 
    Ezekiel 31:1–32:32 
    Hebrews 12:14-29 
    Psalms 113:1–114:8 
    Proverbs 27:18-20 
    Ezekiel 33:1–34:31 
    Hebrews 13:1-25 
    Psalm 115:1-18 
    Proverbs 27:21-22 
    Ezekiel 35:1–36:38 
    James 1:1-18 
    Psalm 116:1-19 
    Proverbs 27:23-27 
    Ezekiel 37:1–38:23 
    James 1:19–2:17 
    Psalm 117:1-2 
    Ezekiel 39:1–40:27 
    James 2:18–3:18 
    Psalm 118:1-18 
    Ezekiel 40:28–41:26 
    James 4:1-17 
    Psalm 118:19-29 
    Proverbs 28:3-5 
    Ezekiel 42:1–43:27 
    James 5:1-20 
    Proverbs 28:6-7 
    Ezekiel 44:1–45:12 
    1 Peter 1:1-12 
    Psalm 119:17-32 
    Proverbs 28:8-10 
    Ezekiel 45:13–46:24 
    1 Peter 1:13–2:10 
    Psalm 119:33-48 
    Ezekiel 47:1–48:35 
    1 Peter 2:11–3:7 
    Psalm 119:49-64 
    Proverbs 28:12-13 
    Daniel 1:1–2:23 
    1 Peter 3:8–4:6 
    Psalm 119:65-80 
    Daniel 2:24–3:30 
    1 Peter 4:7–5:14 
    Psalm 119:81-96 
    Proverbs 28:15-16 
    Daniel 4:1-37 
    2 Peter 1:1-21 
    Psalm 119:97-112 
    Proverbs 28:17-18 
    Daniel 5:1-31 
    2 Peter 2:1-22 
    Psalm 119:113-128 
    Proverbs 28:19-20 
    Daniel 6:1-28 
    2 Peter 3:1-18 
    Psalm 119:129-152 
    Proverbs 28:21-22 
    Daniel 7:1-28 
    1 John 1:1-10 
    Psalm 119:153-176 
    Proverbs 28:23-24 
    Daniel 8:1-27 
    1 John 2:1-17 
    Psalm 120:1-7 
    Proverbs 28:25-26 
    Daniel 9:1–11:1 
    1 John 2:18–3:6 
    Psalm 121:1-8 
    Proverbs 28:27-28 
    Daniel 11:2-35 
    1 John 3:7-24 
    Psalm 122:1-9 
    Daniel 11:36–12:13 
    1 John 4:1-21 
    Psalm 123:1-4 
    Proverbs 29:2-4 
    Hosea 1:1–3:5 
    1 John 5:1-21 
    Psalm 124:1-8 
    Proverbs 29:5-8 
    Hosea 4:1–5:15 
    2 John 1:1-13 
    Psalm 125:1-5 
    Proverbs 29:9-11 
    Hosea 6:1–9:17 
    3 John 1:1-15 
    Psalm 126:1-6 
    Proverbs 29:12-14 
    Hosea 10:1–14:9 
    Jude 1:1-25 
    Psalm 127:1-5 
    Proverbs 29:15-17 
    Joel 1:1–3:21 
    Revelation 1:1-20 
    Psalm 128:1-6 
    Amos 1:1–3:15 
    Revelation 2:1-17 
    Psalm 129:1-8 
    Proverbs 29:19-20 
    Amos 4:1–6:14 
    Revelation 2:18–3:6 
    Psalm 130:1-8 
    Proverbs 29:21-22 
    Amos 7:1–9:15 
    Revelation 3:7-22 
    Psalm 131:1-3 
    Obadiah 1:1-21 
    Revelation 4:1-11 
    Psalm 132:1-18 
    Proverbs 29:24-25 
    Jonah 1:1–4:11 
    Revelation 5:1-14 
    Psalm 133:1-3 
    Proverbs 29:26-27 
    Micah 1:1–4:13 
    Revelation 6:1-17 
    Psalm 134:1-3 
    Proverbs 30:1-4 
    Micah 5:1–7:20 
    Revelation 7:1-17 
    Psalm 135:1-21 
    Proverbs 30:5-6 
    Nahum 1:1–3:19 
    Revelation 8:1-13 
    Psalm 136:1-26 
    Proverbs 30:7-9 
    Habakkuk 1:1–3:19 
    Revelation 9:1-21 
    Psalm 137:1-9 
    Zephaniah 1:1–3:20 
    Revelation 10:1-11 
    Psalm 138:1-8 
    Proverbs 30:11-14 
    Haggai 1:1–2:23 
    Revelation 11:1-19 
    Psalm 139:1-24 
    Proverbs 30:15-16 
    Zechariah 1:1-21 
    Revelation 12:1-17 
    Psalm 140:1-13 
    Zechariah 2:1–3:10 
    Revelation 12:18–13:18 
    Psalm 141:1-10 
    Proverbs 30:18-20 
    Zechariah 4:1–5:11 
    Revelation 14:1-20 
    Psalm 142:1-7 
    Proverbs 30:21-23 
    Zechariah 6:1–7:14 
    Revelation 15:1-8 
    Psalm 143:1-12 
    Proverbs 30:24-28 
    Zechariah 8:1-23 
    Revelation 16:1-21 
    Psalm 144:1-15 
    Proverbs 30:29-31 
    Zechariah 9:1-17 
    Revelation 17:1-18 
    Zechariah 10:1–11:17 
    Revelation 18:1-24 
    Psalm 146:1-10 
    Zechariah 12:1–13:9 
    Revelation 19:1-21 
    Psalm 147:1-20 
    Proverbs 31:1-7 
    Zechariah 14:1-21 
    Revelation 20:1-15 
    Psalm 148:1-14 
    Proverbs 31:8-9 
    Malachi 1:1–2:17 
    Revelation 21:1-27 
    Psalm 149:1-9 
    Proverbs 31:10-24 
    Malachi 3:1–4:6 
    Revelation 22:1-21 
    Psalm 150:1-6 
    Proverbs 31:25-31 

Partial expected output:

    Genesis 1:1–2:25
    Genesis 3:1–4:26
    Genesis 5:1–7:24
    Genesis 8:1–10:32
    Genesis 11:1–13:4
    ...
    Genesis 50:1—Exodus 2:10
    Exodus 2:11–3:22
    Exodus 4:1–5:21
    ...

where if not possible, there can be no order between chapters, e.g. between "Genesis" and "Exodus".

3
  • Please correct your sample files to provide equivalent "key" between the "Books.txt" file and the "Chapter-Verse.txt" file. Specifically, One file refers to "Books" as 1 John, 2 John etc., while the other refers to the "Books" as 1st John, 2nd John etc. Thank you! Oct 15, 2023 at 18:57
  • 1
    Please reduce your sample input/output to a minimal example that demonstrates your needs and make sure to provide the full exact desired output for the input you provide - providing a partial output that contains things not present in the input (e.g. Genesis 50:1—Exodus 2:10) is not useful and neither is providing such a massive amount of data that we can't see at a glance if there's any issues in the input or our tool output.
    – Ed Morton
    Oct 16, 2023 at 16:28
  • 1
    As a good rule of thumb, we shouldn't need to use scroll bars to read your example.
    – Ed Morton
    Oct 16, 2023 at 16:38

3 Answers 3

1

My go-to for this is DSU (decorate/sort/undecorate).

You come up with a fresh key which is in conanical fixed-with format. That is, you allow the largest book name (maybe upper-casing if there are variations), make numbers fixed-length and zero-padded (special treatment for floating-point etc), and any other format issues. If your data is UTF-8, you might want to ASCII-fy the key part and sort under LC_ALL=C.

So your DSU might start off by adding a new key on the front, looking like:

MATTHEW          |002|013|Matthew 2:13–3:6
GENESIS          |013|005|Genesis 13:5–15:21
PSALM            |007|001|Psalm 7:1-17

That enables you to do a vanilla sort -- no key specifications at all, which string-sorts on the whole line.

Finally, you cut off the first 26 (or whatever) bytes after the sort completes.

I don't know how you would deal with the ranges like 11:1–13:4, or any overlaps (I'm not even sure what they would mean).

I normally use awk to do the decorate part, with a suitable printf format.

There are lists of the books of the Bible in order online (in fact, several different editions). That would be a side issue of a verified lookup table of around 80 titles, and use the index number for DSU instead of the text name. Awk is good in that context too. There may be spelling distinctions (2 for II etc).

This is actually slightly better than using sort keys. sort does the comparisons on multiple keys at every compare, so usually O(n log n) times. DSU does it n times up front, which costs O(n). It does not impact performance in the comparison phase, because that terminates on the first difference each time anyway.

1

Using Raku (formerly known as Perl_6)

~$ raku -e 'my @books = "/path/to/books.txt".IO.lines; my %verses;
            for "/path/to/verses.txt".IO.lines.map( *.match( / (^ .+? )  \s+ ((\d+)  \:  .+? ) $ /)) {
                %verses.push: ($_.[0].Str => $_.[1][0].Str => $_.[1].Str)
            };  for @books { put $_ => %verses{$_}  if %verses{$_} };'

This answer uses the Raku programming language, which provides Perl-style %-sigiled hashes to accomplish the OP's collating task.

First lines of the books.txt file are read-in and stored in the @books array. Second lines of the verses.txt file are iterated over. A map call is used to address each line, and matches are sought for (^ .+?) the initial words on the line ahead of \s+ one-or-more space ahead of ((\d+) \: .+?) the chapter/verse structure that ends every line. Parentheses are used to capture matches, so the book, chapter, and chapter/verse are all captured and pushed onto the %verses hash. Then simply, to get books in the desired order the @books array is iterated over, and the %verses hash is interrogated to see if it contains a key equivalent to the $_ topic variable. If so, the $_ => %verses{$_} key and value(s) are returned (the if %verses{$_} clause suppresses output of books without values).

Note, it's probably easier to read output if you use the say call:

Sample Output (using say, first 10 lines only):

Genesis => [1 => 1:1–2:25 3 => 3:1–4:26 5 => 5:1–7:24 8 => 8:1–10:32 11 => 11:1–13:4 13 => 13:5–15:21 16 => 16:1–18:15 18 => 18:16–19:38 20 => 20:1–22:24 23 => 23:1–24:51 24 => 24:52–26:16 26 => 26:17–27:46 28 => 28:1–29:35 30 => 30:1–31:16 31 => 31:17–32:12 32 => 32:13–34:31 35 => 35:1–36:43 37 => 37:1–38:30 39 => 39:1–41:16 41 => 41:17–42:17 42 => 42:18–43:34 44 => 44:1–45:28 46 => 46:1–47:31 48 => 48:1–49:33]
Exodus => [2 => 2:11–3:22 4 => 4:1–5:21 5 => 5:22–7:25 8 => 8:1–9:35 10 => 10:1–12:13 12 => 12:14–13:16 13 => 13:17–15:18 15 => 15:19–17:7 17 => 17:8–19:15 19 => 19:16–21:21 21 => 21:22–23:13 23 => 23:14–25:40 26 => 26:1–27:21 28 => 28:1-43 29 => 29:1–30:10 30 => 30:11–31:18 32 => 32:1–33:23 34 => 34:1–35:9 35 => 35:10–36:38 37 => 37:1–38:31 39 => 39:1–40:38]
Leviticus => [1 => 1:1–3:17 4 => 4:1–5:19 6 => 6:1–7:27 7 => 7:28–9:6 9 => 9:7–10:20 11 => 11:1–12:8 13 => 13:1-59 14 => 14:1-57 15 => 15:1–16:28 16 => 16:29–18:30 19 => 19:1–20:21 20 => 20:22–22:16 22 => 22:17–23:44 24 => 24:1–25:46 25 => 25:47–27:13]
Numbers => [2 => 2:1–3:51 4 => 4:1–5:31 6 => 6:1–7:89 8 => 8:1–9:23 10 => 10:1–11:23 11 => 11:24–13:33 14 => 14:1–15:16 15 => 15:17–16:40 16 => 16:41–18:32 19 => 19:1–20:29 21 => 21:1–22:20 22 => 22:21–23:30 24 => 24:1–25:18 26 => 26:1-51 26 => 26:52–28:15 28 => 28:16–29:40 30 => 30:1–31:54 32 => 32:1–33:39 33 => 33:40–35:34]
Deuteronomy => [2 => 2:1–3:29 4 => 4:1-49 5 => 5:1–6:25 7 => 7:1–8:20 9 => 9:1–10:22 11 => 11:1–12:32 13 => 13:1–15:23 16 => 16:1–17:20 18 => 18:1–20:20 21 => 21:1–22:30 23 => 23:1–25:19 26 => 26:1–27:26 28 => 28:1-68 29 => 29:1–30:20 31 => 31:1–32:27 32 => 32:28-52 33 => 33:1-29]
Joshua => [3 => 3:1–4:24 5 => 5:1–7:15 7 => 7:16–9:2 9 => 9:3–10:43 11 => 11:1–12:24 13 => 13:1–14:15 15 => 15:1-63 16 => 16:1–18:28 19 => 19:1–20:9 21 => 21:1–22:20 22 => 22:21–23:16 24 => 24:1-33]
Judges => [1 => 1:1–2:9 2 => 2:10–3:31 4 => 4:1–5:31 6 => 6:1-40 7 => 7:1–8:17 8 => 8:18–9:21 9 => 9:22–10:18 11 => 11:1–12:15 13 => 13:1–14:20 15 => 15:1–16:31 17 => 17:1–18:31 19 => 19:1–20:48]
Ruth => 2 => 2:1–4:22
Ezra => [1 => 1:1–2:70 3 => 3:1–4:23 4 => 4:24–6:22 7 => 7:1–8:20 8 => 8:21–9:15 10 => 10:1-44]
Nehemiah => [1 => 1:1–3:14 3 => 3:15–5:13 9 => 9:22–10:39 11 => 11:1–12:26 12 => 12:27–13:31]

Sample Output (all lines, using put which separates columns by \t):

Genesis 1   1:1–2:25 3  3:1–4:26 5  5:1–7:24 8  8:1–10:32 11    11:1–13:4 13    13:5–15:21 16   16:1–18:15 18   18:16–19:38 20  20:1–22:24 23   23:1–24:51 24   24:52–26:16 26  26:17–27:46 2828:1–29:35 30 30:1–31:16 31   31:17–32:12 32  32:13–34:31 35  35:1–36:43 37   37:1–38:30 39   39:1–41:16 41   41:17–42:17 42  42:18–43:34 44  44:1–45:28 46   46:1–47:31 48   48:1–49:33
Exodus  2   2:11–3:22 4 4:1–5:21 5  5:22–7:25 8 8:1–9:35 10 10:1–12:13 12   12:14–13:16 13  13:17–15:18 15  15:19–17:7 17   17:8–19:15 19   19:16–21:21 21  21:22–23:13 23  23:14–25:40 2626:1–27:21 28 28:1-43 29  29:1–30:10 30   30:11–31:18 32  32:1–33:23 34   34:1–35:9 35    35:10–36:38 37  37:1–38:31 39   39:1–40:38
Leviticus   1   1:1–3:17 4  4:1–5:19 6  6:1–7:27 7  7:28–9:6 9  9:7–10:20 11    11:1–12:8 13    13:1-59 14  14:1-57 15  15:1–16:28 16   16:29–18:30 19  19:1–20:21 20   20:22–22:16 22  22:17–23:44 24  24:1–25:46 25   25:47–27:13
Numbers 2   2:1–3:51 4  4:1–5:31 6  6:1–7:89 8  8:1–9:23 10 10:1–11:23 11   11:24–13:33 14  14:1–15:16 15   15:17–16:40 16  16:41–18:32 19  19:1–20:29 21   21:1–22:20 22   22:21–23:30 2424:1–25:18 26 26:1-51 26  26:52–28:15 28  28:16–29:40 30  30:1–31:54 32   32:1–33:39 33   33:40–35:34
Deuteronomy 2   2:1–3:29 4  4:1-49 5    5:1–6:25 7  7:1–8:20 9  9:1–10:22 11    11:1–12:32 13   13:1–15:23 16   16:1–17:20 18   18:1–20:20 21   21:1–22:30 23   23:1–25:19 26   26:1–27:26 28   28:1-68 29  29:1–30:20 31   31:1–32:27 32   32:28-52 33 33:1-29
Joshua  3   3:1–4:24 5  5:1–7:15 7  7:16–9:2 9  9:3–10:43 11    11:1–12:24 13   13:1–14:15 15   15:1-63 16  16:1–18:28 19   19:1–20:9 21    21:1–22:20 22   22:21–23:16 24  24:1-33
Judges  1   1:1–2:9 2   2:10–3:31 4 4:1–5:31 6  6:1-40 7    7:1–8:17 8  8:18–9:21 9 9:22–10:18 11   11:1–12:15 13   13:1–14:20 15   15:1–16:31 17   17:1–18:31 19   19:1–20:48
Ruth    2   2:1–4:22
Ezra    1   1:1–2:70 3  3:1–4:23 4  4:24–6:22 7 7:1–8:20 8  8:21–9:15 10    10:1-44
Nehemiah    1   1:1–3:14 3  3:15–5:13 9 9:22–10:39 11   11:1–12:26 12   12:27–13:31
Esther  1   1:1–3:15 4  4:1–7:10 8  8:1–10:3
Job 1   1:1–3:26 4  4:1–7:21 8  8:1–11:20 12    12:1–15:35 16   16:1–19:29 20   20:1–22:30 23   23:1–27:23 28   28:1–30:31 31   31:1–33:33 34   34:1–36:33 37   37:1–39:30 40   40:1–42:17
Psalms  90  90:1–91:16 92   92:1–93:5 95    95:1–96:13 97   97:1–98:9 113   113:1–114:8 90  90:1–91:16 92   92:1–93:5 95    95:1–96:13 97   97:1–98:9 113   113:1–114:8
Proverbs    1   1:1-6 1 1:7-9 1 1:10-19 1   1:20-23 1   1:24-28 1   1:29-33 2   2:1-5 2 2:6-15 2    2:16-22 3   3:1-6 3 3:7-8 3 3:9-10 3    3:11-12 3   3:13-15 3   3:16-18 3   3:19-20 3   3:21-26 3   3:27-32 3   3:33-35 4   4:1-6 4 4:7-10 4    4:11-13 4   4:14-19 4   4:20-27 5   5:1-6 5 5:7-14 5    5:15-21 5   5:22-23 6   6:1-5 6 6:6-11 6    6:12-15 6   6:16-19 6   6:20-26 6   6:27-35 7   7:1-5 7 7:6-23 7    7:24-27 8   8:1-11 8    8:12-13 8   8:14-26 8   8:27-32 8   8:33-36 9   9:1-6 9 9:7-8 9 9:9-10 9    9:11-12 9   9:13-18 10  10:1-2 10   10:3-4 10   10:6-7 10   10:8-9 10   10:11-12 10 10:13-14 10 10:15-16 10 10:20-21 10 10:24-25 10 10:27-28 10 10:29-30 10 10:31-32 11 11:1-3 11   11:5-6 11   11:9-11 11  11:12-13 11 11:16-17 11 11:18-19 11 11:20-21 11 11:24-26 11 11:29-31 12 12:2-3 12   12:5-7 12   12:8-9 12   12:12-14 12 12:15-17 12 12:19-20 12 12:21-23 12 12:27-28 13 13:2-3 13   13:5-6 13   13:7-8 13   13:9-10 13  13:12-14 13 13:15-16 13 13:17-19 13 13:20-23 13 13:24-25 14 14:1-2 14   14:3-4 14   14:5-6 14   14:7-8 14   14:9-10 14  14:11-12 14 14:13-14 14 14:15-16 14 14:17-19 14 14:20-21 14 14:22-24 14 14:26-27 14 14:28-29 14 14:30-31 14 14:32-33 14 14:34-35 15 15:1-3 15   15:5-7 15   15:8-10 15  15:12-14 15 15:15-17 15 15:18-19 15 15:20-21 15 15:22-23 15 15:24-26 15 15:27-28 15 15:29-30 15 15:31-32 16 16:1-3 16   16:4-5 16   16:6-7 16   16:8-9 16   16:10-11 16 16:12-13 16 16:14-15 16 16:16-17 16 16:19-20 16 16:21-23 16 16:26-27 16 16:28-30 16 16:31-33 17 17:2-3 17   17:4-5 17   17:7-8 17   17:9-11 17  17:12-13 17 17:14-15 17 17:17-18 17 17:19-21 17 17:24-25 17 17:27-28 18 18:2-3 18   18:4-5 18   18:6-7 18   18:9-10 18  18:11-12 18 18:14-15 18 18:16-18 18 18:20-21 18 18:23-24 19 19:1-3 19   19:4-5 19   19:6-7 19   19:8-9 19   19:10-12 19 19:13-14 19 19:15-16 19 19:18-19 19 19:20-21 19 19:22-23 19 19:24-25 19 19:27-29 20 20:2-3 20   20:4-6 20   20:8-10 20  20:13-15 20 20:16-18 20 20:20-21 20 20:22-23 20 20:24-25 20 20:26-27 20 20:28-30 21 21:1-2 21   21:5-7 21   21:8-10 21  21:11-12 21 21:14-16 21 21:17-18 21 21:19-20 21 21:21-22 21 21:23-24 21 21:25-26 21 21:28-29 21 21:30-31 22 22:2-4 22   22:5-6 22   22:8-9 22   22:10-12 22 22:17-19 22 22:20-21 22 22:22-23 22 22:24-25 22 22:26-27 22 22:28-29 23 23:1-3 23   23:4-5 23   23:6-8 23   23:9-11 23  23:13-14 23 23:15-16 23 23:17-18 23 23:19-21 23 23:25-28 23 23:29-35 24 24:1-2 24   24:3-4 24   24:5-6 24   24:9-10 24  24:11-12 24 24:13-14 24 24:15-16 24 24:17-20 24 24:21-22 24 24:23-25 24 24:28-29 24 24:30-34 25 25:1-5 25   25:6-8 25   25:9-10 25  25:11-14 25 25:18-19 25 25:20-22 25 25:23-24 25 25:25-27 26 26:1-2 26   26:3-5 26   26:6-8 26   26:9-12 26  26:13-16 26 26:18-19 26 26:21-22 26 26:24-26 27 27:1-2 27   27:4-6 27   27:7-9 27   27:15-16 27 27:18-20 27 27:21-22 27 27:23-27 28 28:3-5 28   28:6-7 28   28:8-10 28  28:12-13 28 28:15-16 28 28:17-18 28 28:19-20 28 28:21-22 28 28:23-24 28 28:25-26 28 28:27-28 29 29:2-4 29   29:5-8 29   29:9-11 29  29:12-14 29 29:15-17 29 29:19-20 29 29:21-22 29 29:24-25 29 29:26-27 30 30:1-4 30   30:5-6 30   30:7-9 30   30:11-14 30 30:15-16 30 30:18-20 30 30:21-23 30 30:24-28 30 30:29-31 31 31:1-7 31   31:8-9 31   31:10-24 31 31:25-31
Ecclesiastes    1   1:1–3:22 4  4:1–6:12 7  7:1–9:18 10 10:1–12:14
Isaiah  1   1:1–2:22 3  3:1–5:30 6  6:1–7:25 8  8:1–9:21 10 10:1–11:16 12   12:1–14:32 15   15:1–18:7 19    19:1–21:17 22   22:1–24:23 25   25:1–28:13 28   28:14–30:11 30  30:12–33:9 3333:10–36:22 37 37:1–38:22 39   39:1–41:16 41   41:17–43:13 43  43:14–45:10 45  45:11–48:11 48  48:12–50:11 51  51:1–53:12 54   54:1–57:14 57   57:15–59:21 60  60:1–62:5 62    62:6–65:25 66   66:1-24
Jeremiah    1   1:1–2:30 2  2:31–4:18 4 4:19–6:15 6 6:16–8:7 8  8:8–9:26 10 10:1–11:23 12   12:1–14:10 14   14:11–16:15 16  16:16–18:23 19  19:1–21:14 22   22:1–23:20 23   23:21–25:38 26  26:1–27:22 28   28:1–29:32 30   30:1–31:26 31   31:27–32:44 33  33:1–34:22 35   35:1–36:32 37   37:1–38:28 39   39:1–41:18 42   42:1–44:23 44   44:24–47:7 48   48:1–49:22 49   49:23–50:46 5151:1-53 51    51:54–52:34
Lamentations    3   3:1-66 4    4:1–5:22
Ezekiel 1   1:1–3:15 3  3:16–6:14 7 7:1–9:11 10 10:1–11:25 12   12:1–14:11 14   14:12–16:41 16  16:42–17:24 18  18:1–19:14 20   20:1-49 21  21:1–22:31 23   23:1-49 24  24:1–26:21 2727:1–28:26 29  29:1–30:26 31   31:1–32:32 33   33:1–34:31 35   35:1–36:38 37   37:1–38:23 39   39:1–40:27 40   40:28–41:26 42  42:1–43:27 44   44:1–45:12 45   45:13–46:24 47  47:1–48:35
Daniel  1   1:1–2:23 2  2:24–3:30 4 4:1-37 5    5:1-31 6    6:1-28 7    7:1-28 8    8:1-27 9    9:1–11:1 11 11:2-35 11  11:36–12:13
Hosea   1   1:1–3:5 4   4:1–5:15 6  6:1–9:17 10 10:1–14:9
Joel    1   1:1–3:21
Amos    1   1:1–3:15 4  4:1–6:14 7  7:1–9:15
Obadiah 1   1:1-21
Jonah   1   1:1–4:11
Micah   1   1:1–4:13 5  5:1–7:20
Nahum   1   1:1–3:19
Habakkuk    1   1:1–3:19
Zephaniah   1   1:1–3:20
Haggai  1   1:1–2:23
Zechariah   1   1:1-21 2    2:1–3:10 4  4:1–5:11 6  6:1–7:14 8  8:1-23 9    9:1-17 10   10:1–11:17 12   12:1–13:9 14    14:1-21
Malachi 1   1:1–2:17 3  3:1–4:6
Matthew 1   1:1–2:12 2  2:13–3:6 3  3:7–4:11 4  4:12-25 5   5:1-26 5    5:27-48 6   6:1-24 6    6:25–7:14 7 7:15-29 8   8:1-17 8    8:18-34 9   9:1-17 9    9:18-38 10  10:1-23 10  10:24–11:6 11   11:7-30 12  12:1-21 12  12:22-45 12 12:46–13:23 13  13:24-46 13 13:47–14:12 14  14:13-36 15 15:1-28 15  15:29–16:12 16  16:13–17:9 1717:10-27 18    18:1-20 18  18:21–19:12 19  19:13-30 20 20:1-28 20  20:29–21:22 21  21:23-46 22 22:1-33 22  22:34–23:12 23  23:13-39 24 24:1-28 24  24:29-51 25 25:1-30 25  25:31–26:13 26  26:14-46 26 26:47-68 26 26:69–27:14 27  27:15-31 27 27:32-66 28 28:1-20
Mark    1   1:1-28 1    1:29–2:12 2 2:13–3:6 3  3:7-30 3    3:31–4:25 4 4:26–5:20 5 5:21-43 6   6:1-29 6    6:30-56 7   7:1-23 7    7:24–8:10 8 8:11-38 9   9:1-29 9    9:30–10:12 10   10:13-31 10 10:32-52 11 11:1-25 11  11:27–12:17 12  12:18-37 12 12:38–13:13 13  13:14-37 14 14:1-21 14  14:22-52 14 14:53-72 15 15:1-47 16  16:1-20
Luke    1   1:1-25 1    1:26-56 1   1:57-80 2   2:1-35 2    2:36-52 3   3:1-22 3    3:23-38 4   4:1-30 4    4:31–5:11 5 5:12-28 5   5:29–6:11 6 6:12-38 6   6:39–7:10 7 7:11-35 7   7:36–8:3 8  8:4-21 8    8:22-39 8   8:40–9:6 9  9:7-27 9    9:28-50 9   9:51–10:12 10   10:13-37 10 10:38–11:13 11  11:14-36 11 11:37–12:7 1212:8-34 12 12:35-59 13 13:1-21 13  13:22–14:6 14   14:7-35 15  15:1-32 16  16:1-18 16  16:19–17:10 17  17:11-37 18 18:1-17 18  18:18-43 19 19:1-27 19  19:28-48 20 20:1-26 20  20:27-47 21 21:1-28 21  21:29–22:13 22  22:14-34 22 22:35-53 22 22:54–23:12 23  23:13-43 23 23:44–24:12 24  24:13-53
John    1   1:1-28 1    1:29-51 2   2:1-25 3    3:1-21 3    3:22–4:3 4  4:4-42 4    4:43-54 5   5:1-23 5    5:24-47 6   6:1-21 6    6:22-42 6   6:43-71 7   7:1-30 7    7:31-53 8   8:1-20 8    8:21-30 8   8:31-59 9   9:1-41 10   10:1-21 10  10:22-42 11 11:1-54 11  11:55–12:19 12  12:20-50 13 13:1-30 13  13:31–14:14 14  14:15-31 15 15:1-27 16  16:1-33 17  17:1-26 18  18:1-24 18  18:25–19:22 19  19:23-42 20 20:1-31 21  21:1-25
Acts    1   1:1-26 2    2:1-47 3    3:1-26 4    4:1-37 5    5:1-42 6    6:1-15 7    7:1-29 7    7:30-50 7   7:51–8:13 8 8:14-40 9   9:1-25 9    9:26-43 10  10:1-23 10  10:24-48 11 11:1-30 12  12:1-23 12  12:24–13:15 13  13:16-41 13 13:42–14:7 14   14:8-28 15  15:1-35 15  15:36–16:15 16  16:16-40 17 17:1-34 18  18:1-22 18  18:23–19:12 19  19:13-41 20 20:1-38 21  21:1-17 21  21:18-36 21 21:37–22:16 22  22:17–23:10 23  23:11-35 24 24:1-27 25  25:1-27 26  26:1-32 27  27:1-20 27  27:21-44 28 28:1-31
Romans  1   1:1-17 1    1:18-32 2   2:1-24 2    2:25–3:8 3  3:9-31 4    4:1-12 4    4:13–5:5 5  5:6-21 6    6:1-23 7    7:1-13 7    7:14–8:8 8  8:9-25 8    8:26-39 9   9:1-24 9    9:25–10:13 10   10:14–11:12 11  11:13-36 12 12:1-21 13  13:1-14 14  14:1-23 15  15:1-22 15  15:23–16:9 16   16:10-27
Galatians   1   1:1-24 2    2:1-16 2    2:17–3:9 3  3:10-22 3   3:23–4:31 5 5:1-12 5    5:13-26 6   6:1-18
Ephesians   1   1:1-23 2    2:1-22 3    3:1-21 4    4:1-16 4    4:17-32 5   5:1-33 6    6:1-24
Philippians 1   1:1-26 1    1:27–2:18 2 2:19–3:4 3  3:5-21 4    4:1-23
Colossians  1   1:1-17 1    1:18–2:7 2  2:8-23 3    3:1-17 3    3:18–4:18
Titus   1   1:1-16 2    2:1-15 3    3:1-15
Philemon    1   1:1-25
Hebrews 1   1:1-14 2    2:1-18 3    3:1-19 4    4:1-16 5    5:1-14 6    6:1-20 7    7:1-17 7    7:18-28 8   8:1-13 9    9:1-10 9    9:11-28 10  10:1-17 10  10:18-39 11 11:1-16 11  11:17-31 11 11:32–12:13 12  12:14-29 13 13:1-25
James   1   1:1-18 1    1:19–2:17 2 2:18–3:18 4 4:1-17 5    5:1-20
Jude    1   1:1-25
Revelation  1   1:1-20 2    2:1-17 2    2:18–3:6 3  3:7-22 4    4:1-11 5    5:1-14 6    6:1-17 7    7:1-17 8    8:1-13 9    9:1-21 10   10:1-11 11  11:1-19 12  12:1-17 12  12:18–13:18 14  14:1-20 15  15:1-8 16   16:1-21 17  17:1-18 18  18:1-24 19  19:1-21 20  20:1-15 21  21:1-27 22  22:1-21

It seems the OP may have provided test data that is already in correct sort order. If you need to sort by "chapter", switch out the last Raku statement above and use the following instead:

say ($_ => %verses{$_}.sort: *.key.Int) if %verses{$_} for @books;

Nota bene: Any inconsistencies between "keys" in the two analyzed files will result in keys (i.e. Books) being dropped from the final output. So make sure keys (like 1 John, 2 John etc.) are consistently named (and not, for example, 1st John, 2nd John etc.).

https://docs.raku.org
https://raku.org

0

Off the top of my head (i.e. not fully tested, but seems to make sense with a small sample):

while read -r book; { grep $book text.file | sort -V }; done < reflist.txt

(sort -V also handy for IP addresses and other things)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .