Saturday, May 28, 2011

awk variables

In awk there are many default variables which can be used .
We will see each of them with examples:
acer@ubuntu:~$ cat > record1
John robinson
koren inc
phno:555555555


usha
yyy technologies
ph no:546456456


dddd
aaa technologies
ph no:44444444


acer@ubuntu:~$
consider the above record in which our requirement is to produce a single line for each record having the name with phone no.
In the above record ,the each record is separated by blank line.

For producing we need to
1)change the input record separator(RS) which is default to "\n" to blank line.
2)change the Field separator which is default to " " to  "\n"

acer@ubuntu:~$
acer@ubuntu:~$ cat separate.awk
BEGIN { FS="\n";RS="" }
{ print $1,$NF}
acer@ubuntu:~$ awk -f separate.awk record1
John robinson phno:555555555
usha ph no:546456456
dddd ph no:44444444

Here as you may see from the ouput the each record is converted into a single line.Here $NF refers to the last field in the record.

In the same way we can change the OFS and ORS and change the usual way the ouput is displayed .

In this we change the Output field separator(OFS) to "\t" from default separator space.
and the Output Record separator(ORS)  to "\n\n" from the default separator "\n"

Sample example illustrates the change better.
acer@ubuntu:~$ cat separate.awk
BEGIN { FS="\n";RS="";OFS="\t";ORS="\n\n" }
{ print $1,$NF}
acer@ubuntu:~$
acer@ubuntu:~$ awk -f separate.awk record1
John robinson    phno:555555555

usha    ph no:546456456

dddd    ph no:44444444

acer@ubuntu:~$


Regarding the usage of NR variable in awk.It just prints the record number.
Sample example:
acer@ubuntu:~$ cat ggg
hello life
wonderful
great
marvellous
awesome
acer@ubuntu:~$ awk '{print NR,$1 }' ggg
1 hello
2 wonderful
3 great
4 marvellous
5 awesome
acer@ubuntu:~$


NR can also be used in this way.
acer@ubuntu:~$ cat ggg
hello life
wonderful
great
marvellous
awesome
acer@ubuntu:~$ awk 'NR==1{print $0 ": i am in first row" }' ggg
hello life: i am in first row
acer@ubuntu:~$

"$0" usuage in awk is to print the complete record as from the below example:
acer@ubuntu:~$ awk '{print $0 }' ggg
hello life
wonderful
great
marvellous
awesome
acer@ubuntu:~$

Hope this gives a better idea about awk variables.

Kindly comment was the information useful.



The below table provides overview of all the awk variables available.

Variable
Represents
NR
$0
NF
$1-$n
FS
OFS
RS
ORS
FILENAME
record number of current record
the current record (as a single variable)
number of fields in the current record
fields in the current record
input field seperator (default:
 SPACE or TAB)
output field seperator (default:
 SPACE)
input record seperator (default:
 NEWLINE)
output record seperator (default:
 NEWLINE)
name of the current input file

finding no of blank lines in file using awk in shell



This program prints the no of blank lines in a file using awk in shell

acer@ubuntu:~$
acer@ubuntu:~$
acer@ubuntu:~$ cat > ddd

dsdsd

sdsdsd


dsds
acer@ubuntu:~$ awk 'BEGIN { x=0 }
> /$^/{ x=x+1 } # for counting the no of blank lines
> END { print " no of blank lines in the file  " x }' ddd
 no of blank lines in the file  4
acer@ubuntu:~$




For detailed understanding of BEGIN,END you can see the AWK PROGRAMMING MODEL blog in this site.

Thanks.

Was it useful.Kindly put your comment.


AWK programming model

awk is usually input driven .i.e. it executes the command for the no of lines in the file supplied or the no of lines which is piped to the awk command.

For example:

acer@ubuntu:~$ cat > fff
ewrrwer
ewrw
wer
werwe
ewrwer^Z
[1]+  Stopped                 cat > fff
acer@ubuntu:~$ awk '{ print "hello world" } ' fff
hello world
hello world
hello world
hello world
hello world
acer@ubuntu:~$

Here the hello world is printed  5 times since fff has 5 lines.

There is a exception to it you can use BEGIN or END command which executes without waiting for the input provided by the file or the pipe.

In case of BEGIN command in awk executes before the input from the file is processed.

For example:
acer@ubuntu:~$ cat ggg
hello life
wonderful
great
marvellous
awesome
acer@ubuntu:~$
acer@ubuntu:~$ awk 'BEGIN { print "hello from begin"}  
> /wonderful/ { print "hello during file process" }'  ggg    
hello from begin 
hello during file process
acer@ubuntu:~$

PROCESS IN AWK passes through three simple steps:

It executes the BEGIN COMMAND before any input is read.
                                     |
It process the file or input for the no of lines in file.(main loop)
                                     |
It executes the END command after the main loop ends.

Here the BEGIN and END are optional.

And a note about the END command it does not get executed if there is no lines in the file or input and it waits for input unlike BEGIN command.

acer@ubuntu:~$ awk 'END { print "hello from end command" }'
it waits endlessly

When you provide a input , it executes
acer@ubuntu:~$ echo "hello" | awk 'END { print "hello from end command" }'
hello from end command
acer@ubuntu:~$

These in combination can be used for writing many useful commands.

For example : for now we will write a shell program to print the no of lines in the file

acer@ubuntu:~$ awk 'BEGIN { x=0 }
> { x=x+1 }   # for adding the no of lines
> END { print "no of lines in the file is" x }' ggg
no of lines in the file is5
acer@ubuntu:~$


Hope this clarifies your doubt about AWK PROGRAMMING MODEL.

Kindly comment


Friday, May 27, 2011

sprintf usuage in perl and C

Sprintf:
Perl :
SYNTAX:sprintf(FORMAT,LIST);

sprintf is to produce the formatted string based on the input string.
It is based on sprintf C library routine .

sprintf is useless if  you give the no of digits to be padded less than the no of digits in your variable.As you may see from the below example:

dd="456456456";
$rr=sprintf("%04d",$dd);
print("$rr"."\n");


Output will be:456456456

But if you give the no of digits less than the padding digits.In addition to the digits available the remaining will be padded.
$dd="2";
$rr=sprintf( "%08d",$dd);
printf("$rr"."\n");

Output:00000002
Here 7 zeros are padded with the available digit 2.

The format has many options.In this case we round the digits to 3 decimal point.
$ff="333.45454545";
$rr=sprintf("%.3f",$ff);
printf("$rr"."\n");

Output:333.455

C language:
sprintf has a provision to save the formatted string in the syntax itself.

SYNTAX:sprintf(saved string ,format,string to be formatted);

Here the change is the formatted string is saved in the syntax itself,instead of saving through equality condition.

#include <stdio.h>int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a %d char long string\n",buffer,n);
  return 0;
}

Output:
[5 plus 3 is 8] is a 13 char long string






Monday, May 23, 2011

finding the memory size in Linux

To my knowledge there are three command to find the memory in Linux:

1)free command:

It is used to print the memory size in bytes by default

>> free
total used free shared buffers cached
Mem: 16438664 16294220 144444 0 235596 13380932
-/+ buffers/cache: 2677692 13760972

options:

-b - to print the bytes

-m - to print the information in MegaBytes

-k - to print the information in kilobytes.

>>free -m
total used free shared buffers cached
Mem: 16053 15953 100 0 214 13119
-/+ buffers/cache: 2619 13433
Swap: 9855 1 9854


2)Top command:

It is used to display the ongoing process in the Cpu and the memory used by the same.

>>Top
PID USERNAME THR PRI NICE SIZE RES SHR STATE TIME CPU COMMAND
4536 root 1 0 -20 37M 8696K 2204K sleep 189:47 1.60% mount
23308 karthik 1 15 0 18M 5948K 4392K sleep 0:00 1.60% vim
3754 root 1 16 0 10M 680K 584K sleep 3:03 0.20% vi


3)dmesg | grep ^Memory
This also gives the information about the available memory.

> dmesg | grep ^Memory
Memory for crash kernel (0x0 to 0x0) notwithin permissible range
Memory: 16435064k/17563644k available (2577k kernel code, 339988k reserved, 1305k data, 212k init

awk unix command

awk is a unix command

Awk is used to get a particular pattern in a ouput

you can use this to print a particular pattern

To print the third column in the output using space as the delimiter
awk -F " " '{print $3}'

If you want to print the particular row and a particular column you can use like this

awk -F " " 'NA==3{print $2}'

-F is used as a the Field separator

awk print

Awk is used to get a particular pattern in a ouput

you can use this to print a particular pattern

To print the third column in the output using space as the delimiter
awk -F " " '{print $3}'

If you want to print the particular row and a particular column you can use like this

awk -F " " 'NA==3{print $2}'