O'Reilly logo

Text Processing with Ruby by Rob Miller

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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​​

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required