1

I have numbers in [.] indicating the page number of an orginal printed document I am transcribing. However, the document also contains other numbers in [.] that do not correspond to page numbers.

For example:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing [267] elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in est.

Vivamus fermentum semper porta. Nunc diam velit, adipiscing ut tristique vitae, sagittis vel odio. Maecenas convallis ullamcorper ultricies. Curabitur ornare, ligula semper consectetur sagittis, nisi diam iaculis velit, id [1] fringilla sem nunc vel mi. Nam dictum, odio nec pretium volutpat, arcu ante placerat erat, non tristique elit urna et turpis. Quisque mi metus, ornare sit amet fermentum et, tincidunt et orci. Fusce eget orci a orci congue vestibulum. Ut dolor diam, elementum et vestibulum eu, porttitor vel elit. Curabitur venenatis pulvinar tellus gravida ornare. Sed et erat faucibus nunc euismod ultricies ut id justo. Nullam cursus suscipit nisi, et ultrices justo sodales nec. Fusce venenatis facilisis lectus ac semper. Aliquam at massa ipsum. Quisque bibendum purus [2] convallis nulla ultrices ultricies. Nullam aliquam, mi eu aliquam tincidunt, purus velit laoreet tortor, viverra pretium nisi quam vitae mi. Fusce vel volutpat elit. Nam sagittis nisi dui.

Suspendisse lectus leo, consectetur in tempor sit amet, placerat quis neque. Etiam luctus porttitor [1] lorem, sed suscipit est rutrum non. Curabitur lobortis nisl a enim congue semper. Aenean commodo ultrices imperdiet. [3] Vestibulum ut justo vel sapien venenatis tincidunt. Phasellus eget dolor sit amet ipsum dapibus condimentum vitae quis lectus. Aliquam ut massa in turpis dapibus convallis. Praesent elit lacus, vestibulum at malesuada et, ornare et est. Ut augue nunc, sodales ut euismod non, adipiscing vitae orci. Mauris ut placerat justo. Mauris in ultricies enim. Quisque nec est eleifend nulla ultrices egestas quis ut quam. Donec sollicitudin lectus a mauris pulvinar id aliquam urna cursus. Cras quis ligula sem, vel elementum mi. Phasellus non ullamcorper urna. [24]

Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In euismod ultrices facilisis. Vestibulum porta sapien adipiscing augue congue id pretium lectus molestie. Proin quis dictum nisl. Morbi id quam sapien, sed vestibulum sem. Duis elementum rutrum mauris sed convallis. Proin vestibulum magna mi. Aenean tristique hendrerit magna, [5] ac facilisis nulla hendrerit ut. Sed non tortor sodales quam auctor elementum. Donec hendrerit nunc eget elit pharetra pulvinar. Suspendisse id tempus tortor. Aenean luctus, elit commodo laoreet commodo, justo nisi consequat massa, sed vulputate quam urna quis eros. Donec vel.

I would like to sequentially replace all the [.] that are page numbers with, in TeX, \marginpar{[.]}, but ignore any numbers that are not in sequence.

So, I would like sed or emacs or another similar utility to match, for the example above, what I've made bold here:

[267] [1] [2] [1] [3] [24] [5]

Thus, I would like to match only what is in numeric order.

How could I do this?

  • 1
    To clarify, [24] comes in numerical order after [3], but the intent is not to match [24] but to match [5], which also comes after [3] and is also not immediately after [3]? What would be the tolerable difference for a match? If a difference of 2 is OK, is a difference of 3 also OK? – Simon Apr 14 '14 at 6:04
  • @Simon: Any difference ≥2 would have to be rejected, although it would be nice to be able to explicitly specify such a threshold, too. – Geremia Apr 14 '14 at 15:28
  • I'm thinking awk could do what I'm looking for. – Geremia Apr 14 '14 at 16:54
2

awk can certainly do this. For example, the script subpar.awk:

BEGIN   { 
    maxgap = ARGV[1]
    ARGV[1] = "-"
    count = 0 
    }

{   i = count + 1
    while (i <= count+maxgap) {
        n = sub("\\[" i "\\]","\\marginpar{[" i++ "]}")
        count += n
        }
    print
    }

... could process the example text as awk -f subpar.awk 2 <lorem.txt, where the parameter 2 is the maximum difference allowable in the sequence. sub() returns the number of substitutions made (either 0 or 1) so count is only incremented when a substitution has been made in the line. This code deals with the case where there is more than one substitution to be made on a line.

| improve this answer | |
  • The only thing is that I have to make maxgap very big for some reason… – Geremia Apr 15 '14 at 17:08
  • For the test case, maxgap=2 sufficed. The only reason to make it much larger would be if there was a much larger genuine gap in the sequence, which seems unlikely in the context. How big did you have to make it and what was the sequence of bracketed numbers in that case? – Simon Apr 15 '14 at 20:37
  • I ran exactly awk -f subpar.awk 2 <lorem.txt, but for some reason it only matched the first dozen or so and didn't match the rest. I had to increase it to about 23 before it matched everything, all the way to the end. – Geremia Apr 15 '14 at 20:57
  • I'd suggest inserting the following code in a line after while (i <= count+maxgap) {: print "Searching for [" i "]", which should tell you exactly what page number the script is searching for at any given moment. Then you can compare the text that it is searching for (but not finding) to the text that you expect it to find in the following paragraph and see if there are any differences. – Simon Apr 16 '14 at 1:08

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.