Showing posts with label pipeline. Show all posts
Showing posts with label pipeline. Show all posts

Thursday, June 10, 2010

Decrypting mojibake in Linux

Today I received an e-mail, which contained something completely unreadable.

=D0=9D=D0=B5 =D0=BF=D1=8B=D1=82=D0=B0=D0=B9=D1=82=D0=B5=D1=81=D1=8C =D0=BE=
=D1=82=D0=B2=D0=B5=D1=87=D0=B0=D1=82=D1=8C =D0=BD=D0=B0 =D1=8D=D1=82=D0=BE =
=D0=BF=D0=B8=D1=81=D1=8C=D0=BC=D0=BE! =D0=9E=D1=81=D1=82=D0=B0=D0=B2=D0=BB=
=D1=8F=D0=B9=D1=82=D0=B5 =D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=
=D1=80=D0=B8=D0=B8 =D0=BD=D0=B0 =D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B8=D1=86=
=D0=B5 =D1=8D=D1=82=D0=BE=D0=B9 =D0=B7=D0=B0=D1=8F=D0=B2=D0=BA=D0=B8

When using Windows, I get used to use the "Stirlitz" utility (ru.wikipedia.org) to automatically decrypt mojibake (or "krakozyabry" as we call the stuff in Russian).

As far as I know, all these equal signs, "A"s, "B"s, "C"s, etc., are used in the so called "Quoted-printable" encoding.

At my OpenSUSE 11.2 I found a small utility called recode that is well suitable to solve the task. I saved an e-mail as 66.eml and pipelined it to recode. The latter successfully decoded QP into UTF-8:

$> cat 66.eml | recode /qp

Saturday, May 1, 2010

Computations in linux command line

The bc command is useful to perform some simple arithemetical calculations in console. It may be used in scripts too.

For example, percent values from my previous post have been obtained as

$> echo '265594/292891' | bc -l
$> echo '262938/292891' | bc -l

Argument of echo contains an arithmetic express, and echo pipes it to the bc with standard math library attached (-l).

Do you want to know the Euler's number e (the base of the natural logarithm)?

$> echo 'e(1)' | bc -l

Let's print first 80 digits of π (pi) using arctangent function (a(1.0)):

$> echo 'scale=80; 4.0*a(1.0)' | bc -l

The bc has interactive mode as well; just type

$> bc

In order to quit from interactive mode, type quit or exit.

Optimizing PNG with optipng and pngcrush

There several tools to perform lossless optimization of images in PNG format.
My favourite GNU/Linux distro repositories contain two ones: optipng and pngcrush.

Pngcrush is invoked as follows:

$> pngcrush -e .opt.png -brute *.png

The command tries to reduce size of all PNG files from current directory (*.png) using brute-force search (-brute). Of course, you may omit the -brute key and use default heuristics (which is much faster and produce comparable compression). Resulting files will have .opt.png suffix (-e .opt.png), i.e. vpn-nm-7.png becomes vpn-nm-7.opt.png.

In order to get more information on pngcrush command line keys type:

$> pngcrush -h | less

Another program, optipng, is a bit more friendly, for my taste.
Its syntax is quite similar:

$> optipng -keep -o7 *.png

The command optimizes all PNG images from current directory (*.png) using exhastive search (-o7) and keeps backups (-keep).

As usual, in order to get more information on optipng type:

$> man optipng 

Finally, I have written down compression ratios of pngcrush and optipng on my eight small test PNG files obtained from KSnapshot.

pngcrush - 265594/292891 bytes - 90.7%
optipng  - 262938/292891 bytes - 89.8%

Thursday, April 22, 2010

Sending an e-mail with attachment using mutt command line interface

Sending an e-mail message with an attachment file using command line interface is a useful operation. It is a convenient way, say, to backup some small files.

$> echo "Hello! This is a backup." | mutt -s "backup" -a '/tmp/backup.tgz' -- 'mail@example.com'

This command sends an e-mail message with a body specified as an argument of echo command ("Hello! This is a backup.").

The subject of an e-mail is given with -s argument (-s "backup").

Attachment is specified with -a option (-a '/tmp/backup.tgz').

Be sure to specify two dashes (--) before an e-mail address ('mail@example.com'), otherwise you get "No recipients specified." message on some systems.

Monday, April 19, 2010

Print desired number of hexdump lines with xxd

$> xxd -l 0xN0 FILE
The command prints the first N hexdump lines (-l 0xN0) of the file (FILE).
The stuff works because each line of xxd output consists of 16=0x10 octets by default, so 0xN0 is the number of octets in the first N lines.

The other useful way to view binary file is the following: 
$> xxd FILE | less 

Sunday, November 8, 2009

How to fix some sort of mp3-files

#!/bin/bash

# 
# I had some mp3-files that were repeated twice inside themselfes.
# It looked like someone copied them one day with append command.
# The script splits such files into to equal parts and checks if
# those parts are coinside.
#

for f in *.mp3 ; do
    # Calculate size of the file
    size=`du -b "${f}" | cut -f 1`
    half_size=`echo ${size}/2 | bc`

    # Split file into two parts
    f1="$f".1
    f2="$f".2
    head -c $half_size "$f" > "${f1}"
    tail -c $half_size "$f" > "${f2}"

    # Calculate checksums and check if they coinside
    md1=`md5sum "${f1}" | cut -f 1 -d " "`
    md2=`md5sum "${f2}" | cut -f 1 -d " "`
    if [ ${md1} != ${md2} ] ; then
        echo "Problems with $f"
    fi
done