AWK
Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan.
Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators.
AWK is one of the most prominent text-processing utilities on GNU/Linux. It is very powerful and uses a simple programming language.
Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that define text patterns that are to be searched for in each line of a document and the action that is to be taken when a match is found within a line.
1. AWK Operations:
(a) Scans a file line by line
(b) Splits each input line into fields
(c) Compares input line/fields to pattern
(d) Performs action(s) on matched lines
2. AWK Useful For:
(a) Transform data files
(b) Produce formatted reports
Syntax:
$ awk options 'selection _criteria {action }' input-file > output-file
Or
$ gawk options 'selection _criteria {action }' input-file > output-file
Options:
-f program-file: Reads the AWK program source from the file
Awk is not just a command, but a programming language too. In other words, awk utility is a pattern scanning and processing language. It searches one or more files to see if they contain lines that match specified patterns and then performs associated actions, such as writing the line to the standard output or incrementing a counter each time it finds a match.
Variables
Awk allows the user to use variables of their choice. You can now print a serial number, using the variable count, and apply it to those directors drawing a salary exceeding 6700:
$ awk –F "|" '$3 == "director" && $6 > 6700 {
kount = kount+1
printf " %3f %20s %-12s %d\n", kount,$2,$3,$6 }' empn.lst
THE –f OPTION: STORING awk PROGRAMS IN A FILE
You should hold large awk programs in separate files and provide them with the awk extension for easier identification. Let's first store the previous program in the file
empfile.awk:
$ cat empfile.awk
Observe that this time we haven't used quotes to enclose the awk program. You can
now use awk with the –f filename option to obtain the same output:
$ gawk –F "|" –f empfile.awk empn.lst
THE BEGIN AND END SECTIONS
Awk statements are usually applied to all lines selected by the address, and if there are no addresses, then they are applied to every line of input. But, if you have to print something before processing the first line, for example, a heading, then the BEGIN section can be used gainfully. Similarly, the end section useful in printing some totals after processing is over.
The BEGIN and END sections are optional and take the form
BEGIN {action}
END {action}
These two sections, when present, are delimited by the body of the awk program. You
can use them to print a suitable heading at the beginning and the average salary at the end.
BUILT-IN VARIABLES
Awk has several built-in variables. They are all assigned automatically, though it is also possible for a user to reassign some of them. You have already used NR, which signifies the record number of the current line. We'll now have a brief look at some of the other variables.
The FS Variable:
as stated elsewhere, awk uses a contiguous string of spaces as the default field delimiter. FS redefines this field separator, which in the sample database happens to be the |. When used at all, it must occur in the BEGIN section so that the body of the program knows its value before it starts processing:
BEGIN {FS="|"}
This is an alternative to the –F option which does the same thing. The OFS Variable: when you used the print statement with comma-separated arguments, each argument was separated from the other by a space. This is awk's default output field separator, and can reassigned using the variable OFS in the BEGIN section:
BEGIN { OFS="~" }
When you reassign this variable with a ~ (tilde), awk will use this character for delimiting the print arguments. This is a useful variable for creating lines with delimited fields. The NF variable: NF comes in quite handy for cleaning up a database of lines that don't contain the right number of fields. By using it on a file, say emp.lst, you can locate those lines not having 6 fields, and which have crept in due to faulty data entry:
$awk 'BEGIN {FS = "|"}
NF! =6 {
Print "Record No ", NR, "has", "fields"}' empx.lst
Output NS2 Trace file:
The Trace file contains 12 columns: Event type, Event time, From Node To Node, Packet Type, Packet Size, Flags (indicated by --------), Flow ID, Source address, Destination Address, Sequence ID, Packet ID
Structure of Trace Files
When tracing into an output ASCII file, the trace is organized in 12 fields as follows in fig shown below,
The meaning of the fields is:
1. The first field is the event type. It is given by one of four possible symbols r, +, -, d which correspond respectively to receive (at the output of the link), enqueued, dequeued, and dropped.
2. The second field gives the time at which the event occurs.
3. Gives the input node of the link at which the event occurs.
4. Gives the output node of the link at which the event occurs.
5. Gives the packet type (eg CBR or TCP)
6. Gives the packet size
7. Some flags
8. This is the flow-id (fid) of IPv6 that a user can set for each flow at the input OTcl script
one can further use this field for analysis purposes; it is also used when specifying a stream
color for the NAM display.
9. This is the source address given in the form of "node. port".
10. This is the destination address, given in the same form.
11. This is the network layer protocol's packet sequence number. Even though UDP
implementations in a real network do not use sequence numbers, ns keeps track of UDP
packet sequence number for analysis purposes
12. The last field shows the unique id of the packet.
hi
ReplyDeleteHow to download your file?
ReplyDelete