Академический Документы
Профессиональный Документы
Культура Документы
By
Alfred V Aho Peter J Weinberger Brian W Kernighan
program, and gawk is gnu awk, from the Free Software Foundation. Each version is a little different. Here well confine ourselves to simple examples which should be the same for all versions. On some OSs awk is really nawk.
OPTIONS
-F field-separator -v var=val Assign var=val -f program-file
Output File
Patterns
Actions
performs the specified operation on each line, or fields of the line, that contain those patterns. You can specify the pattern matching statements for awk either on the command line, or by putting them in a file and using the -f program_file option
Input is divided into records and fields. The default record separator is <newline>, and the variable NR keeps the record count. The default field separator is whitespace, spaces and tabs, and the variable NF keeps the field count. Input field, FS, and record, RS, separators can be set at any time to match any single character. $n, where n is an integer, is used to represent the nth field of the input record, while $0 represents the entire input record.
END matching the end of input, after the last field is read Printing is allowed through the print, and formatted print, printf, statements.
Built-in Variables
ARGC The number of command line arguments ARGIND The index in ARGV ARGV Array of command line arguments The array is indexed from 0 to ARGC ENVIRON An array containing the values of the current environment e.g.,ENVIRON["HOME"]
FILENAME
FNR FS NF
NR
The name of the current input file. The input record number in the current input file. The input field separator, a space by default. The number of fields in the current input record. The total number of input records seen so far.
Input/Output Statements
getline Set $0 from next input record getline <file> Set $0 from next record of file getline var Set var from next input record print Prints the current record. print expr-list Prints expressions printf format,expr-list system (cmd-line) Execute the command cmd-line
Built-in string functions length([s]) Returns the length of the string s, or the length of $0
index(s,t)
Returns the position of string s where t first occurs, or 0 if it doesnt Returns the n-character substring of s, beginning at position m
Examines str, and returns its numeric value. Returns a str in uppercase Returns a str in lower case
substr(s,m,n)
strtonum(str) tolower(str) toupper(str)
USER-DEFINED FUNCTIONS
# Awk program to accept a number and delete the entire # line and create a new file
BEGIN { printf("\n Enter code number :"); getline code < "-"; printf("code number is %d",code); } { if ( $5 != code ) { print $0 ; }
# # # # #
awk program to make row into column and column into row ,field seperator is tab example of split function taking three parameter string , array name , field seperator
END { for(i=1 ; i <= n ; i++) { printf("\n"); for( k=1 ; k <= j ; k++) printf("%10s\t", score[i,k]); } }
# # # #
awk program to change a particular word to some other word and create a new file Example of gsub to substitute a particular word to a another word in the given string
BEGIN { print "Convert file"; print "Enter a word to change"; getline sword < "-"; print "Enter a new word to change"; getline tword < "-"; } { gsub(sword,tword,$0); print $0 }