Skip to content

Instantly share code, notes, and snippets.

@mattn
Last active December 12, 2015 07:28
Show Gist options
  • Save mattn/4737234 to your computer and use it in GitHub Desktop.
Save mattn/4737234 to your computer and use it in GitHub Desktop.
let s:sfile = expand('<sfile>')
function! ltsv#parse_line(line, ...)
let sep = get(a:000, 0, "\t")
let r = {}
for o in split(a:line, sep)
let kv = matchlist(o, '^\([^:]\+\):\(.*\)$')
let r[kv[1]] = substitute(kv[2], "\n", '', 'g')
endfor
return r
endfunction
function! ltsv#parse_lines(lines, ...)
let sep = get(a:000, 0, "\t")
return map(split(a:lines, "\n"), 'ltsv#parse_line(v:val)')
endfunction
function! ltsv#parse_file(filename, ...)
let sep = get(a:000, 0, "\t")
return ltsv#parse_lines(readfile(a:filename), sep)
endfunction
function! ltsv#test()
echo ltsv#parse_line("hoge:foo\tbar:baz\ttime:20:30:58\n")
echo ltsv#parse_lines(substitute(join(readfile(s:sfile), "\n"), '.*\nfinish\n', '', ''))
endfunction
finish
time:05/Feb/2013:15:34:47 +0000 host:192.168.50.1 req:GET / HTTP/1.1 status:304 size:0 referer:- ua:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.58 Safari/537.22 reqtime:0.000 uprestime:-
time:05/Feb/2013:15:34:47 +0000 host:192.168.50.1 req:GET /poweredby.png HTTP/1.1 status:304 size:0 referer:http://192.168.50.6/ ua:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.58 Safari/537.22 reqtime:0.000 uprestime:-
time:05/Feb/2013:15:34:47 +0000 host:192.168.50.1 req:GET /nginx-logo.png HTTP/1.1 status:304 size:0 referer:http://192.168.50.6/ ua:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.58 Safari/537.22 reqtime:0.000 uprestime:-
time:05/Feb/2013:15:34:47 +0000 host:192.168.50.1 req:GET /favicon.ico HTTP/1.1 status:404 size:3652 referer:- ua:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.58 Safari/537.22 reqtime:0.000 uprestime:-
time:05/Feb/2013:15:35:06 +0000 host:192.168.50.1 req:- status:400 size:0 referer:- ua:- reqtime:0.000 uprestime:-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment