For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > June 2007 > using sed to extract keys from lines









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author using sed to extract keys from lines
shakahshakah@gmail.com

2007-06-26, 8:05 am

I find myself doing variations on the following when I want to strip
keys from logfiles:

jc:~$ sed -e "s/^.*<user>\(.*\)<\/user>.*$/\1/" <<EOT
> <ts>2007-06-26</ts><user>Dilbert</user><page>index.html</page>
> <ts>2007-06-26</ts><user>Dogbert</user><page>index.html</page>
> EOT

Dilbert
Dogbert
jc:~$

Works fine for my purposes, but is there an easier way to do this,
perhaps an option to sed which removes the need for the "^.*" and ".*
$" ?

sail0r@creepjoint.net

2007-06-26, 10:06 pm

x-no-archive: yes
shakahshakah@gmail.com wrote:
> I find myself doing variations on the following when I want to strip
> keys from logfiles:
>
> jc:~$ sed -e "s/^.*<user>\(.*\)<\/user>.*$/\1/" <<EOT
> Dilbert
> Dogbert
> jc:~$
>
> Works fine for my purposes, but is there an easier way to do this,
> perhaps an option to sed which removes the need for the "^.*" and ".*
> $" ?

Your question is a little odd.
Usually people post broken code and want fixes and
instead you have posted perfectly functional code and want to know
how to make the perfectly fine code even more perfectly fine.
Honestly, I don't know if it is possible to improve this even further,
Maybe someone in comp.unix.shell does?
shakahshakah@gmail.com

2007-06-26, 10:06 pm

On Jun 26, 3:02 pm, sai...@creepjoint.net wrote:
> x-no-archive: yesshakahsha...@gmail.com wrote:
>
>
>
> Your question is a little odd.
> Usually people post broken code and want fixes and
> instead you have posted perfectly functional code and want to know
> how to make the perfectly fine code even more perfectly fine.
> Honestly, I don't know if it is possible to improve this even further,
> Maybe someone in comp.unix.shell does?


I thought I might be overlooking a simple option in sed, or a way to
do it with cut, or some other way. My usual crutch for such tasks is
awk, but that strikes me as a heavyweight tool at times, and in any
case it's good to see a few other ways to approach things.

Logan Shaw

2007-06-26, 10:06 pm

shakahshakah@gmail.com wrote:
> I find myself doing variations on the following when I want to strip
> keys from logfiles:
>
> jc:~$ sed -e "s/^.*<user>\(.*\)<\/user>.*$/\1/" <<EOT
> Dilbert
> Dogbert
> jc:~$
>
> Works fine for my purposes, but is there an easier way to do this,


I guess you're looking for something more concise, in which case
I could suggest this:

perl -nle 'print /<user>(.*)<\/user>/'

This ends up being a little simpler to read because Perl is not
transforming the line and then printing the transformed result;
instead, it is simply printing the remembered pattern directly.
(The regular expression evaluates to a list of all the patterns
that matched.)

By the way, in your original sed version, you should either
backslash-escape your dollar sign or you should
Logan Shaw

2007-06-27, 4:18 am

Logan Shaw wrote:
> (The regular expression evaluates to a list of all the patterns
> that matched.)


Whoops, I should say the regular expression evaluates to a list
of all the strings matched by the pattern.

- Logan
Ben Bacarisse

2007-06-27, 8:06 am

"shakahshakah@gmail.com" <shakahshakah@gmail.com> writes:

> I find myself doing variations on the following when I want to strip
> keys from logfiles:
>
> jc:~$ sed -e "s/^.*<user>\(.*\)<\/user>.*$/\1/" <<EOT
> Dilbert
> Dogbert
> jc:~$
>
> Works fine for my purposes, but is there an easier way to do this,
> perhaps an option to sed which removes the need for the "^.*" and ".*
> $" ?


I often find myself using cut. In this case (because there is no
single field delimiter) you need two 'cut's so you may consider it
ugly:

cut -d\> -f4 | cut -d\< -f1

--
Ben.
Kunzdakente

2007-06-30, 2:51 am

Christina Aguilera and Laetitia Casta Tongue Tickles Lesbian Lovers Pussy Movies!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com