Reputation: 383
I'm trying to get the ip addresses out of a log file on lines that include the string 'PyMongo'. This is the command I'm executing:
ruby -ne "print $1 if /from (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):.*PyMongo/" /usr/local/var/log/mongodb/mongo.log
The output looks like this:
2020-04-19T21:09:35.567-0400 I NETWORK [conn31491] received client metadata from 123.234.123.234:53016 conn31491: { driver: { name: "PyMongo", version: "3.10.1" }, os: { type: "Linux", name: "Linux", architecture: "x86_64", version: "4.9.0-12-amd64" }, platform: "CPython 3.5.3.final.0" }
I've used the same construct in irb and the code only prints the IP address. Why when run on the command line does it echo the entire input line?
Upvotes: 0
Views: 76
Reputation: 383
Thanks for your answers and comments. Just in case anyone else runs into a similar problem, I wanted to post my final solution.
ruby -ne 'print "#{$1}\n" if from (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):.*PyMongo/' /usr/local/var/log/mongodb/mongo.log
Without the string interpolation what I got was an unending string of digits and dots.
Upvotes: 0
Reputation: 626926
To avoid the shell expanding the $1
variable, you may use the command in single quotation marks:
ruby -ne 'print $1 if /from (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):.*PyMongo/' /usr/local/var/log/mongodb/mongo.log
Upvotes: 4