Pipelining

Julia defines a pipeline function to redirect the output of a command as the input to the following command:

run(pipeline(`cat $file`,"test.txt"))  

This writes the contents of the file referred to by $file into test.txt, which is shown as follows:

run(pipeline("test.txt",`cat`)) 

This pipeline function can even take several successive commands, as follows:

run(pipeline(`echo $("\nhi\nJulia")`,`cat`,`grep -n J`)) #>        3:Julia

If the file tosort.txt contains B, A, and C on consecutive lines, then the following command will sort the lines:

run(pipeline(`cat "tosort.txt"`,`sort`))  # returns A B C 

Another example is to search for the word is in all the text files in the current folder. Use the following command:

run(`grep is $(readdir())`) ...

Get Julia 1.0 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.