0

I'm preparing a document in markdown for print and as an EPUB file. I want to remove footnotes for word definitions for the EPUB file.

The foonotes I want to remove are in the format [mean-X] where X can be any set of alphanumeric characters (including another -).

I've tried:

grep -o "\[\^mean-.*\]"

Sample results:

[^mean-vicissitudes]
[^mean-immortality]
[^mean-ingot]
[^mean-remonstrance]
[^mean-libeller]
[^mean-debauched] and the most amorous[^mean-amorous]
[^mean-turpitude]
[^mean-debauched] drunken, licentious,[^mean-licentious]
[^mean-frivolous]
[^mean-despots]
[^mean-repentance]
[^mean-ignominy]
[^mean-vain]
[^mean-presumptuousness] haughtiness,[^mean-haughtiness]

The problem occurs when two or more footnotes appear on the same line, the regex extends to the last occurrence of a ].

I've also tried:

  1. grep -o "\[\^mean-.*\][^\[]" which still had some occurrences like the original along with capturing commas and colons after the last ].

  2. grep -o "\[\{1\}\^mean-.*\]" but it doesn't solve the problem. I thought it would capture only one instance of a [.

  3. grep -o "\[\^mean-.*\]\{1\}" but it doesn't solve the problem either. I thought it would capture only one instance of a ].

How can I capture the footnotes using grep and remove them (not replace them with space, but remove them)?

P.S. I'm using Git Bash for Windows but I assume the commands will be the same.

  • 1
    You may also want to take look at pandoc -t epub -f markdown -o out.epub – JJoao Jul 20 at 9:17
  • \{n\} (or {n} in ERE) means n instances of the previous item in a row, e.g. a\{4\} would match aaaa. \{1\} is not really useful since all the atoms match once by default. \{n\} with the opening bracket doesn't affect the later .* – ilkkachu Jul 20 at 10:57
  • 1
    @JJoao, I'm using Pandoc and have created a makefile to cater for PDF and EPUB. It's just that most ereaders have built-in dictionaries, so I wanted to remove footnotes that give word definitions. – Khalid Hussain Jul 20 at 14:01
  • @ Khalid, yes. It makes sense. – JJoao Jul 20 at 16:43
2

The issue is the .* in you expression, which is "greedy". This makes your expression match the ] and the rest of the line up to the final ] on the line. Instead, make this part of the expression match only what you yourself said it may match, i.e. any alphanumeric and -. An alphanumeric is matched with [[:alnum:]], and [[:alnum:]-] allows the bracketed expression to match a dash as well:

\[\^mean-[[:alnum:]-]*\]

Running this on the data in your question:

$ grep -o '\[\^mean-[[:alnum:]-]*\]' file
[^mean-vicissitudes]
[^mean-immortality]
[^mean-ingot]
[^mean-remonstrance]
[^mean-libeller]
[^mean-debauched]
[^mean-amorous]
[^mean-turpitude]
[^mean-debauched]
[^mean-licentious]
[^mean-frivolous]
[^mean-despots]
[^mean-repentance]
[^mean-ignominy]
[^mean-vain]
[^mean-presumptuousness]
[^mean-haughtiness]

Your grep command will obviously extract the matches, but won't remove them. To do that, use a sed editing script:

$ sed 's/\[\^mean-[[:alnum:]-]*\]//g' file





 and the most amorous

 drunken, licentious,





 haughtiness,

Add to this some removal of empty lines and of initial whitespace,

$ sed 's/\[\^mean-[[:alnum:]-]*\]//g; /^[[:blank:]]*$/d; s/^[[:blank:]]*//' file
and the most amorous
drunken, licentious,
haughtiness,

Or, to only modify the lines that had a [^mean-...] on them,

sed '/\[\^mean-[[:alnum:]-]*\]/{ s///g; /^[[:blank:]]*$/d; s/^[[:blank:]]*//; }' file
  • or just grab everything but the closing ] with \[[^]]*\]? – ilkkachu Jul 20 at 9:20
  • 1
    @ilkkachu That's another option, but I used what I used because the user was explicit about what was allowed after the mean-. Also, we don't know if there is other markup within brackets that should not be removed. – Kusalananda Jul 20 at 10:19
  • an intermediate \[\^mean-[^]]*\] could be useful... – JJoao Jul 20 at 16:41

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.