Mykhailo Makukha's blog

Just another WordPress.com weblog

Replacing substrings in file names and contents in Unix/Linux shell script

leave a comment »

Today I’d like to share two useful code snippets to deal with string replacement in file names and contents. Each of those two operations can be done in a single pipe, without intermediate files and variables and thus fast and without side effects.

Suppose you want to replace some $STRING_ONE with $STRING_TWO in files and folders’ names (rename them) and in all files’ contents for some given directory $BASEDIR.

cd $BASEDIR

To rename all files and folders by replacing $STRING_ONE to $STRING_TWO:

find . -name "*$STRING_ONE*" -print \
 | sed "h; s/$STRING_ONE\([^/]*\)$/$STRING_TWO\1/; H; g" \
 | sed -n 'N; 1! G; $ p; h' | xargs -L 2 mv

To replace string $STRING_ONE with $STRING_TWO in all files:

grep -lr "$STRING_ONE" . \
 | xargs sed -i '' -e "s/$STRING_ONE/$STRING_TWO/g"

The second operation uses grep to select files that contain string to be replaced before actual replacement. This makes renaming more effective when there are a lot of files in the directory and only some of them contain $STRING_ONE, but there are a lot of such substrings to be replaced.

Code snippets were tested on Mac OS X 10.6.5.

Written by makukha

09.12.2010 at 00:03

Posted in Shell

Typical .hgignore file for LaTeX project

leave a comment »

When working on TeX documents, version control can help very much. I use Mercurial.

When compiling a final document, typesetting engine (e.g. pdflatex) produces a lot of intermediate files, which should not be version controlled. One way to exclude such files from version control is Mercurial’s .hgignore file that should be placed in repository root.

Here’s such an example file that tells hg to ignore TeX intermediate stuff:

syntax: glob

*.aux
*.bbl
*.blg
*.brf
*.log
*.out
*.synctex.gz
*.thm
*.toc

Written by makukha

10.11.2010 at 12:23

Posted in Typesetting

Tagged with ,

Customizing LaTeX.tmbundle

with 2 comments

TextMate is a great Mac OS X plain text editor with many features and supported coding languages. LaTeX support (syntax highlighting, commands, code snippets, etc.) in TextMate is provided by the LaTeX Bundle, that is contained in any TextMate installation by default.

LaTeX bundle defines a default shortcut Command+R to show a typesetting status dialog window with some buttons for additional actions – compiling bibliography, building index, previewing, etc. I wanted to have an additional action button to run MetaPost on the file with .mp extension:

latextmbundle

To get the desired functionality, LaTeX bundle should be checked out from TextMate SVN and patched. Save the patch Latex.tmbundle.diff (file contents is listed below) to your Desktop and run the following commands in the Terminal:

$ cd ~/Desktop
$ svn co http://svn.textmate.org/trunk/Bundles/Latex.tmbundle
$ patch -p1 -d Latex.tmbundle < Latex.tmbundle.diff

To install updated LaTeX bundle into TextMate, simply double-click patched Latex.tmbundle, press “Update” in the dialog window. “Bundle Editor” TextMate window will be displayed, you may just close it.

That’s all. Your modified Latex.tmbundle was moved to ~/Library/Application Support/TextMate/Pristine Copy/Bundles and installed.

Latex.tmbundle.diff

Read the rest of this entry »

Written by makukha

30.10.2009 at 10:50

Коды УДК

leave a comment »

Список некоторых кодов универсальной десятичной классификации.

  1. Классификатор УДК на русском языке: http://teacode.com/online/udc.
  2. Сервис ВИНИТИ для расшифровки формул УДК: http://scs.viniti.ru/udc.

Список кодов

Read the rest of this entry »

Written by makukha

03.10.2009 at 23:42

Posted in Science

Tagged with

Follow

Get every new post delivered to your Inbox.