####### | Intro to Piping | #####E Heppenheimer, Summer 2018 ##If your coding habits are similar to mine, you probably generate a lot of intermediate file ##I've known there was a way around this, but until recently I lacked the confidence to use it ##Turns out its actually not so hard! ##the basic gist is that one can use a "pipe" (|) in place of an output file to use the output of one command as input for the next ##########For example: ##Keep only entries for which column 9=6 and then print column 5 #in the old days, I would have done this in three steps: $ $9 == 2' inputfile > tempfile $ awk '{print $5}' tempfile > outfile $ rm tempfile #I can condense this to a single line with the use of a pipe | $ awk '$9 == 2' inputfile| awk '{print $5}' > outfile ##But it doesn't have to stop there: ##say I also wanted to sort the outfile: awk '$9 == 2' inputfile| awk '{print $5}'| sort > outfile ####This is also useful for some basic commands to, like counting the number of files in a directory $ ls /directory/path| wc -l #### And many other examples that mitigate the need to write intermediate files #########One other thing to note about pipes is that they can be used within a grep commands to mean "or" ##For example, this will retain any entry with the word Chromosome OR the word position. grep -E 'Chromosome|Position' infile > outfile ##Now that you know, stop writing so many intermediate files!