#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Select specific field from CSV


    Hi,

    I am not using regex alongside another language it is infact used in an application I am running and trying to customize. Its basically a firewall log.

    I have a csv:

    <25>Jan 23 03:52:57 192.168.27.200 20090123, 9380690, 2009/01/23 10:52:54, 2009/01/23 10:52:53, global.devices, 499, mydevice, 0.0.0.0, traffic, traffic log, trust, (NULL), 10.1.2.6, 4021, 216.141.86.226, 1516, untrust, (NULL), 216.34.209.13, 80, 216.34.209.13, 80, tcp, global.devices, 499, Combined-Demo, fw/vpn, 32, accepted, info, no, Close - TCP FIN, (NULL), (NULL), (NULL), 1, 853, 715, 1568, 0, 0, 10, 1, no, 0, Not Set, sos

    and I want to extract fields by selecting field 5 which should be global.devices. Field 5 could have anything in here not just english chars and .'s. I would like to use this construction to eventually pick any field I want out of the csv, not just 5.

    Any help here greatly appreciated - I have tried allsorts but to no avail.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,074
    Rep Power
    9398
    If there are commas in one of the fields (and so it has quotes around it) it'll be more complicated.

    When asking for regex help it's always nice to tell us what flavor of regex it is. They're all subtly different. Or in your case, what program you're using.
    Code:
    ^(\s*([^,]*),?){5}
    $2 is the field you want, regardless of what number you have in the {}s.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0
    Thanks I will try that when I am in the office tomorrow. The program is radar by q1labs and I have no idea what version of regex it is.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0
    Originally Posted by andydouth
    Thanks I will try that when I am in the office tomorrow. The program is radar by q1labs and I have no idea what version of regex it is.
    This is so close !

    It is selecting the , on the end is there a simple way of removing this?

    Thanks
  8. #5
  9. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,074
    Rep Power
    9398
    Hmm, you must be using it a bit differently than I thought.
    Code:
    ^\s*[^,]*(,\s*([^,]*)){4}

IMN logo majestic logo threadwatch logo seochat tools logo