Shell

How to pad string with zeroes & right-justify in bash using sed

Pad any string using this sed command:

sed -e :a -e 's/^.\{1,3\}$/0&/;ta'

This pads every string to length 4, usage example:

$ echo x | sed -e :a -e 's/^.\{1,3\}$/0&/;ta'
000x

In case you want to pad to a different length, replace 3 in the script by (your desired length - 1).

You can also use a bash function like this:

# Zero pad to length 4, right-justified
function zero_pad4 { echo $1 | sed -e :a -e 's/^.\{1,3\}$/0&/;ta' ; }

Usage example:

$ zero_pad4 1x
001x

Original source, modified: The Unix School

 

Posted by Uli Köhler in Shell

How to convert a DVI file to SVG on the command line

If you want to convert my.dvi to my.svg, use this command

dvi2ps my.dvi | ps2eps - > my.eps && eps2svg my.eps

This produces my.svg – note that if my.svg already exists, eps2svg will create my_1.svg, my_2.svg and so on and will not overwrite my.svg!

You can also use this shell function:

function dviToSVG { dvi2ps "$1" | ps2eps - > "${1%.*}.eps" && eps2svg "${1%.*}.eps" "${1%.*}.svg" ; }

Usage example:

dviToSVG my.dvi # Produces my.svg

 

Posted by Uli Köhler in LaTeX, Linux, Shell

How to recompress gzipped .gz files to .xz files on Linux

Copy and paste this shell function into your bash or zsh:

function gzToXz { zcat "$1" | xz -c - -ev9 > "${1%.*}.xz" ; }

This will decompress the file using zcat and pipe it into xz with the highest compression setting (-ev9). If you prefer less compression but more speed, use e.g. -5 instead of -ev9.

Use like this:

gzToXz my.gz

which will produce my.xz.

Posted by Uli Köhler in Linux, Shell

git svn: Clone latest revision only

Problem:

You want to use git-svn to clone a SVN repository, but you don’t want to clone the entire history (which can be quite slow) but only the latest revision.

Continue reading →

Posted by Uli Köhler in git, Shell, Subversion, Version management

Shell: Strip directory from path

Problem:

In the Linux shell, you have  a file path and you want to strip everything but the filename, for example  you have the path ../images/photo.jpg  and want only photo.jpg

Continue reading →

Posted by Uli Köhler in Shell