Print Selected Info From Terminal Commands 3


Print Selected Info From Terminal Commands


I like using Terminal a lot. So much so I have a word document full of tricks and tips. Most I don’t use very often, but the ones I do use I will eventually end up sharing with you. As you develop scripts and programs using the bash Terminal, you may only want to select certain parts of a commands output. In my previous tutorial I used this command extensively. For example cutting certain pieces of information out of the hostname command. This is very useful to cut down on clutter. It doesn’t take long to implement. This sort of command is not designed for everyday use. Mostly in scripts.

Single Line Outputs

For this tutorial we are going to use the Terminal command “host”. It is designed to list the host name of any IP address. This sort of command is useful for this tutorial, since the output is already quite small and on one line. If you have multi-line outputs you need to use the second half of this post.

If you type in Terminal


It should give you something like this. domain name pointer

This shows the IP address of this server and the resulting hostname. For this mini section I am going to cut out the very end.

Instead of writing just host name you would, in your script, write:

host | awk '{print $5}'

The second part of the command is what we are interested in. The program is called awk. Otherwise known as “pattern-directed scanning and processing language”. Basically it is a search and retrieve function. Although it does have hundreds of functions and the abilty to bend it to do what you want, it can do something very simple.

When looking through the command the vertical line (|) is designed so the commands are run together. I do not fully understand the meaning the the character but it enables use to do things at the same time. The awk section is out second program and command. The final section “{print $5}” tells the awk command to search through and print the fifth output. You may see the dollar sign ($) in Terminal before. The different outputs are usually separated by spaces. As a result it looks through the output and finds the fifth piece. domain name pointer

The first piece is “” = {print $1}, second domain = {print $2} so on and so forth. Unless you use this command regularly it may need a bit of trial and error. But once it works you can get some really “professional” results. You can of course combine them by placing more than one separated by a comma.

Multi-Line Results

If you have a piece of output from a command that has multi-line, i.e not like the example above you will need to search through the output. For example type in Terminal:

ifconfig en0

It has a lot of output data. You would not, very easily, be able to use the previous type of command. But if you add in what you want to search for in forward slashes, you can extract extra data.

ifconfig en0 | awk '/inet / {print $2}'

This means that awk will search through the document find “inet” and then print the results. Again this will use trial and error. You will have to play around with the number and possibly the space after “inet”, I get different results if it is in there or not.


This post is by no means perfect or conclusive. There is probably better ways and easier ways to get the same results. I decided to post this up due to the small numbers of emails from users contacting me about there scripts they are building. It was just an trick I had and used, and I decided to share it with you.

If you want to take your skills with Terminal a bit further I recommend you check out the Terminal Category on this site. If you fancy reading a book there is a couple on Amazon that I regularly see mentioned and recommend, O’reilly Unix Geeks and Unix Under the Hood both are designed for Mac OS X and take Terminal further.

If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

  • Subscribe To Mac Tricks And Tips