Tuesday, September 4, 2012

How do you execute a command and kill itself after some duration?

timeout 5s <COMMAND>

-> Starts COMMAND, and kills itself after 5 seconds

How can you sort the values of a Hash alphabeticlly?

#!/usr/bin/perl
   
%FM = ( '91.1 '=> 'Radio City',
              '92.7' => 'Big FM',
              '93.5' => 'Red FM',
              '94.3' => 'Radio One',
              '98.3' => 'Radio Mirchi',
              '104.0' => 'Fever',
              '104.8' => 'Oye FM',
              '107.1' => 'FM Rainbow',
              '100.7' => 'FM Gold');

foreach $station (sort { $FM{$a} cmp $FM{$b} } keys %FM)
{
    print "$station is $FM{$station}.\n";
}

How can you sort the values of a Hash numerically?

#!/usr/bin/perl
   
%FM = ( 'Radio City' => '91.1',
              'Big FM' => '92.7',
              'Red FM' => '93.5',
              'Radio One' => '94.3',
              'Radio Mirchi' => '98.3',
              'Fever' => '104.0',
              'Oye FM' => '104.8',
              'FM Rainbow' => '107.1',
              'FM Gold' => '100.7');

foreach $station (sort { $FM {$a} <=> $FM {$b}} keys %FM )
{
        print "$station is $FM{$station} MHz.\n";
}

How can you sort the keys of a Hash alphabetically?

#!/usr/bin/perl
   
%FM = ( 'Radio City' => 91.1,
              'Big FM' => 92.7,
              'Red FM' => 93.5,
              'Radio One' => 94.3,
              'Radio Mirchi' => 98.3,
              'Fever' => 104.0,
              'Oye FM' => 104.8,
              'FM Rainbow' => 107.1,
              'FM Gold' => 100.7);

foreach $station (sort keys %FM)
{
    print "$station is $FM{$station} MHz.\n";
}

How can you sort the keys of a Hash numerically?

#!/usr/bin/perl

%FM = ( '91.1' => 'Radio City',
              '92.7' => 'Big FM',
              '93.5' => 'Red FM',
              '94.3' => 'Radio One',
              '98.3' => 'Radio Mirchi',
              '104.0' => 'Fever',
              '104.8' => 'Oye FM',
              '107.1' => 'FM Rainbow',
              '100.7' => 'FM Gold');

for $key ( sort {$a<=>$b} keys %FM )
{
        print "$key->$FM{$key}\n";
}

or

foreach ( sort { $a <=> $b } keys(%FM) )
{
    print "$_ = $FM{$_}\n";
}

How can you get the keys of a Hash in Perl?

#!/usr/bin/perl

%FM = ( 'Radio City' => 91.1,
              'Big FM' => 92.7,
              'Red FM' => 93.5,
              'Radio One' => 94.3,
              'Radio Mirchi' => 98.3,
              'FM Gold' =>100.7,
              'Fever' => 104.0,
              'Oye FM' => 104.8,
              'FM Rainbow' => 107.1);

#get keys only
foreach my $key (sort keys %FM)
{
        print "$key\n";
}

Write shell script to convert 10 to binary.

#!/bin/sh

binary=({0..1}{0..1}{0..1}{0..1})
echo ${binary[10]}

How could you delete all empty lines from a file within VI?

:g/^$/d
or
:g!/\S/d

Monday, September 3, 2012

How do you get the duplicate/repeated lines in a file in UNIX?

uniq -d <FileName> # print only those lines which are repeated

uniq -c <FileName> # adds the number of occurrences of each line

uniq -u <FileName> # print only those lines which are not at all repeated

What is the substitute to backticks?

"$( )" is the substitute to backticks (``) with an advantage of nesting commands without escapes and better readability. The only bottleneckness $( ) suffers from is lack of portability.

For Example,

VAR=`date +%D`
echo "The date is: $VAR"

or

echo "The date is: `date +%D`"

The above lines can be replaced with the following one :

echo "The date is: $(date +%D)"