1

For the purpose of building an index I am searching for some words in a bunch of latex files. This process is complicated by the fact that latex has a discretionary hyphen command \-, which indicates to latex at which places it can break a word. I want to include it into my search, but so far I have not succeeded in doing so. For example I would need an expression that would match all of

  • pādapracāra
  • pāda\-pracāra
  • pā\-da\-pra\-cā\-ra

or differently hyphenated instances of the same word.

I understand that to match the backslash beginning a latex command one has to type four backslashes, such as $ grep \\\\mycommand *tex`.

In vim I can search for such an expression via /pā\(\\-\)*da\(\\-\)*pra\(\\-\)*cāra, so I thought in grep it would be something like grep pā\(\\\\-\)\?da *tex (and so on, but already this one didn't match anything).

1

I don't think you can instruct 'grep' to disregard the hyphenation.

How about filtering it first through 'sed' with something like :

echo 'pa\\-da\\-pra\\-ca\\-bra' | sed -e s'/\\-//g'

And you can do the pattern matching while at it with another expression (as suggested by user cas):

echo 'pa\\-da\\-pra\\-ca\\-bra' | sed -n -e s'/\\-//g' -e '/pada/p'
| improve this answer | |
  • That would in practice then mean to first loop through all my files with sed removing the hyphenation, saving those to temporary files, and then grepping these temporary files for my words? Interesting idea. I think though that grep can handle optional things by \? but I haven't found the correct syntax yet. I'll edit my question to make it clearer. – muk.li Sep 27 '15 at 9:50
  • I sincerely doubt that \? could help you, unless you want to include the possible hyphenation in the search patterns already, e.g. echo 'pa\\-da\\-pra\\-ca\\-bra' | grep -E 'pa(\\-)?da' – Tommi Kyntola Sep 27 '15 at 9:55
  • Yes, I was going to include the optional hyphenation commands in the word. In vim this works, but with grep I haven't gotten it to work yet. See my edit above. I still don't quite understand the role of echo in your answer. – muk.li Sep 27 '15 at 10:05
  • 1
    no need for temp files, sed can do it all: sed -r -n -e 's/\\-//g ; /pādapracāra|word2|word3/p – cas Sep 27 '15 at 10:05
  • 2
    sed can filter out the optional hyphenation from the input and then proceed to match just like grep. That way you can search for words without first having to insert the optional hyphenation into the search pattern, which can be both cumbersome and error-prone. – Tommi Kyntola Sep 27 '15 at 10:28
0

$ grep -Ei 'pā(\\-)?da(\\-)?pra(\\-)?cā(\\-)?ra' *tex does what I need.

| improve this answer | |
0

It is not right way to edit laTeX' docs by bash but hereis if you'd like

grep -Ee "$(echo "padapracara" | sed "s/\B/(\\\\\\\\-)?/g")"
| 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.