.. highlight:: console .. role:: bash(code) :language: bash :class: highlight Pipes ----- Earlier, we mentioned the possibility of redirecting output to a file. Another very powerful redirection method are :dfn:`pipes`, used like this:: $ cmd1 | cmd2 | cmd3 | cmd4 Instead of redirecting the output of :command:`cmd1` to a file, it will be used directly as input to :command:`cmd2` and so on. sort, grep, sed, awk, uniq, head, tail ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In this section, you'll see some commands that are useful in the middle of pipes. Remember to use the man pages to find options! #. Compare:: $ echo -e "d\nb\nc\na" $ echo -e "d\nb\nc\na" | sort #. Sort these lines by *numerical* value:: $ echo -e "3\n1\n2\n12" #. Copy the file :file:`/usr/share/dict/words` to your working directory, then use :command:`grep` to find out which words contain the string ``physics``:: $ grep physics words #. Alternatively, :command:`grep` can also be used as part of a pipe:: $ cat words | grep physics #. How many entries in :file:`words` do not contain ``physics``? Let :command:`grep` do the count for you. #. Create a file called :file:`physicswords` that contains the *physics* words twice. Run :command:`sort` on it. Try inserting :command:`uniq` into the pipe before or after the sort. #. Try:: $ sort physicswords | uniq | head -3 Do the same with :bash:`tail -3`. #. :command:`sed` is a very powerful command, and worth spending more time on. It is most often used for text replacement using the ``s///`` construct:: $ cat physicswords | sed s/physics/foo/ #. A similarly comprehensive command is :command:`awk`. Again, we can only show some common uses. Compare:: $ ls -l ~ $ ls -l ~ | awk '{print $1 " abc " $3 " def " $5}' What is happening? Also try:: $ ls -l ~ | awk '{ sum+=$5 ; print sum } END { print "===\ntotal: "sum }' If you have any PDF files in :file:`~`, also try matching specific lines:: $ ls -l ~ | awk '/pdf/ { sum+=$5 ; print sum } END { print "===\ntotal: "sum }' #. List the three oldest files in your home directory. #. Use :command:`ls`, :command:`grep`, :command:`sort`, :command:`head` and :command:`awk` to find the five largest files you have. Check all your directories, not just :file:`~`, do not include directory sizes! The output should list only the file names, one per line, largest first. If you feel up to a challenge, try :command:`find` instead of :command:`ls` and :command:`grep`. date, cal ~~~~~~~~~ Two little useful tools to close this block (they fit equally badly anywhere else):: $ date $ cal