Tuesday, November 30, 2010

Reverse the characters in each word in a string?

Perl Script:


#!/usr/bin/perl5.8.8

$x = "the cat in the hat";

$x =~ s/(\w+)/reverse $1/ge
  
OUTPUT:
 
$x contains "eht tac ni eht tah"
 

WAP to print each character in the word hello in separate lines?

Perl Script:


#!/usr/bin/perl5.8.8

my $word="hello";
chomp($word);
my $l=length($word);
chomp($l);
my $i;
for ($i=0; $i<= $l; $i++)  {
my $x = substr($word,$i,1);
print "$x \n";
}



OUTPUT:
h
e
l
l
o

Monday, November 29, 2010

How to import the data into an excel sheet using Perl / Shell?

INPUT: 

%cat temp.txt

abc 123 abc@123
def  456 def@456
ghi  789 ghi@789

Perl Script:

#!/usr/bin/perl5.8.8

open FH1, "temp.txt" or die "cannot open the file $!: \n";
open FH2, ">output_file.csv" or die "cannot open file handler  $!: \n";  ###( .csv stands for comma separated variables.)
@lines=<FH1>;
foreach my $l (@lines) {

chomp($l);
@words=split(/\s+/,$l);
foreach my $w (@words) {
print FH2 "$w,";
}
print FH2 "\n";
}
close(FH1);
close(FH2);

Shell Script:

#!/bin/sh

file=`cat ./temp.txt | sed -s 's/ /,/g'`;
echo "$file" > output_file.csv;


OUTPUT: 

Open the output_file in a cvs format viewer. You can find an excel sheet with the information.

Wednesday, November 24, 2010

What is the use of "-f" option in shell scripts, For ex: /bin/sh -f?

For a csh script, the first line is typically "#!/bin/csh -f".  The "-f" option
tells the shell not to read the user's .cshrc file on startup, which improves
both speed and portability.

It's a common mistake to do the same thing for Bourne shell scripts by using
"#!/bin/sh -f".  However, the Bourne shell's "-f" option is completely
different from csh's "-f" option.  It disables file name generation.  For
example, the following script:

    #!/bin/sh
    echo foo*
 
OUTPUT: 
 foo foo1 foo2 foo123 

will print the names of all the files in the current directory whose names
begin with "foo" (or a literal "foo*" if there are no such files), but the
following script:

    #!/bin/sh -f
    echo foo* 
 
OUTPUT:
 foo*

will print a literal "foo*" unconditionally.

There are a number of Globus scripts that use "#!/bin/sh -f".  It's possible
that this was deliberate, but unlikely.  A quick look at the scripts indicates
that none of them use file wildcards anyway, so the "-f" probably has no
effect.

Suggestion: Replace "#!/bin/sh -f" with just "#!/bin/sh", just to avoid
confusion.

Sunday, November 21, 2010

How to find the IP address of a linux machine?

To find out IP address of Linux/UNIX/BSD/Unixish system you need to use command called

ifconfig.

It is used to configure the kernel-resident network interfaces. It is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed. If no arguments are given to ifconfig command it displays the status of the current active interfaces. It displays Ethernet IP address, Mac address, subnet mask and other information. Type /sbin/ipconfig command to display IP address:


$ /sbin/ifconfig

OUTPUT:

eth0     Link encap:Ethernet  HWaddr 00:0F:EA:91:04:07
  inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
 
In above example 192.168.1.2 is IP address of eth0 Ethernet interface.
 

Wednesday, November 10, 2010

What is Fedora OS?

Fedora: is an RPM-based, general purpose collection of software including an operating system based on the Linux kernel, developed by the community-supported Fedora Project and sponsored by Red Hat. The Fedora Project's mission is to lead the advancement of free and open source software and content as a collaborative community.

Main Objective: Contain software distributed under free and open and content as a collaborative community.