Using mirrors in SED

It took me long to discover mirrors in SED. That’s a feature that may help you a lot in some replacing problems. Mirrors are denoted by \1, \2, \3, .. \9 and are used to cut a part of the input string to use it in the replacement string, like a temporary variable. For use mirrors in SED, you need to use extended regexp, flag -r. Let’s see some applications:

Chaging a data:

$ echo 12/31/2004 | sed -r 's@([0-9][0-9])/([0-9]{2})/([0-9]{4})@\1.\2.\3@'
$ echo 12/31/2004 | sed -r 's@([0-9][0-9])/([0-9]{2})/([0-9]{4})@\2.\1.\3@'
$ echo 12/31/2004 | sed -r 's@([0-9][0-9])/([0-9]{2})/([0-9]{4})@\2-\1-\3@'

Inverting field order:

$ echo abcdefg | sed -r 's/(a)(b)(c)/\3:\2:\1/'
$ echo abcdefg | sed -r 's/(a)(b)(c).*/\1:\2:\3/'
$ echo abcdefg | sed -r 's/(a)(b)(c).*/\3:\2:\1/'

Read more:
http://aurelio.net/curso/sucesu/sucesu-seder-prompt.html

See ya!

How to generate a bash script with an embeeded tar.gz (self-extract)

Consider that you need to perform a routine in a remote server, where you need to decompress a tar.tz and execute a list of commands on this data. One alternative is send the tar.gz file to the remote server throught a ftp or scp and then log in the remote server and run a shell script or run manually a list of commands. Recall Java JRE setup, they use script.bin that comes with an embeeded tar.gz, which is self-extracted in the beginning of script execution. To build the self-extraction script I follow a tutorial published by Stuart Wells, which consists in four steps:

1) Create/identify a tar.gz file that you wish to become self extracting.

2) Create the self extracting script. A sample script is shown below:

> cat extract.sh
#!/bin/bash
echo "Extracting file into `pwd`"
# searches for the line number where finish the script and start the tar.gz
SKIP=`awk '/^__TARFILE_FOLLOWS__/ { print NR + 1; exit 0; }' $0`
#remember our file name
THIS=`pwd`/$0
# take the tarfile and pipe it into tar
tail -n +$SKIP $THIS | tar -xz
# Any script here will happen after the tar file extract.
echo "Finished"
exit 0
# NOTE: Don't place any newline characters after the last line below.
__TARFILE_FOLLOWS__

3) Concatenate The script and the tar file together.

> cat extract.sh example.tar.gz > example.sh
> chmod +x example.sh

4) Now test in another directory.

> cp example.sh /tmp
> cd /tmp
> ./example.sh

See ya!

Creating AVI Video from mp3 File

If you want to upload a mp3 file to youtube, you will need to convert it into video format. So this Shell script will be useful. You can get the script bellow and also in Dzone.


#!/bin/bash
FFMPEG=`which ffmpeg`
FPS=1                                                                                   # for a youtube video from a mp3, it is enough</code>

if [ "$FFMPEG" = "" ] ; then
echo "Please install ffmpeg.";
exit 0;
fi
if [ $# != 2 ] ; then
echo "Usage: $0  ";
exit 0;
fi
if [ ! -f $1 ] ; then
echo "Source mp3 '$2' not found.";
exit 0;
fi
if [ -f $2 ] ; then
echo "Output file '$2' exists.  Overwrite? (y/n)";
read CONFIRM
if [ "$CONFIRM" == "y" ] ; then
echo "Overwriting '$2'"
else
if [ "$CONFIRM" == "Y" ] ; then
echo "Overwriting '$2'"
else
echo "Operation canceled.";
exit 0;
fi
fi
fi

TITLE=`$FFMPEG -i $1 2>&1 | grep TIT2 | cut -d: -f 2 | tr -d "'"`
IMAGE=/tmp/$RANDOM.gif
convert -size 1024x240 xc:black -fill white -draw "gravity Center text 0,0 '$TITLE'" $IMAGE
TIME=`$FFMPEG -i $1 2>&1 | grep Duration | cut -f1 -d, | cut -f2,3,4,5 -d:`
$FFMPEG -r $FPS -loop_input -i $IMAGE -i $1 -acodec copy -y -t $TIME $2
rm $IMAGE

wget output conflicting options

I was trying to download some image with wget and set both a name and directory for its output. Reading the wget man page, it shows different options to deal with each one of the situations, -O to set the downloaded file’s name and -P to set its directory.

Anyway, if you try to use both one there is a kind of overwriting options. Let’s try the following example to realize this unexpected behavior, at least in my opinion:

I want to download the Google’s logo with a customized name “downloaded.gif” and also want that the output file to be take to /tmp directory. So, the following command should do the work:

wget http://www.google.com.br/intl/en/images/logo.gif -P /tmp -O downloaded.gif

What we have here is, wget (google logo) -P (the directory where I want the downloaded logo output) -O (the name of the output file). But the resulting file “downloaded.gif” goes to ‘.‘ directory. It seems that the default output directory did not change even when using the -P parameter. The parameters order does not matter, i.e., the following command does the same unwanted thing:

wget http://www.google.com.br/intl/en/images/logo.gif -O downloaded.gif -P /tmp

To solve this problem I did forget the -P parameter, and used just the -O writing together both the output directory and file name. So, the following command solved my problem:

wget http://www.google.com.br/intl/en/images/logo.gif -O /tmp/downloaded.gif

How can I show only my current directory in bash

In projects with deep directory hierarchy the abosolute path became noisier, especially if you’re using many terminals side by side. To change it you can edit your .bashrc to set it to show only the current directory.

sed -i "s/\\\w\\\/\\\W\\\/g" ~/.bashrc

See ya