The title of the question says it all. What is your favorite sed one-liner when it comes to editing TeX files. I have seen at least one sed goodfella on this forum who is writing a book about a sed in TeX. Maybe he could volunteer some of his tricks?

I promise mine are coming soon (I love using sed for editing!).


3 Answers 3


I mostly use sed for searching & replacing within TeX files in whole directories. For example:

find "." -name "*.tex" | xargs sed -i -e "s/\(\\usepackage\){times}/\1{mathptmx}/g"

This changes all occurrences of \usepackage{times} into \usepackage{mathptmx}, to enable Times font both for text and for math.

  • find searches all .tex files
  • the pipe symbol | allows using the output of one command as input of another
  • xargs takes the list of file names, returned by find, and makes them into one line
  • sed applies the desired change for each file contained in that line. I use options
    • -i for in-place editing (you could specify a suffix for making backups)
    • -e for executing a script, here s/.../.../.. for search & replace
  • sed understands regular expression syntax, here I additionally used backslashes \ for quoting some characters with a special meaning for the shell, such as parentheses and the backslash itself
  • The backslash \ at the end of the first line is for breaking the line, for better displaying here. You don't need it if you type all into one command line.

For such frequently used command lines I usually create a shell function or a shell script, so I don't have to type much and just call my function/script with a regular expression as argument.


One of my previous answers on this site is a script that pulls out the pages with figures and compiles them into one document. The whole script is also in a Gist.

The script works by pulling a list of figures from the .aux file. This following lines pulls out only figures, and then just the unique number at the end (the page number in the document):

 grep figure |     sed 's/.*}}{\([1-9]*\)}}/\1/' | uniq |

This also shows an important point: There might be good ways to use sed, but the important thing is how the Unix tools work in concert. Caring only about sed in isolation is missing the usefulness of it.


I found a nice one on commandlinefu.com:

grep -R usepackage * | cut -d']' -f2 | cut -s -d'{' -f 2 | sed s/"}"/.sty"}"/g \
  | cut -d'}' -f1 | sort | uniq | xargs dpkg -S | cut -d':' -f1 | sort | uniq

It returns the Debian package names corresponding to LaTeX packages used in a document or folder, respectively. Could also combined with find and xargs for checking including subdirectories. This works of couse ony on a TeX installation from Debian or Ubuntu repositories.

This is an example where sed is just used for simple search & replace in a bigger and more general context, combined with command line tools such as sort, uniq and grep.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .