• Embed Doc
  • Readcast
  • Collections
  • 1
    CommentGo Back
Download
 
 Awk Introduction
 Explained by examples rather than by definitions Syntax for one line awk commands
awk: awk -Fs '/search/ {action}' awkvar=$shellvarinfilenawk: awk -Fs -v awkvar=$shellvar '/search/ {action}'infilegawk: 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 o possibilities to process the found lines and output them in the new format. It does notchange 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 domost, 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.
 Search and Action
Searching happens within "//" and actions within "{}". The main action is to print.Reprint all:
awk '{print}' infile
Print lines that contain "love":
awk '/love/ { print }' infile
Print first entry in lines that contain "money":
awk '/money/ { print $1 }' infile
Variables
Awk does not distinguish between strings and numbers. So one may just put anything intoa variable with varname = othervar or varname = "string". To get it out of the var justwrite it's name as a function argument or on the right side of any operator.
 
 Multiline awk in a shell script 
All between '' is in awk. With a=$var awk get's a shell variable.The action is to print variable a and put it into a file.
awk 'BEGIN { print a > "testfile" }' a=$var
 BEGIN { }, { } and end { }
An awk script can have three types of blocks. One of them must be there. The BEGIN{} block is processed before the file is checked. The {} block runs for every line of inputand the END{} block is processed after the final line of the input file.
awk 'BEGIN { myvalue = 1700 }/debt/ { myvalue -= $4 }/want/ { myvalue += $4 }END { print myvalue }' infile
 Match in a particular field 
Awk auto splits a line on whitespace as default. The fields are stored in $1 through $NFand the whole line is in $0. One can match or not match an individual field.
awk '$1 ~ /fred/ && $4 !~ /ok/ {print "Fred has not yet paid $3"}' infile
For, If, substr()
Awk can do for() loops like in c++ and has the normal if and while structures. In NR iscurrent line number and in NF the number of fields on the current line.
awk 'BEGIN { count = 0 }/myline/ {for(i=1;i<=NF;i++){if(substr($i,3,2) == "ae"){bla = "Found it on line: "print bla NR " in field: " i}}}END { print "Found " count " instances of it" }
 
' infile
Turn around each word in a file:
awk '{ for(i=1;i<=NF;i++){len = length($i)for(j=len;j>0;j--){char = substr($i,j,1)tmp = tmp char}$i = tmptmp = ""} print}' infile
 Awk scripts within a shell script 
Extract email addresses from incoming mail. The mail would be guided to the followingscript from within the ~/.forward file. This is not an efficient method, but only anexample to show serial processing of text. The next example will do the same thingwithin awk only and will be efficient. The mail comes in over standard input into thescript.Between the commands there must be a pipe "|". For continuing on the next line oneneeds a "\" behind the pipe to escape the invisible newline.
#!/usr/bin/ksh{ while read line;doprint - "$line"done } |\tee -a /path/mymailfile |\
awk '/^From/ || /^Replay/ {for(i=1;i<=NF;i++){if($i ~ /@/){print $i}}}' |\
sed 's/[<>]//g;s/[()]//g;s/"//g;...more substitutions for really extracting the email only...' |\{ while read addr;doif [[ $(grep -c $addr /path/antimailfile) -gt 0 ]];then
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...