Site TIPS PAGE🔎 SEARCH  Ξ INDEX  MAIN MENU  UP ONE LEVEL
 OJB's Web Site. Version 2.4. Mac Tip.Location: tip231 tips mac owen2 
Tip

Discuss   Up to OJB's Mac Tips List

Extract Columns Using Terminal

You can extract specific columns from text from the command line (Terminal app).

Use awk to extract a column. By default, columns are defined as non-space text separated by one of more whitespace characters (spaces or tabs) but this can be modified.

Here's a simple (pointless) example:
echo '111 2 3 4444 5' | awk '{print $2}'
This returns "2"

Note 1: The "print" is an awk command which prints a string, then a new line.
Note 2: The "$2" means return the second field.

This is useful to return a particular field from another command. Example:
ls -al | awk '{print $5}'
This displays the size (column 5) of all files in the current directory.

Here's a more complex example which shows the filename and the size, separated with "=":
ls -al | awk '{for (i=9; i<=NF; i++) printf $i " "; print "= " $5}'
Because the filename can contain spaces we need to show all columns after, and including, column 9.

Note 1: This works because the file name is the last field returned by "ls".
Note 2: The variable "NF" is built into awk and stores the number of fields.
Note 3: The "printf" awk command displays text without a line break.

You can use a different column separator (instead of the default whitespace) by using the "-F" option.

Here's an example:
echo "1,2,3,4,5,666,7,888,9" | awk -F"," '{print $7 " " $5 " " $6}'
Returns "7 5 666"





I usually write a blog post about once a week. The latest post can be viewed here: Think for Yourself: In the end, everything is just an opinion. Be skeptical, and think for yourself! (posted 2026-04-25 at 21:12:02). Or go to the Home Page of My Blog to choose a post to view.

I do podcasts too! You can listen to my latest podcast, here: OJB's Podcast 2026-04-14 How Far is that Star?: How would we really know how big the universe is? Or subscribe to my podcast RSS feed, on my RSS Feeds page.

If you're not sure what to view from the thousands of pages on my site, here are some suggestions: My Latest Airshow Report (photos and movies from Warbirds Over Wanaka, 2026), My Favourite Wines and Beers (tasting notes for some of my favourites), An Interesting Astronomical Observation (learn a bit about astronomy from these observing notes), See Some Photos (A short rail journey through the Taieri Gorge), Read Some Mac Tips (Learn about how to use your Mac, and fix some problems).


Latest Site News and Notifications (Desktop): You are currently viewing OJB's web site, version 2.4 which has some major changes, and possibly errors! Please report any problems to ojb@mac.com.

 Site ©2026 by OJBOJB's BlogWhy Macs are BestMade & Served on Mac 
Site Features: Blog RSS Feeds Podcasts Feedback Log07 Jun 2024. Hits: 1,001,579,009
Description: Mac TipLogged: nothingLoad Timer: 141ms