AWK - AHO KERNIGHAN AND WEINBERGER PATTERN SCANNING LANGUAGE
awk (and its relatives gawk and nawk) is a pattern scanning and processing language that goes one step beyond grep; not only does it match, but it processes too. This means its a really powerful tool (and quick and easy too) for reformmating and reporting on data files.
Let's see an example. I would like to find all of my people who have Python skills in our sample data file:
[trainee@pasanda shell]$ awk /Python/ requests.txt
hazel PHP Python Perl Ruby ASP
leane PHP Python ASP Perl Java
olivia MySQL Python ASP PHP
adam Tcl/Tk Perl Python MySQL
barry Python XML Java Perl PHP
harry PHP Python Java
ken Tcl/Tk Python Java Perl
nigel PHP Python Java Perl
rupert Java Python MySQL
[trainee@pasanda shell]$
That's done just what a grep would do, but I can add a specification that I want to manipulate my output "just printing the names" I can do so:
[trainee@pasanda shell]$ awk '/Python/{print $1}' requests.txt
hazel
leane
olivia
adam
barry
harry
ken
nigel
rupert
[trainee@pasanda shell]$
So that's "match a regular expression, and if it matches output as follows". Awk takes a series of pattern and operation statements and you'll usually wrap them in single quotes to protect them from the shell. Let's do something a bit more interesting:
[trainee@pasanda shell]$ awk 'BEGIN{print "Python Skills"; count=0};
/Python/{count++; printf "%3d %-10s mainly %s\n",count,$1,$2;}'
requests.txt
Python Skills
1 hazel mainly PHP
2 leane mainly PHP
3 olivia mainly MySQL
4 adam mainly Tcl/Tk
5 barry mainly Python
6 harry mainly PHP
7 ken mainly Tcl/Tk
8 nigel mainly PHP
9 rupert mainly Java
[trainee@pasanda shell]$
Here, the block marked BEGIN is run before any lines of input are read (a block marked END could also have been provided) and then each line of the incoming data is checked against the pattern and a block is performed if it matches. You can specify multiple pattern and match blocks if you wish; awk is a complete language. Rather than write long and complex instructions, you can save your awk program into a file:
[trainee@pasanda shell]$ awk -f demo.awk requests.txt
Python Skills
1 hazel mainly PHP
2 leane mainly PHP
3 olivia mainly MySQL
4 adam mainly Tcl/Tk
5 barry mainly Python
6 harry mainly PHP
7 ken mainly Tcl/Tk
8 nigel mainly PHP
9 rupert mainly Java
Do not consider: antonia barbara cherry delia ethel florence
gloria iris jenny kerry margaret nina petra queenie rita
sally tina uva venus wendy xena yollanda zoe charles david
ed fred graham ivan john len morris orpheus peter quentin
steve tommy ulsyees victor william xavier yuri zachary
9 staff out of 52 have Python skills
[trainee@pasanda shell]$
Here's the complete awk script:
BEGIN{print "Python Skills"; yes=0; no=0; pass=""};
END{print "Do not consider:",pass;}
END{print yes,"staff out of",yes+no,"have Python skills"};
{
if (/Python/) {
yes++;
printf "%3d %-10s mainly %s\n",yes,$1,$2;
} else {
no++;
pass = sprintf("%s %s",pass,$1);
}
}
See also
Linux Basics Course
Please note that articles in this section of our
web site were current and correct to the best of our ability when published,
but by the nature of our business may go out of date quite quickly. The
quoting of a price, contract term or any other information in this area of
our website is NOT an offer to supply now on those terms - please check
back via
our main web site
Web Application Deployment - Linux Utilities [63] - ()
[71] - ()
[1361] - ()
[1366] - ()
[1690] - ()
[2145] - ()
[2320] - ()
[2484] - ()
[2638] - ()
[3446] - ()
[3764] - ()
[3902] - ()
[4586] - ()
[4682] - ()
Web Application Deployment - Linux -An Introduction For Users [73] - ()
[74] - ()
[152] - ()
[249] - ()
[430] - ()
[431] - ()
[593] - ()
[659] - ()
[679] - ()
[703] - ()
[710] - ()
[711] - ()
[749] - ()
[984] - ()
[1012] - ()
[1013] - ()
[1068] - ()
[1259] - ()
[1287] - ()
[1288] - ()
[1366] - ()
[1408] - ()
[1438] - ()
[1527] - ()
[1651] - ()
[1764] - ()
[1803] - ()
[1893] - ()
[1897] - ()
[1902] - ()
[1904] - ()
[2201] - ()
[2203] - ()
[2299] - ()
[2300] - ()
[2479] - ()
[2494] - ()
[2636] - ()
[2831] - ()
[3179] - ()
[3256] - ()
[3791] - ()
[3819] - ()
resource index - Deployment
Solutions centre home page
You'll find shorter technical items at
The Horse's Mouth and
delegate's questions answered at
the
Opentalk forum.
At Well House Consultants, we provide
training courses on
subjects such as Ruby, Lua, Perl, Python, Linux, C, C++,
Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer)
many questions, and answers to those which are of general
interest are published in this area of our site.