Unix Tip: Printing in columns
Send your Unix questions today!
See additional Unix tips and tricks
Regardless of how sophisticated Unix has become, one of its ever-endearing qualities is the ease with which the command line can be used to select, manipulate and display data. Clever little languages like sed and awk continue to remind me why it was so much fun to sit down with the early O'Reilly books, trying each new little "trick" that they taught me and discovering just how much processing I could do via simple scripts. And, while I rarely write awk scripts these days, there are occasions in which it is the perfect tool for the task. In this week's column, we're going to look at a couple tools for creating columnar output from a list of strings -- a simple task, but one that I hate to do "by hand".
In the first of two scripts, data arriving in a list such as this:
| apricot banana chick peas dill pickle egg french fries grapes hot dogs ice cream jello ketchup lemon melon |
would be processed to look like this:
| apricot | banana | chick peas |
| dill pickle | eggs | french fries |
| grapes | hot dogs | ice cream |
| jello | ketchup | lemon |
| melon |
This rearrangement of data can be helpful if, for example, you are including a list of file names or numbers in a text file and want to include multiple items in each row to shorten the length of the inserted text.
For anyone who hasn't written awk scripts. it's useful to know that awk processes one line of input at a time and that it automatically assigns each string on a line to the variables $1, $2 and so on ($0 represents the entire line of input).
Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world
jfruh
Apple syncing patent can't come soon enough
pasmith
New Twitter features borrow from 3rd party clients
Esther Schindler
Open Source Changes the Software Acquisition Process
mikelgan
How to set up continuous podcast play on the new iTunes
David Strom
Five important Windows 7 mobility features
sjvn
Guard your Wi-Fi for your own sake
Sandra Henry-Stocker
Grepping on Whole Words
Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325
Join the conversation here
Quick, practical advice for IT pros. Made fresh daily.
Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.












