An Awk Primer
en.wikibooks.org
en.wikibooks.org
1. http://cm.bell-labs.com/cm/cs/awkbook/ 2. https://www.goodreads.com/book/show/703101.The_awk_Programmi...
Obviously people pick this up on the job but not everyone who works with Unix terminals has a fully fledged developer/sys-admin role. My 'bag-of-tricks' evolves at a snail's pace.
"The 3-finger Claw Technique" The sweetest 3 functions, ever.
shout() { echo "$0: $*" >&2; }
barf() { shout "$*"; exit 111; }
safe() { "$@" || barf "Cannot $*"; }
and always,
exit 0I use these daily, they're extremely cross-platform friendly in any bourne-derived shell I've used in the last 4 years.
safexample.sh :
--
#!/bin/sh
shout() { echo "$0: $*" >&2; }
barf() { shout "$*"; exit 111; }
safe() { "$@" || barf "cannot $*"; }
# consider the following lines
safe cd /some/dir
safe tar xzvfp /my/big/tarball.tbz
exit 0
--
In the above example, using 'safe' suddenly gives the user the following special powers:1. if `/some/dir` does not exist, the script will safely exit before that un-tarring does any damage.
2. the actual 'directory does not exist' will return to stderr for the script, (as opposed to just telling us the useless fact that the enclosing script failed). Now bourne shell starts behaving like a modern language! (ala Python/Ruby tracebacks, Perl errors, etc…)
Additionally, if you 'exit 0' at the end, you can run non-safe operations, and always be guaranteed that if the shell exits, it was 'complete'. An example:
# consider the following lines
safe cd /some/dir
tar xzvfp /my/big/tarball.tbz
Now, safe was removed from the un-tar command, right? So, imaging tarballs created by people using a Mac (with HFS+ and some filesystem-specific sticky bit somewhere in the filesystem which was tar'd up). Now, you un-tar it on some BSD or other NIX box, and tar complains as it goes- and exits non-zero. One way to handle this, is to consider the tar 'reliable', and not use safe for it.Now, if we continue to be conscious of this simple safe/notsafe distinction with these scripts, they can be called by other safe scripts- and behave just like any respectable UNIX program!
Now, consider this crontab entry:
1 3 * * * someuser flock -k /tmp/safexample.lock /path/to/safexample.sh
If it fails, (for bad perms, no tarball to unpack, etc…) cron will actually have a reasonable message to email/log on failure!Thanks to the noble efforts of the Clan of the White Lotus, these 3 functions originate during the late thirteenth century. The original author, William Baxter, explained that these three functions took 15 years to boil down to what they are- I believe that after mercilessly abusing them for several years myself…
Below is another example with more bits.
#!/bin/sh
shout() { echo "$0: $*" >&2; }
barf() { shout "$*"; exit 111; }
safe() { "$@" || barf "cannot $*"; }
for i in "before $@ after"
do
echo "arg <$i>"
done
for i in "before $* after"
do
echo "arg <$i>"
done
safe echo "this is ok"
safe echo "this is ok, too" || safe echo "so is this"
safe bad_echo "this is bad"
exit 0edit: or the way more useful entire nifty(1) gmane-thread http://thread.gmane.org/gmane.org.user-groups.bsd.nycbug/871...
Try writing "set -eu" as the first line of your shell scripts. It's sort of like compiling with "-Wall -Werror"--not the default but perhaps should have been.
Parts of it are dated now (it's ... gasp, 22 years since its first edition), but since it's one of those books based on UNIX philosophy it's aged quite well.
It, Linux in a Nutshell, the Sed & AWK book, are still a pretty good introduction.
Perl 5 adds variable scoping, pointers, modules, OOP and a bunch of powerful expressive stuff and abstractions -- but if you learn awk on top of what you've already got it means you're about 90% of the way to introductory Perl proficiency, and you can add the advanced stuff later.
So by all means learn awk! Just remember that it's not suitable for really big jobs -- if you need to create awk scripts that go beyond a couple of dozen lines of code, that's when you'll probably want to upgrade to Perl.
Life is good.
Where awk really shines is situations that call for a set of record based production rules, and if in fact your problem is in that domain, you could write hundreds or even thousands of rules without the problem becoming unsuitable for awk. Like for any domain specific language, you could consider the limitations imposed by awk as valuable discipline to keep you in the problem domain.
While perl can be written in an awk-like style, it's really much more of a general purpose language, so it can accommodate workflows that deviate substantially from the "sequentially process records" style that awk excels at.
Once you've tried Perl you don't want to flip back and forth to awk because the inconsistencies in syntax (within and to each other) will drive you mad.
Even when I think in sed or awk, I'll generally write in Perl; or, if I have an old awk script I'll use a2p to convert it.
a2p (awk to perl), s2p (sed to perl) and find2perl (find to perl) are all excellent ways to get you into Perl.
Nowadays my preferred language for almost anything is Python, but I still find myself writing Perl for some throwaways.
http://www.grymoire.com/Unix/Awk.html
Also, check out the sed tutorial while you are there.
I'm forcing myself to use Python for SysAdmin tasks because I like it so much.
Here's backticks in Python, pretty much a copy n paste from the docs.
def backtick(cmd):
return subprocess.Popen(shlex.split(cmd),
stdout=subprocess.PIPE).communicate()[0]This seems unwise.
Use the best tool for a given job.
And folks tend make too much of the differences between languages.