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.

Wednesday, October 20, 2010

How to interpret the information about a file when using command : "ls -lrt"

Here is an example of the additional information about a file which we can get when used with "ls -lrt"

Example:

lrwxrwxrwx   1 sarala  7 2010-10-20 03:40 testfile



(l , d) -
rwx

rwx

rwx

1

sarala
7

2010-10-20  03:40

testfile

 (Symlink, directory) file owner group Everyone else links owner size Modified date File name

chmod command

chmod: command is used to set the access permissions of a specific file or directory.

Example:

chmod who=permissions filename   (This gives “who” the specified permissions for a given filename.) 
Permissions: 
Of course, the permissions are the same letters that you see in the directory listing: 

  r   Permission to read the file.
  w   Permission to write the file.
  x   Permission to execute the file, or, in the case of a directory, search it.


who: 

The “who” is a list of letters that specifies whom you’re going to be giving permissions to. These may be specified in any order. 

LetterMeaning
  u   The user who owns the file (this means “you.”)
  g   The group the file belongs to.
  o   The other users
  a   all of the above (an abbreviation for ugo

Wednesday, September 29, 2010

Replace a string in a file and rewrite to the same file.

Perl One liner:

% perl -pi -e 's/HEAD/TAIL/g' file


Using sed command:

% sed 's/PERL/perl/g' txt1 > TMP_FILE

% mv TMP_FILE txt1
Using Perl Script: 
#!/depot/perl-5.8.3/bin/perl

open FH1,"txt1";
@arr1=<FH1>;
open FH1,">txt1";
foreach $l (@arr1){
$l =~ s/this is sarala/this is SARALA/g;
print FH1 "$l";
}
close FH1;


(or)
 #!/depot/perl-5.8.3/bin/perl

open FH1,"txt1";
@arr1=<FH1>;
foreach my $l (@arr1) {
$l =~ s/this is sarala/this is SARALA/g;
##print "$l";
}
print "@arr1";
close FH1;
unlink txt1;
open FH2, ">", "txt1";
print FH2 "@arr1";
close FH2;



Tuesday, September 28, 2010

Predefined macros in Makefiles.

MAKE - current make program name
$?   - list of dependencies, can be used in target rules
$@   - current target name, can be used in target rules
$<   - target prerequisite, can be used in target rules
$*   - name without suffix, can be used in target rules
$%   - name corresponding .o file, can be used in target rules
@D   - directory part of target name, can be used in target rules
@F   - file name part of target name, can be used in target rules
$$va - make strips first $ and sends $va to shell, contents of var.

What is double colon target in Makefiles?

Double colon target - can exist in a makefile more than once with different dependencies and target rules. Another effect is that they are always executed so they work nicely for directory traversal targets where the target name is a directory which is always up to date. Finally once you define a target name with a double colon you can not have the same name with a single colon.

directory::
  cd $@ && "$(MAKE)"
      

Monday, September 27, 2010

What is a phony target in Make?

A phony target is one that is not really the name of a file; rather it is just a name for a recipe to be executed when you make an explicit request.

There are two reasons to use a phony target:
      a)  To execute the target unconditionally (i.e No dependencies are mentioned)
      b)  to avoid a conflict with a file of the same name
      c) to improve performance.

Example:
PHONY: clean 
clean:
             rm *.o temp

Advantages of Perl over Shell?

1) Perl is platform independent - Perl runs on all platforms and is far more portable. Can be used on Unix as well as windows platform.

2) Perl provides most advanced regular expressions which is used for text processing which made it very popular language for text processing. The name itself indicated this "Practical Extraction and Reporting Language" - so extensively used for pattern extraction and reporting.

3) Perl Provides :
     a) Modularization (i.e Packages)
     b) Object Oriented Techniques.
     c) Good memory management, efficiently implemented data structures like lists and hashes.

4) Better Performance compared to Shell while processing huge files.

Difference between grep, egrep and fgrep?

a) grep: Search for a Pattern  in the named input files.

Example: 

% cat text
Hiii

How r u...

fish

fishes

fisherman

Excellent

%  grep fish text

fish
fishes
fisherman

b) egrep:  (grep -E in linux is -E, --extended-regexp ) Extended grep where additional regular expression metacharacters have been added like ^, $,*,.,|,\,+, ?, | and ().
Example:

% grep -E '^fish' text

fish
fishes
fisherman

c) fgrep: (grep -F in linux is -F, --fixed-strings) Fixed or fast grep and behaves as grep but does not recognise any regular expression metacharacters as being special.

Example: 

% grep -F fish text

fish
fishes
fisherman

How to count empty lines in a file?

% cat file_name | grep -E '^$' | wc -l

How to print a particular line in a file? For example 5 th line in a file

head -5 file_name | tail -1

Friday, September 17, 2010

Difference between system, exec and `` (backticks) in perl

They're all different, and the docs explain how they're different. Backticks capture and return output; system returns an exit status, and exec never returns at all if it's successful.

exec

executes a command and never returns. It's like a return statement in a function.
If the command is not found execute returns false. It never returns true, because if the command is found it never returns at all. There is also no point in returning STDOUT, STDERR or exit status of the command. You can find documentation about it in perlfunc, because it is a function.

system

executes a command and your perl script is continued after the command has finished.
The return value is the exit status of the command. You can find documentation about it in perlfunc.

System fork the current process into two processes (parent and child) and replace the child process
with an exec call to the external program while the parent process  waits for the child process to complete.  This is what the system
function** does.  It is implemented in terms of a fork***, an exec, and a waitpid****. 

backticks

like system executes a command and your perl script is continued after the command has finished.
In contrary to system the return value is STDOUT of the command. qx// is equivalent to backticks. You can find documentation about it in perlop, because unlike system and exec it is an operator.

Unix Vs Ms-Dos Commands

Unix                                MS-DOS


ls                                    dir
clear                                 cls
cp                                  copy
rm                                  del
rm -rf                             deltree
vi                                   edit
mv                                 move / rename
cd                                 chdir / cd
mkdir                             md
set/setenv                       set temp=C:\temp   
echo $PATH                  echo %PATH%
pwd                               echo %cd%
history                            DOSKEY /history

How to find all the files which has a pattern "xxx" in its file name and delete them in unix?

rm -rf `find . -name "*xxx*"`

WAP to reverse the string.

#!/depot/perl-5.8.3/bin/perl

my @line=qw (Hii welcome to Perl.);
@arr1=reverse (@line);
print "@arr1 \n";

OUTPUT:

Perl. to welcome Hii

what is $_ in perl?

$_ is known as the "default input and pattern matching space"

How does a parent and the child process communicate?

A parent and child can communicate through any of the normal
inter-process communication schemes (pipes, sockets, message queues,
shared memory), but also have some special ways to communicate that take
advantage of their relationship as a parent and child.

One of the most obvious is that the parent can get the exit status of
the child through waitpid() call.

What is a fork process?

The fork() system call will spawn a new child process which is an identical process to the parent except that has a new system process ID.

The process is copied in memory from the parent and a new process structure is assigned by the kernel.

The return value of the function is which discriminates the two threads of execution. A zero is returned by the fork function in the child's process.

The environment, resource limits, umask, controlling terminal, current working directory, root directory, signal masks and other process resources are also duplicated from the parent in the forked child process.

The two processes obviously have two different process id.s. (pid). In a C program process id.s are conveniently represented by variables of pid_t type, the type being defined in the sys/types.h header.

In UNIX the PCB of a process contains the id of the process's parent, hence the child's PCB will contain as parent id (ppid) the pid of the process that called fork(), while the caller will have as ppid the pid of the process that spawned it.

The child process has its own copy of the parent's file descriptors. These descriptors reference the same under-lying objects, so that files are shared between the child and the parent. This makes sense, since other processes might access those files as well, and having them already open in the child is a time-saver.

How to find which kernel version installed on a Linux system?

% uname -r

OUTPUT: 

2.6.9-67.ELsmp

Difference between XML and HTML

XML files are meant to hold data and data in an xml file is well described. If you look at an xml file you can say what it holds. For example if you find a number in an xml file you can find out easily what that number identifies, whether it is the number of products, or the price of a product etc. In html it is not the case. 
 
HTML is used to display the data in a formatted way. You can apply styles and use different layouts to display the data in an html file. The data that is displayed in an html file could come from an xml file.
So to say in simple words, html displays the data and xml holds the data!

Difference between use and require

A use anywhere in the code will be evaluated when the code is run compiled, but require - import's can only get evaluated when encoutered - good for when you want one module or another but both have quite a large initialisation overhead which means you only want the one you need.

The use statement works at compile time, require at run time. So if you have a module that does a lot in a begin block and you don't really need it in all cases, then it's clever to "require" it there where you want to use it but don't "use" it. So you don't have to wait for all that initializations of that module in case you don't need it.

When you 'use' the control is passed to that file at runtime.
Whereas, when you 'require' the code from that file is included into your code file at run time. (Remember, 'perl' is an interpreter, not compiler).


Use :
1. The method is used only for the modules(only to include .pm type file)
2. The included objects are verified at the time of compilation.
3. No Need to give file extension.
Require:
1. The method is used for both libraries and modules.
2. The included objects are verified at the run time.
3. Need to give file Extension.

 
use myModule;

or

require "myModule.pm";

Difference between "my" and "local"

1) Quick summary: 'my' creates a new variable, 'local' temporarily amends the value of a variable

2) The variables declared with "my" can live only within the block it was defined and cannot get its visibility in the inherited functions called within that block, but one defined with "local" can live within the block and have its visibility in the functions called within that block.

3) The variables declared with my() are visible only within the scope of the block which names them. They are not visible outside of this block, not even in routines or blocks that it calls. local() variables, on the other hand, are visible to routines that are called from the block where they are declared. Neither is visible after the end (the final closing curly brace) of the block at all.



4) My: 

My creates a new variable and gives a lexical scope for that variable.The variable is not visible outside the block in which it is defined.
 

 Local:

Local saves the value of a global variable.It never creates a new variable. Here also the variable is not accessible outside the block but it is visible in the subroutine which is called from the original block. My is more preferring than local . But in various places local is useful where my is illegal.



Example:

#!/usr/local/bin/perl
$var=4;
print $var, "\n";
&sub_routine1;
print $var, "\n";
&sub_routine2;
print $var, "\n";

#subroutine

sub sub_routine1 {
        local $var = 10;
        print $var, "\n";
        &sub_routine2;
        print $var, "\n";
}

sub sub_routine2 {
        $var++;

}



OUTPUT:

4
10
11
4
5

Wednesday, September 1, 2010

Multiple patterns in a sinlge grep command

find . -name "*" | grep -E '\.c$|\.cc$|\.h$|\.cpp$|\.cxx$'

bc command for Floating point operations.

Example of bc command:

PASS_RATE=`echo "scale=2;(($PASS_main + $PASS_dp) * 100 ) / ($TOTAL_main + $TOTAL_dp) " | bc -l ` ;

"-l": option is to load math routines and perform the arithmetic operations.

scale: is used to specify the no. of significant digits in the output.

Command line options of bc command.

/usr/bin/bc -h

usage: /usr/bin/bc [options] [file ...]
  -h  --help         print this usage and exit
  -i  --interactive  force interactive mode
  -l  --mathlib      use the predefine math routnes
  -q  --quiet        don't print initial banner
  -s  --standard     non-standard bc constructs are errors
  -w  --warn         warn about non-standard bc constructs
  -v  --version      print version information and exit

Monday, August 23, 2010

Difference between Windows and Unix?

Unix is much better at handling multiple tasks for a single user or for multiple users than windows.

Unix:

Multiple Users can access and use the unix systems


Windows:

Single User can access and use the system.

Wednesday, August 18, 2010

Important file tests in Perl

File Test                   Meaning

-M                           Modification age (measured in days)

-A                            Access age (measured in days)

-C                            Inode modification age (measured in days)

The shift and unshift operators

The shift and unshift operators perform the operations on the start of the array ( the left side of an array or the portion with the lowest subscripts ).

shift Operator:

@array=qw # abc def ghi #;
$element_1=shift(@array); #gets abc
$element_2=shift @array; #gets def

unshift Operator:


unshift (@array, zyx); # @array has zyx abc
unshift @array, wvu; # @array has wvu zyx abd
@other_array=1..5; # has 1 2 3 4 5
unshift @array, @other_array; #@array now has 1 2 3 4 5 wvu zyx abd

Push and Pop Operator

Push and pop functions are used to implement stack where new values are added to and removed from the right hand side of the list.

pop Operator:

Pop operator takes the last element off an array and returns it:

For example:

$array=5..9;                               #5 6 7 8 9
$last_element_1=pop(@array);   #gets 9

$last_element_2=pop @array;    # gets 8 ( pop operator works without parentheses also)

push Operator:

Push operator adds and element to the end of the array

For example:

@array=3..6;         #3 4 5 6
push(@array, 7);    #@array has 3 4 5 6 7
push @array, 8;     #@array has 3 4 5 6 7 8

Short Circuit Operator in Perl?

The logical "or" operator i.e "||" is called as Short Circuit Operator.

Perl Script to reverse a string

Program:

#!/depot/perl-5.8.3/bin/perl

my $str1="Madam i m adaM";
print "ORIGINAL_STRING: $str1 \n";
my $l=length($str1);
my $rev="";
while ($l >0) {
    $l--;
    $rev .= substr($str1,$l,1);
    }
print "REVERSED_STRING: $rev \n";

Sub routine with an array, a scalar and a string as arguments

#!/depot/perl-5.8.3/bin/perl

@arr1=(1..4);
my $count="9";
my $str1="To verify the subroutine argument list";
&dolist(\@arr1, $count, $str1);

sub dolist
   {
      print "ALL_ARGUMENTS = @_ \n";
      my @list = @{$_[0]};
      print "ARRAY  ==>  @list \n";
      my $c = $_[1];
      print "SCALAR  ==>  $c\n";
      my $string1= $_[2];
      print  "STRING  ==>  $string1 \n";

}

Perl Command Line options

-p   -  Write a perl program

-w  -   Enables warnings

-e   -   Executable code follows

-d   -   Enable diagnostics

-M  -  To load a pragma only when needed instead of loading it at the compile time.

System Variables

$$     -   Yields the process id of the perl program
$@   -    Yields the error message STDERR
$|      -   Yields the corresponding System Error
@_   -    Stores all the arguments passed to a sub routine
$&    -    Part of  the string that matches the pattern
$`      -   String before the pattern match
$'       -   String after the pattern match
$^I    -     Backup file name's extension

Find the missing element - There are 9 No's which are distinct and are within the range 1-10. How to find the missing element.

Solution 1:

#!/depot/perl-5.8.3/bin/perl

@array=(1, 2, 3, 4, 5, 6, 7, 8, 10);
foreach $n (1..10) {
if (!grep (/\b$n\b/,@array)) {
    print "$n \n";
}
}

Solution 2:

As the no's are in sequence and without repetition we can use the summation formula n (n + 1) /2.

 #!/depot/perl-5.8.3/bin/perl

@array=(1, 2, 3, 4, 5, 6, 7, 8, 10);
my $sum_of_10_consecutive_no=(10 * 11 / 2);
my $sum=0;
foreach (@array) {
    $sum += $_;
    }
print $sum_of_10_consecutive_no - $sum;

Perl script to print the below triangle pattern

OUTPUT:

1
1 2
1 2 3
1 2 3 4

Program:

#!/depot/perl-5.8.3/bin/perl

foreach (1..4) {
@a=(1..$_);
print "@a \n";
}

Given a file, print the count of all the words (OUTPUT: word --> count)

This is using two for loops so the time complexity would be n^2, where n is the no of lines in file1.



#!/depot/perl-5.8.3/bin/perl

open FH1, "file1" or die "Cannot open file file1 : $!";
@array=<FH1>;
foreach $l (@array) {
push(@arr1,split(/\s+/,$l));
}
my %hash1;
foreach $y (@arr1) {
$hash1{$w}=0;
foreach $z (@arr1) {
if ($y eq $z) {
$hash1{$y}++;
}
}
}

foreach $key (keys %hash1) {
print "$key --> $hash1{$key} \n";
}



This is using a while and a for loop, the time complexity is "n":

#!/usr/local/bin/perl

use strict;
use warnings;

my %count;
my $file = shift or die "Usage: $0 FILE\n";
open my $fh, '<', $file or die "Could not open '$file' $!";
while (my $line = <$fh>) {
        chomp $line;
        foreach my $str (split /\s+/, $line) {
             $count{$str}++;
        }
}

foreach my $str (sort keys %count) {
        printf "%-31s %s\n", $str, $count{$str};
}


USAGE: ./count.pl  txt1

OUTPUT:

Hii                             2
am                              1
are                             3
doing                           1
hii                             2
how                             2
well                            1
you                             3