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 variable. as stated elsewhere, awk uses a contiguous string of spaces as the default field delimiter. FS The FS Variable: 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: {FS=”|”} This is an alternative to the option which does the same thing. –F when you used the print statement with comma-separated arguments, each argument was The OFS Variable: 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: { OFS=”~” } When you reassign this variable with a ~ awk will use this character for delimiting the print arguments. This is a useful variable for creating lines with delimited fields. NF comes in quite handy for cleaning up a database of lines that don’t contain the right number The NF variable: 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 {FS = ‘BEGIN “|”} NF! =6 { Print No NR, empx.lst “Record “, “has”, “fields”}’ Dept of ISE, CIT, Gubbi Page no 10 Part-A Networks Lab 2015-16 THREE NODE POINT TO POINT NETWORK Experiment No: Date: 1
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 variable. 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: {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: { OFS=”~” } When you reassign this variable with a ~ 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 Dept of ISE, CIT, Gubbi Page no 10 Networks Lab Part-A 2015-16 Experiment No: 1 THREE NODE POINT TO POINT NETWORK Date:
ISE-VINAY-NET.docx