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