By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,157 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,157 IT Pros & Developers. It's quick & easy.

Print all entries with status codes in the 200's

P: 5
can anyone tell me how to print out the ip address and timestamp from a weblog file for all the entries where the status code begins with 2.
Mar 10 '15 #1
Share this Question
Share on Google+
5 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Are you able to read the file? What is the structure of the file?
Mar 10 '15 #2

P: 5
sorry i should have said its an apache server log file
Mar 10 '15 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Let me repeat my questions. Are you able to read the file? What is the structure of the file (provide a sample)?
Mar 10 '15 #4

P: 5
its combined log format -

127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://www.example.com/start.html" "Mozilla/4.08 [en] (Win98; I ;Nav)"
Mar 10 '15 #5

bvdet
Expert Mod 2.5K+
P: 2,851
The information can be parsed from the given line of text like this:
Expand|Select|Wrap|Line Numbers
  1. >>> s = '127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://www.example.com/start.html" "Mozilla/4.08 [en] (Win98; I ;Nav)"'
  2. >>> ip = s.split("-")[0].strip()
  3. >>> ip
  4. '127.0.0.1'
  5. >>> timestamp = s[s.index("[")+1:s.index("]")]
  6. >>> timestamp
  7. '10/Oct/2000:13:55:36 -0700'
  8. >>> 
Mar 10 '15 #6

Post your reply

Sign in to post your reply or Sign up for a free account.