Writing to Other Processes

In Chapter 3, Shell One-Liners, we looked at how powerful chaining processes together in pipelines could be. The chapter culminated in our mixing Ruby with other Unix commands to process text in an ad hoc fashion, with multiple processes linked together in a pipeline chain. Each took some text as input and then passed it along, in some transformed form, as output to the next process.

Here, for example, we use Ruby to extract only those rows of a file that we’re interested in, and then use sort, uniq, and head to further process those lines:

 
$ ​cat​​ ​​error_log​​ ​​\
 
​​|​​ ​​ruby​​ ​​-ne​​ ​​'$_ =~ /^\[.+\] \[error\] (.*)$/ and puts $1'​​ ​​\
 
​​|​​ ​​sort​​ ​​|​​ ​​uniq​​ ​​-c​​ ​​|​​ ​​sort​​ ​​-rn​​

Get Text Processing with Ruby now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.