Awk.Info

"Cause a little auk awk
goes a long way."

About awk.info
 »  table of contents
 »  featured topics
 »  page tags


About Awk
 »  advocacy
 »  learning
 »  history
 »  Wikipedia entry
 »  mascot
Implementations
 »  Awk (rarely used)
 »  Nawk (the-one-true, old)
 »  Gawk (widely used)
 »  Mawk
 »  Xgawk (gawk + xml + ...)
 »  Spawk (SQL + awk)
 »  Jawk (Awk in Java JVM)
 »  QTawk (extensions to gawk)
 »  Runawk (a runtime tool)
 »  platform support
Coding
 »  one-liners
 »  ten-liners
 »  tips
 »  the Awk 100
Community
 »  read our blog
 »  read/write the awk wiki
 »  discussion news group

Libraries
 »  Gawk
 »  Xgawk
 »  the Lawker library
Online doc
 »  reference card
 »  cheat sheet
 »  manual pages
 »  FAQ

Reading
 »  articles
 »  books:

WHAT'S NEW?

Mar 01: Michael Sanders demos an X-windows GUI for AWK.

Mar 01: Awk100#24: A. Lahm and E. de Rinaldis' patent search, in AWK

Feb 28: Tim Menzies asks this community to write an AWK cookbook.

Feb 28: Arnold Robbins announces a new debugger for GAWK.

Feb 28: Awk100#23: Premysl Janouch offers a IRC bot, In AWK

Feb 28: Updated: the AWK FAQ

Feb 28: Tim Menzies offers a tiny content management system, in Awk.

Jan 31: Comment system added to awk.info. For example, see discussion bottom of ?keys2awk

Jan 31: Martin Cohen shows that Gawk can handle massively long strings (300 million characters).

Jan 31: The AWK FAQ is being updated. For comments/ corrections/ extensions, please mail tim@menzies.us

Jan 31: Martin Cohen finds Awk on the Android platform.

Jan 31: Aleksey Cheusov released a new version of runawk.

Jan 31: Hirofumi Saito contributes a candidate Awk mascot.

Jan 31: Michael Sanders shows how to quickly build an AWK GUI for windows.

Jan 31: Hyung-Hwan Chung offers QSE, an embeddable Awk Interpreter.

[More ...]

Bookmark and Share

categories: ,Dec,2009,KennyM,EdM

Awk's Equivalent to VI's J

A recent discussion in comp.lang.awk demonstrated a very cute, and very succinct, awk trick.

Neil Harris wanted to clean up this output:

host1name.com 
10.10.10.1 
host2name.com 
10.10.10.2 
host3name.com 
10.10.10.3 

He was using an uppercase J in vi to manually move the hostname's IP address up onto the same line as it's hostname. But he wanted to automate the task with awk.

Kenny McCormack offered:

ORS=NR%2?" ":"\n" 

(Yes, that is the whole program.)

Ed Morton offered a more elegant version:

ORS=NR%2?FS:RS 

Finally, Kenny McCormack commented:

  • I'm 98% sure that I personally invented the basic idea (ORS=... as the pattern, with no action - i.e., default action).
  • Ed's enhancement was using FS and RS instead of hardcoding space and newline. It's nice for two reasons:
    1. Saves a few golf strokes
    2. Is more "portable" (or "logical", if you look at it that way) in that if FS and RS had been assigned non-default values, they would be used.
  • Also, as he says, it is a very instructive 14 characters of AWK code.
blog comments powered by Disqus