Wednesday, December 22, 2010

use variable refeences in awk

I need to compare 1st column of user.csv with value, it works well with following

value=2
awk -F':' '$1==$value {print}' user.csv

now I have a case where column number is also a variable for me, in that case, the solution would be:

value=2
column=1
awk -F':' ''\$$column==$value' {print}' filename

1. You need to put backslash (\) before first $
2. You need to put comparison in single quote

Monday, September 27, 2010

Retrieve web pages using perl

Here is the sample script to fetch web pages using perl.

#!/usr/local/bin/perl -w
use strict;
use LWP 5.64;
use URI;

my $browser = LWP::UserAgent->new;
$browser->cookie_jar({});

my @browserHeaders = (
'User-Agent' => 'Mozilla/4.0; (compatible; MSIE 6.0; Windows NT 5.1; en-US)',
'Accept-Language' => 'en-US',
);

my $response;
$response = $browser->get("http://vishnuagrawal.blogspot.com",@browserHeaders);

if ($response->is_success) {
print $response->content;
} else {
print $response->error_as_HTML, "\n";
print $response->status_line, "\n";
}

print $response->code(), "\n";
print $response->message() , "\n";
print $response->header('content-type'), "\n";

my $url = URI->new("http://www.softwareqa.com");
$response = $browser->get($url,@browserHeaders);

print $response->content;

Friday, September 24, 2010

Find the day diff between two days

Following is the shell script solution for getting number of days between two dates:

#/bin/sh
date1=`date +%s -d $1`
date2=`date +%s -d $2`
diff=`expr $date2 - $date1`
diff_days=`expr $diff / \( 60 '*' 60 '*' 24 \)`
echo $diff_days


[vishnu@ shell]# ./daydiff.sh 2010-08-02 2010-09-15
44
[vishnu@ shell]#

Here is the perl solution for the same:

#!/bin/perl
use strict;
use warnings;
use Time::Local;

my $date1 = $ARGV[0];
my $date2 = $ARGV[1];

my @formatted_date1 = split(/-/, $date1);
my @formatted_date2 = split(/-/, $date2);

my $local_date1 = timelocal(0, 0, 0, $formatted_date1[2], $formatted_date1[1], $formatted_date1[0]);
my $local_date2 = timelocal(0, 0, 0, $formatted_date2[2], $formatted_date2[1], $formatted_date2[0]);

my $diffSeconds = $local_date2- $local_date1;
my $diffDays = $diffSeconds / (60 * 60 * 24);
print "Day diff :: $diffDays\n";

[vishnu@ shell]# ./daydiff.pl 2010-08-02 2010-09-15
Day diff :: 43
[vishnu@ shell]#

Here is modified version of above shell script, if you want to find out time difference in seconds

#!/bin/bash

d1=`date +%s -d "$1"`
d2=`date +%s -d "$2"`
((diff_sec=d2-d1))
echo "Diff Seconds : $diff_sec"

[vishnu@ shell]#./date.sh "2011-07-19 22:44:34" "2011-07-20 02:04:14"
Diff Seconds : 11980
[vishnu@ shell]#

Wednesday, September 8, 2010

Dump cpu and memory usage of a process

Here is the automated shell script which can dump the memory and cpu usage of a process. Helpful in cases when you want to monitor a process for a long time.

#!/bin/bash

if [ "$#" -ne 1 ]; then
echo "Usage ... [$0 <pid>]"
exit
fi

pid=$1

# maxRunTime is used to control the script, so that it run only for specified number of seconds
maxRunTime=72000
# sleepTime is used to sleep for specified seconds, to take the memory dump again
sleepTime=120

logFile=${pid}_process.log
commandFile=${pid}_command.log

`ptree $pid >> $commandFile`;
`pargs $pid >> $commandFile`;

currentRunTime=0

while [ $currentRunTime -lt $maxRunTime ]
do
time=`date +%F::%X | tr -d '[\n]'`
line=`ps -eo pid,pcpu,vsz | grep $pid`;
echo "$time $line" >> $logFile
sleep $sleepTime
currentRunTime=`expr $currentRunTime + $sleepTime`
echo "Execution Time : ${currentRunTime} seconds [max ${maxRunTime}]"
done;

Wednesday, August 4, 2010

Flash Player debug version on New Google Chrome

If you install the latest version of Google Chrome (mine is 5.0.375.125), flash player debug version do not work in it; though the same debug player is working fine in Firefox3. Reason is that Chrome now comes pre-installed with the Flash player which get used by default, despite whatever other version you might have installed. It always use its original version. To change this, follow below steps:

- Open Google Chrome
- Type about:plugins in the url, - It will show the list of all the plugins
- Click on the "Disable" link of "Shockwave Flash" plugin

It will not disable flash, but just the built in version. Now the chrome will use the system-wide flash player version (probably the debug version).

Wednesday, February 24, 2010

Unix: Get a file's name, extension and directory name

file="/usr/local/bin/test.sh"

# get extension; everything after last '.'
ext=${file##*.}
# will return 'sh'

# everything after last '/'
basename=${file##*/}
# will return 'test.sh', similar to 'basename'

# everything before last '/'
basename=${file%/*}
# will return '/usr/local/bin', similar to 'dirname'

# back one directory
basename=${file%/*/*}
# will return '/usr/local'

Saturday, January 16, 2010

Share files between local and remote computer using Remote Desktop Connection

One of the most common tasks you'll need to do when using an Remote Desktop Connection is transfer a file from your local machine to the remote machine that you're logged into. During a Remote Desktop session, you can gain easy access to your local disk drives on the remote computer so that you can transfer files between these two systems in the same way that you copy files from a network share.



1. Click Start ->All Programs->Accessories->Remote Desktop Connection.
2. Click Options, and then click the Local Resources tab.
3. Click Disk Drives and then click Connect.
4. When you get logged into the remote box, open My Computer and you'll see that all the local drives will show up as mapped disk drives.
5. Now you can transfer your file as you do in local system.

Thursday, January 14, 2010

Remove a file whose name begins with "-"

Due to a bug in our product the log file was being created with the name "-server.log" (instead of "host-server.log"). Once the issue was fixed, i was trying to remove the file. Tried single quote/double quote/escape, but nothing worked. Finally after googling, found solution for it. Since the file name begins with the "-", all the unix commands treats the file name itself as a parameter to the command. To make it work put -- or ./ before the file name.

example:
rm -- -server.log
rm ./-server.log

Thursday, January 7, 2010

Paragliding in Manali

Last Christmas I went on vacation in Manali (Himachal) with my friends. One of the adventure we planned for the trip was Paragliding and we all were excited about it. By googling, we found out that Paragliding could be done in Solang Valley of Manali.

After reaching Manali, we enquired about the paragliding packages in Solang Valley. Most of the tour agents offered us 3 packages Short, Fly, Mediaum Fly and High fly. While roaming into Manali market, we noticed a tour agency which was offering paragliding in comaprtive cheaper rates. We talked to sales guy of that agency and he told us that they do offer pragliding in Naggar Village (which is 20 kms away from Manali). After having talk with him, we found it to be excited (though we were not sure, how would it be as we hven't heard of it before but we thought of giving it a try). Finally we gave him advance payment. He asked us to reach a particular destination (Heritage Hotel) which was 16 km from the Manali, from where their guys were supposed to pick us.

After half an hour we reached the Heritage hotel where one guy was waiting for us. We parked our car there and we sit in the Jeep which that guy was driving. REAL ADVENTURE starts from here.

Next 4 km was a mountain road with blind curves and had a road space only for a single Car/jeep. That guy started the Jeep and he was driving on that ride like he is running a ferrari on that road . We asked him that Jeep has Brake, you can apply them, he answered that if he would apply break then how would the jeep drive -:). Moreover, after half of our drive, we knew that the jeep do not have power steering. The experince in this 4 km drive was hilarious and full of adventure :). Finally we reached a point from where we have to trek to a top of mountain from where our glides were suppose to take off. Ohhh My GOD, that was again a surprise for us... we had to treck for around 2 kms. to reach at peak of the hill. No problem as we all like the trekking .. we started treking and reached on top in around 25 mins.

We were around 2500ft above from the ground and the view was fantastic from there. We had a rest there for some time as we were tired of the trekking. After some time the guys were ready with the glide and I was the first one to start. The pilot prepared me for the drive (helmet and glide dress which was tied with the glides), then Finally it was time to take off. We (me and my pilot) have to run for few distance and then jump from the hill.

"I ran slowly against the wind and suddenly, i felt an elevating force. In an instant, my feet were no longer touching the ground and i went into gentle decent.... The act of flying gives one sensations that are extraordinary and absolutely indescribable"

I was roaming in air around 2500 ft above from the ground. The surrounding hills and ground looks amazing from the top. After around 10-12 mins we landed in fields and this whole experience was amazing.

After a day, we went to Solang Valley for sight seeing. We saw the guys doing paragliding there. We didn't like it as the take off height was so short and the duration was only for couple of minutes. For us it was like swimming in a pond while we already swim in the sea :-)

Guys, if you want to do real paragliding, then you should go for this one.