Question

I am having some trouble working out how to return a line of text if a match is found.

(set 'wireshark "http://anonsvn.wireshark.org/wireshark/trunk/manuf")

(set 'arptable (map (fn (x) (parse x " ")) (exec "arp -a")))

(define (cleanIPaddress x)
  (slice x 1 -1))

(define (cleanMACaddress x) 
  (upper-case (join (slice (parse x ":") 0 3) ":")))

(define (addIPandMACaddress x) 
  (list (cleanIPaddress (nth 1 x)) (cleanMACaddress (nth 3 x))))

(set 'arplist (map addIPandMACaddress arptable))

(set 'routerMAC (last (assoc (exec "ipconfig getoption en1 router") arplist)))

(find-all routerMAC (get-url wireshark))

returns

("20:AA:4B")

so I know that the code "works"

but I would like to retrieve the full line of text

"20:AA:4B Cisco-Li # Cisco-Linksys, LLC"

Was it helpful?

Solution 2

In the end I used:

(find-all (string routerMAC ".*") (get-url wireshark))

OTHER TIPS

This can be performed simply by using a string-split procedure that allows us to use remove-if (the Common Lisp version of filter) to search through a string split by newlines removing any lines that do not contain the string we are searching for. That would result in a list of every line containing the string. The functions we will define here are already available via various Common Lisp libraries, but for the education purposes, we will define them all ourselves. The code you need works like so:

; First we need a function to split a string by character

(defun string-split (split-string string)
  (loop with l = (length split-string)
        for n = 0 then (+ pos l)
        for pos = (search split-string string :start2 n)
        if pos collect (subseq string n pos)
        else collect (subseq string n)
        while pos))

; Now we will make a function based on string-split to split by newlines

(defun newline-split (string)
  (string-split "
" string))

; Finally, we go through our text searching for lines that match our string.
; Make sure to replace 'needle' with the string you wish to search for.

(remove-if #'(lambda (x) 
               (equal 'nil (search (string-upcase "needle") 
                                   (string-upcase x))))
           (newline-split haystack))

You should be able to apply this strategy to the code you posted with a few small modifications. This code was tested on SBCL 1.0.55.0-abb03f9, an implementation of ANSI Common Lisp, on Mac OS X 10.7.5.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top