2

I have a single transcript file for a set of course lectures I am watching. After doing some formatting on the file. I have it broken up into sets of lessons where the main heading is the lesson title and each subheading is the lecture title.

I'm trying to format this file in a way that is easily searchable when taking notes. Here is an example format:

--------------------------------------------------------------------------------

L01 - Introduction to Knowledge-Based AI

01 - Introductions

>> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
   quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
   consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
   cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
   proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

02 - Preview

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

.
.
.

22 - Final Quiz

Lorem ipsum dolor sit amet.

--------------------------------------------------------------------------------

L02 - Introduction to CS7637

01 - Preview

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

.
.
.

14 - 2x1 Matrices IV

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.

33 - Final Quiz

Lorem ipsum dolor sit amet.

________________________________________________________________________________

L03 - Semantic Networks

01 - Preview

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

04 - Exercise Constructing Semantic Nets I

>> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
   quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
   consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
   cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
   proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

.
.
.

A few things of note:

  • Each lesson is preceded by a line of - 80 characters long
  • Each lesson is titled by LXX where XX is the lesson number
  • The lecture are titled by a leading number.
  • These numbers aren't necessarily sequential but monotonically increase.

I would like to be able to prepend all lecture titles with its respective lesson number so that I can query a specific chunk of text while watching the video. So my expected output would be

--------------------------------------------------------------------------------

L01 - Introduction to Knowledge-Based AI

L01E01 - Introductions

>> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
   quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
   consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
   cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
   proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

L01E02 - Preview

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

.
.
.

L01E22 - Final Quiz

Lorem ipsum dolor sit amet.

--------------------------------------------------------------------------------

L02 - Introduction to CS7637

L02E01 - Preview

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

.
.
.

L02E14 - 2x1 Matrices IV

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.

L02E33 - Final Quiz

Lorem ipsum dolor sit amet.

________________________________________________________________________________

L03 - Semantic Networks

L03E01 - Preview

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

L03E04 - Exercise Constructing Semantic Nets I

>> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
   quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
   consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
   cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
   proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

.
.
.

I think something like a 2-part regexp replace would be something like

Find all instances of this match
C-u 80 - C-q C-j C-q C-j \(L[0-9]+\)

Then find all proceding instances of this match
\([0-9]+ - .*$\)

And replace it with
\1E\2

Is there a way to do this in emacs?

UPDATE:

I've managed to get a small bit of code that only accomplishes the replace a single time and then errors out, it might be a good start though:

(with-current-buffer "transcripts.txt"
  (re-search-forward "--------------------------------------------------------------------------------

\\(L[0-9]+\\) - .*$")
  (setq match (match-string 1))
  (re-search-forward "E[0-9]+ - .*$")
  (beginning-of-line)
  (insert match))
  • 1
    I would use a keyboard macro which looks for the lesson boundaries, narrows the buffer to a single lesson's region, finds the lesson number, and search+replaces all lesson titles using that number, then goes to the end and widens the buffer. Then run the macro until there's nothing left to do. – phils Feb 28 at 21:18
2

An advanced usage of M-x query-replace-regexp (or indeed non-query M-x replace-regexp) which achieves this is...

Replace: ^\(L?[0-9]+\) -

With: \,(if (string-match-p "^L" \1) (setq lesson \1) (concat lesson "E" \1)) -

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.