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 useruby -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 useruby -ne 'md = /Missing author for experiment ""(1+)"", row ""(1+)""/.match($); puts md[2] + "," + md[1] if md' input.txt