For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > January 2008 > Extract Text with "sed"









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 Extract Text with "sed"
Meendar

2008-01-29, 4:38 am

Hi,

I am having a text generated from version output of an executable as

$ myprogram -v
Version: Myprogram/1.2.1 - Centos
Built: Aug 8 2007 13:05:44


I need to extract 1.2.1 from the above output, however i used sed like

$ myprogram -v | sed -e '/s/Myprogram\ /(*) ^-

This doesn't work.

Any help?

Thanks.
Giorgos Keramidas

2008-01-29, 8:27 am

On Tue, 29 Jan 2008 00:08:15 -0800 (PST), Meendar <meendar@gmail.com> wrote:
> Hi,
>
> I am having a text generated from version output of an executable as
>
> $ myprogram -v
> Version: Myprogram/1.2.1 - Centos
> Built: Aug 8 2007 13:05:44
>
>
> I need to extract 1.2.1 from the above output, however i used sed like
>
> $ myprogram -v | sed -e '/s/Myprogram\ /(*) ^-
>
> This doesn't work.


That's because the sed expression is either misformatted by the program
you used to post this message or just incomplete, or both.

$ ( echo 'Version: Myprogram/1.2.1 - Centos' ; \
echo foo ) | \
sed -n -e '1s/.*\/\(.*\) - .*/\1/p'
1.2.1

Meendar

2008-01-29, 8:27 am

Thanks for the answer, however i couldn't get the actual while i go
with executable.

Just replace the myprogram with httpd and please answer me. It looks
same as myprogram version No

$ httpd -v | sed -n -e '1s/.*\/\(.*\) - .*/\1/p'

This shows the whole thing.

Thanks Again for the reply.



Giorgos Keramidas wrote:
> On Tue, 29 Jan 2008 00:08:15 -0800 (PST), Meendar <meendar@gmail.com> wrote:
>
> That's because the sed expression is either misformatted by the program
> you used to post this message or just incomplete, or both.
>
> $ ( echo 'Version: Myprogram/1.2.1 - Centos' ; \
> echo foo ) | \
> sed -n -e '1s/.*\/\(.*\) - .*/\1/p'
> 1.2.1

Azazel

2008-01-29, 8:27 am

Please don't top-post.

On 2008-01-29, Meendar <meendar@gmail.com> wrote:
> Giorgos Keramidas wrote:
>
> Thanks for the answer, however i couldn't get the actual while i go
> with executable.
>
> Just replace the myprogram with httpd and please answer me. It looks
> same as myprogram version No
>
> $ httpd -v | sed -n -e '1s/.*\/\(.*\) - .*/\1/p'
>
> This shows the whole thing.


$ (echo Version: Myprogram/1.2.1 - Centos; \
echo Built: Aug 8 2007 13:05:44) | \
sed -n -e '{
s,^Version: Myprogram/\([0-9]*\.[0-9]*\.[0-9]*\).*$,\1, ; T ; p
}'

--
Az.

www: http://www.azazel.net/
pgp: http://www.azazel.net/~azazel/az_key.asc
Ralf Fassel

2008-01-30, 4:34 am

* Meendar <meendar@gmail.com>
| $ myprogram -v
| Version: Myprogram/1.2.1 - Centos
| Built: Aug 8 2007 13:05:44
|
| I need to extract 1.2.1 from the above output, however i used sed like

Is 'sed' a must?

myprogram -v | awk -F/ '/Version/ {print $2}'| awk '{print $1}'

R'
shakahshakah@gmail.com

2008-01-30, 7:24 pm

On Jan 29, 8:54 am, Meendar <meen...@gmail.com> wrote:
> Thanks for the answer, however i couldn't get the actual while i go
> with executable.
>
> Just replace the myprogram with httpd and please answer me. It looks
> same as myprogram version No
>
> $ httpd -v | sed -n -e '1s/.*\/\(.*\) - .*/\1/p'
>
> This shows the whole thing.
>
> Thanks Again for the reply.
>
> Giorgos Keramidas wrote:
>
>
>
>
>
>
>

How about :
sed -e '/^Built/ d' -e 's|^[^/]*/\([0-9.]*\).*$|\1|'

e.g.:
jc@rs-dev2:~$ (echo Version: Myprogram/1.2.1 - Centos; echo Built:
Aug 8 2007 13:05:44) \[color=darkred]
> | sed -e '/^Built/ d' -e 's|^[^/]*/\([0-9.]*\).*$|\1|'

1.2.1

Logan Shaw

2008-01-31, 4:33 am

Meendar wrote:
> Thanks for the answer, however i couldn't get the actual while i go
> with executable.
>
> Just replace the myprogram with httpd and please answer me. It looks
> same as myprogram version No
>
> $ httpd -v | sed -n -e '1s/.*\/\(.*\) - .*/\1/p'
>
> This shows the whole thing.


Your program may be printing its version string to stderr instead
of stdout. Try these two and see which one causes the output
to be discarded:

httpd -v > /dev/null
httpd -v 2> /dev/null

If the first causes the output to be discarded, then it is
writing to stdout, and you can expect the data to go to the
pipe. If, however, the second is the one that discards the
output, then "httpd -v" is writing to the stderr, and you
will need to redirect stderr to stdout in order to get the
data into the pipeline:

http -v 2>&1 | sed [ ... whatever ... ]

- Logan
Meendar

2008-01-31, 7:27 pm

On Jan 31, 9:32 am, Logan Shaw <lshaw-use...@austin.rr.com> wrote:
> Meendar wrote:
>
>
>
>
> Your program may be printing its version string to stderr instead
> of stdout. Try these two and see which one causes the output
> to be discarded:
>
> httpd -v > /dev/null
> httpd -v 2> /dev/null
>
> If the first causes the output to be discarded, then it is
> writing to stdout, and you can expect the data to go to the
> pipe. If, however, the second is the one that discards the
> output, then "httpd -v" is writing to the stderr, and you
> will need to redirect stderr to stdout in order to get the
> data into the pipeline:
>
> http -v 2>&1 | sed [ ... whatever ... ]
>
> - Logan


Thanks to All, Now i got what i meant!
Giorgos Keramidas

2008-01-31, 7:27 pm

On Tue, 29 Jan 2008 05:54:38 -0800 (PST), Meendar <meendar@gmail.com> wrote:
>Giorgos Keramidas wrote:
>
> Thanks for the answer, however i couldn't get the actual while i go
> with executable.
>
> Just replace the myprogram with httpd and please answer me. It looks
> same as myprogram version No
>
> $ httpd -v | sed -n -e '1s/.*\/\(.*\) - .*/\1/p'


There are at least a couple of reasons why this may be happening:

(1) The `httpd' writes to stderr, instead of stdout.

(2) The version line is not the first.

Can you try the variation shown below?

% httpd -v 2>&1 | sed -n -e '/Version:/ s/.*\/\(.*\) - .*/\1/p'

If this doesn't work either, it may be useful to see the output of
something like:

% httpd -v 2>&1 | od -c

Sponsored Links







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

Copyright 2008 codecomments.com