Ruby one-liner: print only the matching part of a string

Sometimes, you have to extract information from a log file or similar. This is where Ruby's command line parameters -n / -p are very convenient.

Our example input file will look like this:

2014-01-31 13:00:49    "Missing author for experiment ""FS_1"", row ""176""
2014-01-31 14:05:51    "Missing date for experiment ""FS_2"", row ""32""


Simple grep replacement

To get only lines containing "author", you can use

ruby -pe 'next unless $_ =~ /author/'

Output

2014-01-31 13:00:49    "Missing author for experiment ""FS_1"", row ""176""

Explanation 

 -p instructs Ruby to put your script inside "while gets; ...; print; end". You can access the current line with $.

Transforming the input with regular expressions

To extract the information  for those lines where the author is missing, you can use
ruby -ne 'md = /Missing author for experiment ""(1+)"", row ""(1+)""/.match($
); puts md[2] + "," + md[1] if md' input.txt

Output:

176,FS_1

Explanation

 -n instructs Ruby to put your script inside "while gets; ...; end". You can access the current line with $_.

Links

http://ruby-doc.com/docs/ProgrammingRuby/ Online version of the original pickaxe (for Ruby 1.6), see section "Ruby and Its World" for a list of Ruby command-line arguments


276 Words

2014-01-31T04:50:00-08:00