using sed to extract keys from lines
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > using sed to extract keys from lines




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-07 12:17 PM

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 ".*
$" ?






[ Post a follow-up to this message ]



    Re: using sed to extract keys from lines  
sail0r@creepjoint.net


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-27-07 12:22 AM

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?





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-27-07 12:22 AM

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.






[ Post a follow-up to this message ]



    Re: using sed to extract keys from lines  
Logan Shaw


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-27-07 06:21 AM

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 use single quotes
instead of double quotes.

- Logan





[ Post a follow-up to this message ]



    Re: using sed to extract keys from lines  
Logan Shaw


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-27-07 06:21 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





[ Post a follow-up to this message ]



    Re: using sed to extract keys from lines  
Ben Bacarisse


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-27-07 12:23 PM

"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.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:44 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register