Friday, June 26, 2009

Perl Errors

Missing Module in array @INC

(use perl -e 'print join("\n", @INC);' to print the list of directories in @INC)

Errors like: Can't locate TimeDateNum.pm in @INC (@INC contains: /usr/local/lib/perl5/5.8.5/i86pc-solaris /usr/local/lib/perl5/5.8.5 /usr/local/lib/perl5/site_perl/5.8.5/i86pc-solaris /usr/local/lib/perl5/site_perl/5.8.5 /usr/local/lib/perl5/site_perl .) at ./test.pl line 5.

Solution:

We need to provide the missing module to perl. It can be done in the following ways.
1. Specify it in the PERL5LIB environment variable
export PERL5LIB="path of required module"

2. Use the -I option to specify the additional directories where it should search for modules while invoking perl.
perl -I "path of required module" script.pl

3. Modify your Perl program to find the module by adding the below line at the top of your program.

use lib "path of required module";

Perl adds this directory to it's @INC search list.


Thursday, June 25, 2009

sed Samples

Quick Examples:

General Syntax:
sed -option 'general expression' [data-file]
sed -option sed-script-file [data-file]

Option can be:

OptionMeaningExample
-eRead the different sed command from command line.
$ sed -e 'sed-commands' data-file-name
$ sed -e 's/test/TEST/' File
$ sed -e

-fRead the sed command from sed script file.
$sed -f
$ sed -f chgdb.sed friends.tdb
-nSuppress the output of sed command. When -n is used you must use p command of print flag to explicitly specify what is to be printed.$ sed -n '/^\*..$/p'

Search and replace:

$ sed 's/Linux/UNIX/' TestFile > file.out

here 's' is the search command and '/' is the delimiter used to separate the text to be searched and replaced.

Deleting blank lines

Using sed you can delete all blank line from file as follow
$ sed '/^$/d' demofile1
Here the pattern /^$/, match blank line and d, command deletes the blank line.

Using Regular Expressions with sed

$ sed -n '/10\{2\}1/p' TestFile

prints 1001, search pattern \{2\}.
Syntax:
\{n,\}
At least nth occurrences will be matched. So /10\{2\} will look for 1 followed by 0 (zero) and \{2\}, tells sed look for 0 (zero) for twice.

Matching any number of occurrence

Syntax:
\{n,\m}
Matches any number of occurrence between n and m.

Example:
$ sed -n '/10\{2,4\}1/p' TestFile2
1001
10001
100001
Will match "1001", "10001", "100001" but not "101" or "10000000".

$ sed -n '/^\*..$/p' demofile2
***
***
Above command prints all lines that begins with *** (three stars or asterisks),

Explanation:
Command
Explanation
^Beginning of line
\* Find the asterisk or star (\ remove the special meaning of '*' metacharacter)
..

Followed by any two character (you can also use \*\* i.e. $ sed -n '/^\*\*\*$/p' demofile2 )

$

End of line (So that only three star or asterisk will be matched)

/pPrint the pattern.

Also the following expression can be used for the same purpose
$ sed -n '/^\*\{2,3\}$/p' TestFile2

Following command finds out lines between *** and *** and then delete all those line
$sed -e '/^\*\{2,3\}$/,/^\*\{2,3\}$/d' demofile2 > /tmp/fi.$$
$cat /tmp/fi.$$


Above expression can be explained as follows

ExpressionMeaning
^Beginning of line
\* Find the asterisk or star (\ remove the special meaning of '*' metacharacter)
\{2,3\}Find next two asterisk
$End of line
,Next range or search pattern
^\*\{2,3\}$Same as above
dNow delete all lines between *** and *** range


basic awk

Built-In AWK Variables

Variable Meaning Default
ARGC Number of command line arguments –
ARGV Array of command line arguments –
FILENAME Name of current input file –
FNR Record number in current file –
FS Controls the input field separator one space
NF Number of fields in current record –
NR Number of records read so far –
OFMT Output format for numbers %.6g
OFS Output field separator one space
ORS Output record separator \n
RLENGTH Length of string matched by match function –
RS Controls the input record separator \n
RSTART Start of string matched by match function –
SUBSEP Subscript separator \034


awk samples:


➨ Print the total number of lines in file to the screen.
→awk ‘END {print NR}’ filename
➨ Prints the 10th input line to the screen.
→awk ‘NR == 10 {print}’ filename
➨ The print command is to print only the first field ($1) of every line found in the file.
→awk ‘{print $1}’ filename
➨ Print the last field of the last input line to the screen.
→awk ‘{field=$NR} END {print field}’ filename
➨ Print all input lines ($0) from file that have more than 4 fields (NF>4).
→awk ‘NF > 4 {print $0}’ filename

➨ Print the values in the first ($1), fourth ($4), and third ($3) fields from every line in the file filename in the listed order to the screen
separated by the output field separator (OFS) which is one space by default.
→awk ‘{print $1, $4, $3}’ filename
➨ This searches for fields that start (^) with MDATA (~/MDATA/) in the first field ($1). For every match, it increments linesdata by one
(++linesdata). After the entire filename has been read, the program prints to the screen the number of lines that met the criteria along
with a little sentence quoting the name of the input file ($FILENAME).
→awk ‘BEGIN {linesdata=0} $1 ~/^MDATA/ {++linesdata} END {print linesdata “ Lines \
start with MDATA in the first field from “ $FILENAME}’ filename
➨ IF the value in the first field in file is equal to 0, THEN the entire line ($0) will be printed to the screen.
→awk ‘($1 == 0) {print $0}’ filename

➨ This will find the largest time value in the first field in the entire file and after finishing reading the file, it will print the maximum time
found in the first field followed by the entire line from which the value came.

→awk ‘($1 > timemax) {timemax = $1; maxinput = $0} \
END {print timemax, maxinput}’ filename
➨ All output from these commands will go to the screen, but you can use UNIX redirection commands to “pipe” the output into another
command or > “redirect” the output to a file or even >> “append” the output to the and of a pre-existing file.
➨ Awk is also very useful when you need to put information in a different format for a script. Just include the formatting awk statement in
your script.