Tech Tip: Fun With Gawk
When grep and sed aren't enough, gawk may provide the extra horsepower that you need. The following tip contains a sampling of some of the things one might do with gawk.
Extract the last column from a text file, whitespace-separated:
cat myfile | gawk '{print $NF}'
or:
gawk '{print $NF}' myfile
List counts of files owned by each user in the current directory:
/bin/ls -l | \
gawk 'NR > 1 {counts[$3]++;}
END {for (s in counts) {
printf(" %-15s : % 5d\n",
s, counts[s]);}}' | \
sort
Kill your processes (one use is to kill a hung login if you can remotely log in to the workstation from another machine):
ps -elf | \
gawk -v me="$USER" '$3 == me {print $4}' | \
egrep -v $$ | \
xargs -i@@ kill -9 @@; kill -9 $$
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Designing Electronics with Linux
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- What's the tweeting protocol?
- Kernel Problem
6 hours 20 min ago - BASH script to log IPs on public web server
10 hours 47 min ago - DynDNS
14 hours 23 min ago - Reply to comment | Linux Journal
14 hours 55 min ago - All the articles you talked
17 hours 19 min ago - All the articles you talked
17 hours 22 min ago - All the articles you talked
17 hours 23 min ago - myip
21 hours 48 min ago - Keeping track of IP address
23 hours 39 min ago - Roll your own dynamic dns
1 day 4 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
thanks
Thanks for the tips, especially pkill (which is new with Solaris 7; 1998! :) ).
The file count routine I use for counting source files in the current directory, so strange filenames are not an issue (also I find find to be kind of slow).
Here is the online manual for gawk: http://www.gnu.org/software/gawk/manual/
Here is a little program people might remember coding from FORTRAN class; maybe it can help put the baby to sleep:
echo 100 1 10 0.01 | gawk '{amp = $1; freq = $2; cycles = $3; tdelta = $4; for (i = 0; i <= 2 * 3.14159 * cycles; i += 2 * 3.14159 / 360) {printf("%*s\n", int((amp/2.0) * sin(i * freq) + (amp/2.0)), "*"); system("sleep " tdelta); }}'
What does this have to do
What does this have to do with Fortran? I use Fortran every day, and have no idea what you're talking about. Also, I was born in '84, long after punch cards and FORTRAN IV etc., which may explain my confusion.
don't parse output of ls
Too many odd things can be in file names, spaces, newlines, etc.
It's a rare case that all you want is a list of file names. In most cases you want to do something to the files based on their names, and if that's the case, 'find' can be a better tool, or maybe ' stat -c %n [glob]'.
Start learning basic unix commands before playing with awk
Sorry, but there is no need to use (g)awk here. I really like (g)awk, but your examples are just poor. Ok, maybe first one is a typical case for awk.
You forgot about dot files, but second one can be replaced with:
ls -Al | sed '1d;s/\([^ \t]\+[ \t]\+\)\{2\}//;s/[ \t].*//' | sort | uniq -cor even better:
Third one (really badly and also wrongly implemented) is a perfect case for pkill:
I can agree that your second example might be faster than mine, but it's irrelevant here.
Regards.