Professional Documents
Culture Documents
Awk Introduction: Explained by Examples Rather Than by Definitions Syntax For One Line Awk Commands
Awk Introduction: Explained by Examples Rather Than by Definitions Syntax For One Line Awk Commands
Explained by examples rather than by definitions Syntax for one line awk commands
awk: awk -Fs '/search/ {action}' awkvar=$shellvar infile nawk: awk -Fs -v awkvar=$shellvar '/search/ {action}' infile gawk: awk -Fs -v awkvar=$shellvar '/search/ {action}' infile
Concept
Awk scans ascii files or standard input. It can search strings easily and then has a lot of possibilities to process the found lines and output them in the new format. It does not change the input file but sends its results onto standard output.
awk/nawk/gawk
Awk is the original awk. Nawk is new_awk and gawk the gnu_awk. The gnu_awk can do most, but is not available everywhere. So best is to use only things which nawk can do, because if that is not installed, then the system is not well anyway.
Variables
Awk does not distinguish between strings and numbers. So one may just put anything into a variable with varname = othervar or varname = "string". To get it out of the var just write it's name as a function argument or on the right side of any operator.
' infile
} END
' infile
} ' infile
mail $addr <<EOF Please dont't send me mail any more! EOF else mail $addr <<EOF Thanks for mailing me. I'll answer as soon as possible!:-)) EOF fi done }
# Compare actual address in $i with loaded address if($i == antiaddr){ # Send a negative mail system("mail " $i " < /path/badmail") # Now end the while loop break }else{ # Send a positive mail system("mail " $i " < /path/goodmail") } } } } }
} if(mytype == "boo"){
var1 *= 4 var2 += 10 var3 = cos(var3) } # Print formated output in reverse order } printf("%-4s%10.3f%10.3f%10.3f\n",mytype,var3,var2,var1)
Functions
This example substitutes the first three occurrences of "searchterm" with a different term in each case and from the fourth case it just prints the line as it is. It should show where to place a function and how to call it.
#!/usr/bin/nawk -f BEGIN{ mysub1 = "first_sub" mysub2 = "second_sub" mysub3 = "third_sub" mycount = 1 find = "searchterm" } { if($0 ~ find){ if(mycount == 1){ if(mycount == 2){ if(mycount == 3){ if(mycount > 3){
}else{ }
mycount++ print
String functions
sub(regexp,sub) sub(regexp,sub,var) gsub(regexp,sub) gsub(regexp,sub,var) split(var,arr) split(var,arr,sep) index(bigvar,smallvar) match(bigvar,expr) length(var) substr(var,num) substr(var,num1,num2) sprintf(format,vars) Substitute sub for regexp in $0 Substitute sub for regexp in var Globally substitute sub for regexp in $0 Globally substitute sub for regexp in var Split var on white space into arr Split var on white space into arr on sep as separator Find index of smallvar in bigvar Find index for regexp in bigvar Number of characters in var Extract chars from posistion num to end Extract chars from num1 through num2 Format vars to a string
When to use awk, when to use perl? Perl can do 100 times more than awk can, but awk is present on any standard unix system, where perl first has to be installed. And for short commands awk seems to be more practical. The auto split mode of perl splits into pieces called: $F[0] through $F[$#F] which is not so nice as $1 through $NF where awk retains the whole line in $0 at the same time. To get the first column of any file in awk and in perl: awk '{print $1}' infile perl -nae 'print $F[0],"\n";' infile