Вы находитесь на странице: 1из 20

AWK

AWK Programming Language

By
Alfred V Aho Peter J Weinberger Brian W Kernighan

Pattern Scanning and Processing Language

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.

nawk is new awk, a newer version of the

Syntax: awk [ options ] <program-text> -f <program-file> <Input file ...>

OPTIONS
-F field-separator -v var=val Assign var=val -f program-file

Input File Awk Gawk nawk

Output File

Patterns

Actions

Awk Script File

/pattern/ { action statements }

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

awk searches its input for patterns and

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.

Format of Awk Program File BEGIN { Statement/s; } { Statement/s; } END { Statement/s; }

BEGIN and END are special patterns


BEGIN matching the beginning of input, before the first field is read

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

Special File Names


/dev/stdin /dev/stdout /dev/stderr /dev/fd/n The standard input The standard output The standard error output The file associated with open file descriptor n

For example : print "You blew it!" > "/dev/stderr"

Built-in arithmetic functions


exp(expr) int(expr) rand() The exponential function Truncates to integer Returns a random number between 0 and 1 srand([expr]) Uses expr as a new seed for the random number generator sqrt(expr) The square root function

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

Functions in AWK are defined as follows: function name(parameter list) { statements }

# 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

BEGIN { print "Table conversion"; j = 0; }

n = split($0,col,"\t"); j++; for ( i = 1 ; i <= n ; i++ ) { score[i,j] = col[i]; }

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 }

Вам также может понравиться