Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bunch of tex files in different folders on my system. The goal is to create new tex files (using a template, that has the title page, packages, new commands,...) that combines all the definitions/theorems of the other tex files at a specific place in this template file.

I'm pretty new to linux, so I was wondering which command/set of commands would be the best to use here. So far I used:

$ sed -n '/begin{definition}/,/end{definition}/p' <infile >outfile

which succeeds in extracting the definitions from one file into another.

Someone any suggestions?

share|improve this question
up vote 3 down vote accepted

If I understand you correctly, you managed to extract the definitions but can't get them inside the template. You can mark the place inside the template where you want to add the definitions with a specific pattern and then use sed or perl to substitute your definitions in place of that pattern.

An even easier way would be to split the template file into 2 files: a file containing everything that must come before the definitions and another for what comes after the definitions and concatenate all 3 files using cat:

cat pre_defs.tex defs.tex post_defs.tex > whole_file.tex
share|improve this answer
Even do something like def_extractor this.tex foo.tex bar.tex | cat pre_defs.tex - post_defs.tex > final.tex, where the def_extractor is something like the sed oneliner above. Oh, the power of Unix. – vonbrand Mar 1 '13 at 18:40
Indeed. Gotta love pipes! :) (and the useful use of cat) – Joseph R. Mar 1 '13 at 22:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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