#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    512
    Rep Power
    50

    Array Element to Variable


    My logfile is:
    Code:
    192.168.30.30   2018-03-15 09:23:57.048936      inactive        Power Off
    192.168.30.30   2018-03-15 09:23:57.048936      rebooting       Power On
    
    192.168.30.27   2018-03-12 09:23:57.048936      inactive        Power Off
    192.168.30.27   2018-03-12 09:23:57.048936      rebooting       Power On
    
    192.168.30.35   2018-03-10 09:23:57.048936      inactive        Power Off
    192.168.30.35   2018-03-10 09:23:57.048936      rebooting       Power On
    
    192.168.30.31   2018-03-15 09:23:57.048933      inactive        Power Off
    I read the log line by line, split it at the tabs and put it into an array list called lines:
    Code:
      with open(logfile) as f:
        for line in f:
           a += 1
          if a > 2:
            line = line.rstrip('\n')
            line = line.split('\t')
            lines.append(line)
          else:
            line = line.rstrip('\n')
            header.append(line)
      
    
      file.close
    I then sort it on the IP Addresses with:
    Code:
    lines = sorted(lines, key=lambda x: x[0])
    Which gives me:
    Code:
    for s in lines: print s
    
    ['']
    ['']
    ['']
    ['192.168.30.27', '2018-03-12 09:23:57.048936', 'inactive', 'Power Off']
    ['192.168.30.27', '2018-03-12 09:23:57.048936', 'rebooting', 'Power On']
    ['192.168.30.30', '2018-03-15 09:23:57.048936', 'inactive', 'Power Off']
    ['192.168.30.30', '2018-03-15 09:23:57.048936', 'rebooting', 'Power On']
    ['192.168.30.31', '2018-03-15 09:23:57.048933', 'inactive', 'Power Off']
    ['192.168.30.35', '2018-03-10 09:23:57.048936', 'inactive', 'Power Off']
    ['192.168.30.35', '2018-03-10 09:23:57.048936', 'rebooting', 'Power On']
    I can sort the lines on the IP address correctly, but now I need to put the IP's into a variable and nothing I try works.

    I'm not sure if it is possible..
    I've tried:
    print lines[3]
    print lines[0]
    print(lines[3:0])

    I know splitting the lines has complicated things. I don't know Python well enough. Maybe I turned this into a tuple..

    I want a = 192.168.30.35 (for example)

    What is my best option?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    664
    Rep Power
    39
    A list is a variable. You have to iterate over the list and then get what you want from each sublist. See "Looping Over Lists" at An Introduction to Python Lists or most other online tutorials.
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    512
    Rep Power
    50
    Thanks.
    Code:
      for i in range(0,len(lines)):
         print(lines[i])
    Results:
    Code:
    ['']
    ['']
    ['']
    ['192.168.30.27', '2018-03-12 09:23:57.048936', 'inactive', 'Power Off']
    ['192.168.30.27', '2018-03-12 09:23:57.048936', 'rebooting', 'Power On']
    ['192.168.30.30', '2018-03-15 09:23:57.048936', 'inactive', 'Power Off']
    ['192.168.30.30', '2018-03-15 09:23:57.048936', 'rebooting', 'Power On']
    ['192.168.30.31', '2018-03-15 09:23:57.048933', 'inactive', 'Power Off']
    ['192.168.30.35', '2018-03-10 09:23:57.048936', 'inactive', 'Power Off']
    ['192.168.30.35', '2018-03-10 09:23:57.048936', 'rebooting', 'Power On']
    How do I get the IP Address out of that?
    Code:
    result = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})',lines[i])
    Results in:
    Code:
        result = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})',lines[i])
      File "/usr/lib/python2.7/re.py", line 181, in findall
        return _compile(pattern, flags).findall(string)
    TypeError: expected string or buffer
    Last edited by additude; March 17th, 2018 at 12:40 PM.
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    512
    Rep Power
    50
    Well after much research, trials and errors I came up with this:
    Code:
      for i in range(0,len(lines)):
        curip = min(lines[i], key=lambda x: x[0])
        print curip
    I used the previous sorted() use of lambda and applied it to get what I needed.
    I still don't totally understand what I did...
    But it's done

IMN logo majestic logo threadwatch logo seochat tools logo