Unix Shell - grepping and cutting

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2007 > grepping and cutting





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 grepping and cutting
Rich Bradshaw

2007-02-21, 7:26 am

Hi,

I am using gnuplot to fit some curves, then trying to extract the
parameters from that file. An exapmle section would be:

initial set of free parameter values

d = 1e-30
c = 1e-30

After 4 iterations the fit converged.
final sum of squares of residuals : 0.235848
rel. change during last iteration : -2.80466e-06

Final set of parameters Asymptotic Standard Error
======================= ==========================

d = 1.28047 +/- 0.03958 (3.091%)
c = 2.90137 +/- 0.1532 (5.279%)


correlation matrix of the fit parameters:

d c
d 1.000
c -0.996 1.000

Now, the value that I would like is the 1.28047, so I am using:

grep 'd =' fit.log | tail -1 | cut -c 19-25

to find the section that says 'd+15 spaces+=' of the file, tail it, as
this section above is repeated many times, I just want the bottom one,
then cut to manually get the number I want.

Is there a better way to do this? It seems a bit hardcoded - if the
file were slightly different, then it would break. (e.g. if the value
was longer, like 100.28047, I wouldn't get all of it.)

Thanks!

Janis

2007-02-21, 7:26 am

On 21 Feb., 12:22, "Rich Bradshaw" <Rich.Brads...@gmail.com> wrote:
> Hi,
>
> I am using gnuplot to fit some curves, then trying to extract the
> parameters from that file. An exapmle section would be:
>
> initial set of free parameter values
>
> d = 1e-30
> c = 1e-30
>
> After 4 iterations the fit converged.
> final sum of squares of residuals : 0.235848
> rel. change during last iteration : -2.80466e-06
>
> Final set of parameters Asymptotic Standard Error
> ======================= ==========================
>
> d = 1.28047 +/- 0.03958 (3.091%)
> c = 2.90137 +/- 0.1532 (5.279%)
>
> correlation matrix of the fit parameters:
>
> d c
> d 1.000
> c -0.996 1.000
>
> Now, the value that I would like is the 1.28047, so I am using:
>
> grep 'd =' fit.log | tail -1 | cut -c 19-25
>
> to find the section that says 'd+15 spaces+=' of the file, tail it, as
> this section above is repeated many times, I just want the bottom one,
> then cut to manually get the number I want.


Specify the pattern (e.g., starting the line with d followed somewhere
by = and a %) of the line that you want to match and print the
requested field ($3, assuming that there is whitespace around the =
sign)...

awk '/^d.*=.*%/ { print $3 }'

Not sure about "the bottom one" requirement; you may want to pipe the
result through tail -1 or do all in awk...

awk '/^d.*=.*%/ { x = $3 } END { print x }'


Janis

>
> Is there a better way to do this? It seems a bit hardcoded - if the
> file were slightly different, then it would break. (e.g. if the value
> was longer, like 100.28047, I wouldn't get all of it.)
>
> Thanks!



aryzhov@spasu.net

2007-02-21, 7:26 am


awk '/^d *=.*%/' | sed 's/^.*= *//;s/ *+.*$//'

i.e. match the lines starting with "d" followed by any number of
spaces,
then = then any number of anything followed by %

In those lines, replace everything from begining till =
by nothing;
and any numbers of blanks followed by + till the end of line
by nothing

Just an idea, it sure can be made more robust and elegant

Regards,
Andrei

Michael Tosch

2007-02-21, 1:18 pm

Janis wrote:
> On 21 Feb., 12:22, "Rich Bradshaw" <Rich.Brads...@gmail.com> wrote:
>
> Specify the pattern (e.g., starting the line with d followed somewhere
> by = and a %) of the line that you want to match and print the
> requested field ($3, assuming that there is whitespace around the =
> sign)...
>
> awk '/^d.*=.*%/ { print $3 }'
>
> Not sure about "the bottom one" requirement; you may want to pipe the
> result through tail -1 or do all in awk...
>
> awk '/^d.*=.*%/ { x = $3 } END { print x }'
>
>
> Janis
>


A regexp with many .* can be demanding.

awk '$1=="d" && $2=="=" {x=$3} END {print x}

might save some CPU cycles.


--
Michael Tosch @ hp : com
Michael Tosch

2007-02-21, 1:18 pm

aryzhov@spasu.net wrote:
> awk '/^d *=.*%/' | sed 's/^.*= *//;s/ *+.*$//'
>


Isn't that

sed '/^d *=.*%/!d; s/^.*= *//; s/ .*//'
?

And the OP wanted the last line:

sed -n '$g; $p; /^d *=.*%/!d; s/^.*= *//; s/ .*//; h'

This only works for spaces as word separator.
Usually I prefer awk because it takes a quantity of
spaces or tabs as word separator.

--
Michael Tosch @ hp : com
aryzhov@spasu.net

2007-02-22, 7:19 am

On Feb 21, 5:17 pm, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se>
wrote:

> Isn't that
>
> sed '/^d *=.*%/!d; s/^.*= *//; s/ .*//'
> ?


Sure, it's a good one.


> And the OP wanted the last line:
>
> sed -n '$g; $p; /^d *=.*%/!d; s/^.*= *//; s/ .*//; h'
>
> This only works for spaces as word separator.
> Usually I prefer awk because it takes a quantity of
> spaces or tabs as word separator.
>


That's why I didn't want to use positional parameters in AWK -
I wasn't sure there's always a delimiter after an =

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com