There are a lot more people switching to a unix based operating system(mainly OS X or Ubuntu) and wonder why you would ever want to use the command line. The answer is simple working through the commandline can get simple tasks done much much quicker, this is mainly due to ‘|’ also known as pipe. Think of pipe as saying “do this”, also if you can get it done through the commandline you can just put the code you used and keep it for later use or maybe run it against all the machines you own. For each example I will be going to ‘cat’ the file ‘hostinfo.txt’ and run some pipes, all cat does is displays it. The hostinfo.txt file looks like…

Server1 192.168.1.1 00:0A:E6:B3:A5:A0
Server2 192.168.1.2 00:0A:E6:B3:A5:A1
Client1 192.168.1.10 00:0A:E6:B3:B5:C3
Client2 192.168.1.11 00:0A:E6:B3:A5:D1

grep

  • “Search for and show me”
  • cat hostinfo.txt | grep Server
    • Server1 192.168.1.1 00:0A:E6:B3:A5:A0
      Server2 192.168.1.2 00:0A:E6:B3:A5:A1

awk, gawk, or nawk

  • “Show me this part”
  • All three are virtually the same but you may not have all of them. So find the one you do have and look at the man page for it to make sure it uses the same syntax as the one your use to, I personally prefer gawk.
  • cat hostinfo.txt | gawk ‘{print $2}’ … the Print $2 is saying get me the second arguement(similiar to word, easiest way to learn this is to play around with the print till it grabs what you want. You’ll get the hang of it quickly.
    • 192.168.1.1
      192.168.1.2
      192.168.1.10
      192.168.1.11

tee

  • “Save what you showed me to this file.”
  • Very usefull for those installers you want to look over what options you actually chose, or in conjunction with sed(to be shown later).
  • cat hostinfo.txt | tee hostinfo.bak.
    • This will copy everything in hostinfo.txt word for word(since it wasn’t used with a nother pipe, very usefull to combine this with another pipe)

sed

  • “Find this and replace it with this”
  • You will want to read up on this one, it looks tricky but is very very simple.
  • Can be used in vi
  • Very usefull when combined with another pipe, I am even going to combine it with tee in my example!
  • cat hostinfo.txt | sed s/192.168.1/10.10.10/g | tee hostinfo.new.txt … I had changed all the ip’s and wanted to update my file to show it.
    • Server1 10.10.10.1 00:0A:E6:B3:A5:A0
      Server2 10.10.10.2 00:0A:E6:B3:A5:A1
      Client1 10.10.10.10 00:0A:E6:B3:B5:C3
      Client2 10.10.10.11 00:0A:E6:B3:A5:D1

  • This also saved it to hostinfo.new.txt

arg, xarg

  • Do this
  • cat hostinfo.txt | awk ‘{print $2}’ | xargs -n 1 ping -c 1 | grep Unreachable
    • This would send 1 ping request(due to xargs) to each of the IP’s(due to the gawk), and only show the ones that failed(due to grep).
One Response to “Why Pipes Rock”
  1. server software - Page 2 - Tutorial Ninjas says:

    [...] the power of the commandline, i wrote up a tutorial so check it out: Tutorial Ninjas » Blog Archive » Why Pipes Rock ‘ls | xargs -n 1 du -lh | grep -v .conf’ - will also go into folders(filtering out .conf) or you [...]

Leave a Reply