Dec 23, 2009

Format Numbers to be Comma Separated

I have a number 1234567890, and I want to insert a comma at the gap of three digits like this:1,234,567,890

If this is the problem running in your head then the following one liner will be helpful to you.

$ echo "1234567890" | sed -e ':a' -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
1,234,567,890

Dec 15, 2009

Convert VOB to AVI using ffmpeg

Suppose you have a myVideo.vob and you want to convert to avi format.
Open a terminal and execute the following command

$ ffmpeg -i myVideo.vob myvideo.avi

Possible errors:
1 "ffmpeg: symbol lookup error: ffmpeg: undefined symbol: avcodec_channel_layout_num_channels"
Solution:
$ export LD_LIBRARY_PATH=/usr/local/lib/ && ffmpeg -i myVideo.vob myvideo.avi

If this doesnt solve the problem then retry that after installing the libavcodec52 and libavutil49 packages:
$ sudo apt-get install libavcodec52  libavutil49

Sep 29, 2009

Bulk upload photos to facebook [BASH]

I got tired of uploading one photo at a time. So I googled a bit, and found few scripts but none did convince me. So I set to write one by myself. And my focus was to keep it short and simple(KISS).

The tool is a python script with following features:
* Bulk upload the pictures to facebook.
* Supports upload from specified folder or current folder (default).
* Allows to choose from available albums.

Usage:
upload2facebook [-a|--aid <album_id>] [<path>]

Install:
(Don't forget to read the notes below)
In Debian (Ubuntu, Kubuntu,...) system:
1. Install from ppa https://launchpad.net/~ssapkota/+archive/ppa
2. Install directly from deb-package -> upload2facebook_0.2_i386.deb , Package Detail

In RPM (Fedora, Centos) system
3. Install the rpm -> http://ssapkota.com.np/downloads/upload2facebook-0.2-2.i386.rpm

4. Get the source, do whatever you like -> upload2facebook_0.2.tar.gz


Note-1:
During the installation of this tool, it will ask you to enter the Application Key and Application Secret Key. To get this two key you need to register an application for yourself at facebook. Follow the following (necessary) steps:
1. Go to http://www.facebook.com/developers and click on Setup Up New Application at the top right corner.
2. Enter the Application Name (I suggest : Photo Uploader)
3. Then click Agree and then Save Changes
4. After this you will get
API Key and Secret. Copy it and save it somewhere; you'd need later. Remember, these keys are meant to be secret and shouldn't be shared (So, did I ;) ).
5. You need to do one more thing to make it work; On the left-navigation click on the Canvas and then enter a URL(may be your website/blog link) as Canvas Callback URL. I don't think this is of any use for command line application, but to make the application work you need to set this.
6. Thats it. Return to the installer and Enter your App Key and App Secret.

Note-2:
1. The tool is developed and tested in Ubuntu and works in all the Debian system.
2. The RPM package is generated using alien and have not been tested.
3. The code is written in python and is OS independent, One can easily port to any other Operation system.
4. If you have tested the tool in other system or have ported it to other OS, please post the status in the comment.

Enjoy the tool. :)

Jun 29, 2009

Verify IPV4 address in php using regex

A simple function in php to verify IPV4 Address. It is completely based in regex and does full ip verifying.
It tests the ip for;
1. Need 4 numeric blocks separated by a dot.
2. Each numeric block must noot exceed 255.
3. Shouldn't contain space. So remember to trim before calling this function.


function isINetAddress($ipaddr){
if( preg_match( "/^((?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9]).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$/m",$ipaddr) > 0)
return true;
}


This can't me more simpler. ;)
Enjoy!

May 24, 2009

Bluk rename files in Linux

Today one of my friend asked me to bulk rename all files in a folder. He had a collection of songs named randomly with no extension. He also wanted to add the extension, mp3 in all files(Yes, he was sure about the encoding)
I opened a terminal in that folder and executed the following command.
NUM=1
for SONG in ./*
do
mv $SONG song$NUM.mp3
let NUM=NUM+1
done

So lets peek what the above script does:
  1. Defined a variable NUM and initialized it with 1.
  2. Defined a foreach loop. Here for each files in current path the for loop is executed.
  3. Rename(move) the file $SONG to song<num>.mp3
  4. Increase the value of variable NUM by 1.
  5. end. ;)
So if you want to rename in a different fashion you can hack the line mv $SONG song$NUM.mp3 in any way you like. But Remember the second argument to the mv command must vary for each loop, otherwise you will end up deleting all files except the last one.

May 11, 2009

Solve no sound problem in Ubuntu

One after another 17 days have passed.. till today my laptop was completely silent when i booted into Ubuntu. But now, just 10 mins back, after a lot of hit and trial with driver model, I could make make my laptop speak. Here's what I did.
Though I specified this as ubuntu problem in title, it the problem of alsa; it's not recognizing your device.

I completely forgot what I did to mess up my laptop-sound-system, so i thought to start afresh.
I removed the alsa and pulseaudio completely. And One thing I would like to mention: I am completely unfamiliar about the interaction of alsa and pulseaudio with the sound system. This is my first deal with sound system of linux to this depth.
sudo apt-get --purge remove linux-sound-base alsa-base alsa-utils \
"pulseaudio-*"

Then I did a quick Reboot; just playing safe. (Jaunty boots on/off so fast, I am fan of it.)
Next I reinstalled the alsa with pulseaudio.
sudo apt-get install linux-sound-base alsa-base alsa-utils \
libasound2-plugins "pulseaudio-*" paman padevchooser \
paprefs pavucontrol pavumeter

Then, I appended following lines to the file /etc/modprobe.d/alsa-base.conf
alias snd-card-0 snd-hda-intel
alias sound-slot-0 snd-hda-intel
options snd-hda-intel model=dell-m6
options snd-hda-intel enable_msi=1

The bold faced "model=dell-m6" was the main hack for the system to work.

Then after another quick boot, I got the sound.

During this I refered a lot of pages, But the most useful and the one that is well documented is: Comprehensive Sound Problem Solutions Guide
And this is the page where I got the model of the sound-card(dell-m6) from.

Apr 30, 2009

Utilize Grep Sed and Awk

In this post I will list commands that I use and include at least one of grep,sed or awk.
  • cat filename | grep "phrase"
    Search line containing phrase in file
  • cat filename | grep -v "phrase"
    Search line not containing phrase in file
  • cat filename | grep "phrase1\|phrase2"
    Search lines containing phrase1 or phrase2 in file.
  • sed -i "s/phrase1/phrase2/g" ./filename
    Replace phrase1 with phrase2 in file.
  • sed -i "s/[ ]*\(.*\)[ ]*/\1/g" ./filename
    Trim spaces of each line of the file.
  • awk '{if($7=="2") $7="5"; print;}' file
    Conditional modification. Field no.7 is is replaced by "5" if it is "2". The default field seperator is .
  • uptime | awk 'BEGIN {FS=" "} { gsub(",",""); if (index($0,"day")) {gsub(":"," hours, ",$5);print $3" "$4", "$5" minutes"} else {gsub(":"," hours, ",$3); print $3" minutes"}}'
    Get the Uptime in a proper and clear format.
  • ls | grep -v 'file or folder or regex' | xargs -I{} mv {} /target/folder/
    Move all but one to /target/folder. You can also use it to move all the files/directories in current directory to another directory in the same path.Be Aware: The Target directory should exist. Just to be at the safe side, I suggest not to miss the slash (/) at the end. This will give you warning/error if you messed up something.


For now I have just listed few that came in my mind; just thought to prepare a seed.
I'll promise to grow this list as soon as they come to my mind.

Note: If you have some commands in your mind, that you regularly use, plz post it as comment.

Mar 24, 2009

make scite, a default editor in gnome

Personally, I prefer scite over gedit (The one that is included by default in gnome). So soon after every system upgrade (fresh installation), I simply remove gedit and install scite:
sudo sh -c "apt-get -y remove gedit && apt-get -y install scite"

Now to make it a default text editor run:
sudo update-alternatives --install /usr/bin/gnome-text-editor \
gnome-text-editor /usr/share/scite 1

Feb 17, 2009

Terminal tool for websms (NTC service)

Websms is a service provided by NTC.
It is service that allows us to send 10 SMS per day via its web-interface.

Each time when I need to send a SMS, I had to open the site,login(no auto remember facility) and send SMS. So I thought to create a tool that would save time.

You can download the debian package from here: websms_1.01_all.deb

Install it using:
dpkg -i ./path/to/websms_1.01_all.deb

Synopse:
websms help
This displays the help for this tool.

websms addcontact <nick> <Mobile Number>
You can store the mobile number with (nick)name in the contacts list. Later you can use the (nick)name to send SMS to him/her.

websms viewcontacts
You can view the complete list of your contacts.

websms send <nick | Mobile Number> <Message>
Send the SMS by Mobile Number or by (nick)name that you stored earlier.The message length cannot exceed above 142.

Examples:

  • $ websms addcontact sweety 98********

  • $ websms send sweety "I will be late."

  • $ websms send 98******** "Call me back. --suraj"

  • $ cat joke | websms send 98********

    Be sure that the size of file "joke" doesnt exceed 142 bytes.

  • $ websms viewcontacts



Enjoy!

Note:The secure-info and the contact-list are located in ~/.websms directory.

EDIT:
On the request of you Guys I have added the RPM package too:

Direct Download Link:websms-1.01-2.noarch.rpm
[Note:The RPM package was generated with alien.]

Jan 5, 2009

Search with single click

Searching cant be any more easy than this.Just select and click.
Here is how you can search anything you select with just a single click.
First install this package "googlizer"
apt-get install googlizer

Just drag it to the panel(from Applications->Internet) for easy access and enjoy.
Select any text anywhere and just click on it.

You can even use it with other links.. May be answers.com or wikipedia.org. Here's, how you do it.

Right click on the googlizer button on the panel and open its properties. And in the command text area type the following to convert it to answerizer ;)
googlizer -u "http://www.answers.com/topic/"

If you want to look it better click on the googlizer Icon at the top left of the properties dialog and change it as you want.

The core thing that the googlizer does is just copies the text in X'selection and appends it to the end of the link that you provide, and finally opens it in Browser. If you do not provide any link then it uses "http://www.google.com/search?q=".


For Wikipedia-izer, whatsoever,
googlizer -u "http://en.wikipedia.org/wiki/"


*Please leave in comment if you use it for any other.