Home ›
Tech Tip: Fun With Gawk
Feb 15, 2010 By James Hinnant
in
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 $$
______________________
The Magazine
Linux Journal is the premier source for how-tos, projects, product reviews, expert advice and opinions for everything Linux.
| Linux Mint Debian Edition Released | Sep 08, 2010 |
| Spotlight on Linux: Zenwalk Linux 6.4 "Live" | Sep 08, 2010 |
| Old Generals Never Die - They just Wear a Red Hat | Sep 07, 2010 |
| Alien - Use Any Package On Any Distribution | Sep 07, 2010 |
| Clonezilla Live | Sep 03, 2010 |
| No Steam for Linux - Right Now | Sep 02, 2010 |
- Boot with GRUB
- Linux Mint Debian Edition Released
- New Issue
- Chess Software for Linux
- Old Generals Never Die - They just Wear a Red Hat
- Building a Two-Node Linux Cluster with Heartbeat
- Alien - Use Any Package On Any Distribution
- Spotlight on Linux: Zenwalk Linux 6.4 "Live"
- VLANs on Linux
- Writing a Simple USB Driver
- Sneak Peek
19 min 43 sec ago - just want to get to windows!!
1 hour 43 min ago - Author's Comment
4 hours 41 min ago - Java executable jar to create xml file
4 hours 52 min ago - real economy
5 hours 47 min ago - I cannot agree with you
7 hours 14 min ago - Elite Army division?
8 hours 3 min ago - Quote by Groucho Marx
10 hours 46 min ago - Get a legal high with brainwave entrainment
10 hours 55 min ago - Correction.
10 hours 57 min ago











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.
Post new comment