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 $$
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- New Products
- Trying to Tame the Tablet
- Paranoid Penguin - Building a Secure Squid Web Proxy, Part IV
- Developer Poll
- Looking Good
1 hour 49 min ago - Hey God - You may not be
6 hours 2 min ago - Reply to comment | Linux Journal
8 hours 35 min ago - Drupal is an Awesome CMS and a Crappy development framework
13 hours 14 min ago - IT industry leaders
15 hours 37 min ago - Reply to comment | Linux Journal
1 day 8 hours ago - Reply to comment | Linux Journal
1 day 10 hours ago - Reply to comment | Linux Journal
1 day 12 hours ago - great post
1 day 12 hours ago - Google Docs
1 day 13 hours ago
Enter to Win an Adafruit Prototyping Pi Plate 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 Prototyping Pi Plate 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
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.



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.