Awk基本入门[6] Additional Awk Commands 4

1、Bit Manipulation


 Just like C, awk can manipulate bits.

AND  and 

OR or

XOR xor

Complement compl

Left Shift  lshift

Right Shift rlshift

 

Awk Example using Bit Functions

$ cat bits.awk
BEGIN {
  number1=15
  number2=25
  print "AND: " and(number1,number2);
  print "OR: " or(number1,number2)
  print "XOR: " xor(number1,number2)
  print "LSHIFT: " lshift(number1,2)
  print "RSHIFT: " rshift(number1,2)
}
$
awk -f bits.awk AND: 9 OR: 31 XOR: 22 LSHIFT: 60 RSHIFT: 3

 

 2、User Defined Functions


 

Awk allows you to define user defined functions. This is extremely helpful when you are writing a lot of awk code and end-up repeating certain pieces of code every time. Those pieces could be fit into a user defined function.

Syntax:

function fn-name(parameters)
{
    function-body
}

The following example creates a simple user defined function called discount that gives a discount in the prices for the specified percentage. For example, discount(10) gives 10% discount on the price.

For any items where the quantity is <= 10, it gives 10% discount,otherwise it gives 50% discount.

$ cat function.awk
BEGIN {
  FS=","
  OFS=","
}
{
if ($5 <= 10)
  print $1,$2,$3,discount(10),$5
else
  print $1,$2,$3,discount(50),$5
}
function discount(percentage)
{
  return $4 - ($4*percentage/100)
}
$ awk -f function.awk items.txt
101,HD Camcorder,Video,189,10
102,Refrigerator,Appliance,765,2
103,MP3 Player,Audio,135,15
104,Tennis Racket,Sports,95,20
105,Laser Printer,Office,427.5,5

 

3、Language Independent Output (Internationalization)


 

 

4、Two Way Communication


 Awk can communication to an external process using "|&", which is two way communication.

The following simple sed example substitutes the word "Awk" with "Sed and Awk".

 

$ echo "Awk is great" | sed 's/Awk/Sed and Awk/'
Sed and Awk is great

To understand how the two way communication from Awk works, the following awk script simulates the above simple example using "|&"

$ cat two-way.awk
BEGIN {
command = "sed 's/Awk/Sed and Awk/'"
print "Awk is Great!" |& command
close(command,"to");
command |& getline tmp
print tmp;
close(command);
}

$ awk -f two-way.awk
Sed and Awk is Great!

In the above example:

  • command = "sed 's/Awk/Sed and Awk/'" -- This is the command to which we are going to establish the two way communication from awk. This is a simple sed substitute command, that will replace "Awk" with "Sed and Awk".
  • print "Awk is Great!" |& command -- The input to the command. i.e. The input to the sed substitute command is "Awk is Great!". The "|&" indicates that it is a two way communication. The input to the command on the right side to the "|&" comes from the left side.
  • close(command,"to") - Once the process is executed, you should close the "to" process.
  • command |& getline tmp - Now that the process is completed, it is time to get the output of the process using the getline. The output of the previously executed command will now be stored in the variable "tmp".
  • print tmp - This prints the output.
  • close(command) - Finally, close the command.


Two way communication can come-in handy when you rely heavily on output from external programs.

 

see: http://www.gnu.org/software/gawk/manual/html_node/Two_002dway-I_002fO.html

 

posted @ 2013-09-09 17:17  风*依旧  阅读(328)  评论(0编辑  收藏  举报