1833016 Members
2211 Online
110048 Solutions
New Discussion

Re: script functionality

 
Dave Walley
Frequent Advisor

script functionality

Hi.

I have a problem whereby I require to extract values from a file. The problem is some lines contain multiple values and I need to retrieve just the first or second value on occasions.

e.g.
Buffer Nowait %: 100.00 Redo NoWait %: 70.00
Buffer Hit %: 96.68 In-memory Sort %: 99.83
Library Hit %: 99.89 Soft Parse %: 99.64
Execute to Parse %: 57.07 Latch Hit %: 99.99

From this I need to retrieve each value in turn, I am using grep to isolate the row but now I need to extract only the relevant value, e.g. when I need the IN_MEMORY SORT I only want 99.83 and not the entire line. Similarly when I want BUFFER HIT I want 99.68 and nothing else from the line.

Any ideas?

Thanks in advance

Dave

why do i do this to myself
7 REPLIES 7
Tore_1
Regular Advisor

Re: script functionality

all the numbers :

awk -F': ' '{print $2,$3}' file | awk '{print $1,$NF}'
Colin Topliss
Esteemed Contributor

Re: script functionality

To get the first value:

cat myfile |awk -F% '{print $2}' |awk '{print $2}'

To get the second value:

cat myfile |awk -F% '{print $2}' |awk '{print $2}'
Steve Steel
Honored Contributor

Re: script functionality

hi

cat file|while read line
do
treatment of line
done


Then you have the lines 1 by 1 in varaible line.

ex

cat file|while read line
do
echo $line|cut -f3 -d":"
done

gives only the last field.


What do you need for format


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
H.Merijn Brand (procura
Honored Contributor

Re: script functionality

a5:/tmp 110 > cat xx Buffer Nowait %: 100.00 Redo NoWait %: 70.00
Buffer Hit %: 96.68 In-memory Sort %: 99.83
Library Hit %: 99.89 Soft Parse %: 99.64
Execute to Parse %: 57.07 Latch Hit %: 99.99
a5:/tmp 111 > perl -nle'BEGIN{$pat=shift}/\s*([a-z_. -]*?$pat[\w%:. -]*?[\d.]+)/i&&print$1' memory xx
In-memory Sort %: 99.83
a5:/tmp 112 > perl -nle'BEGIN{$pat=shift}/\s*([a-z_. -]*?$pat[\w%:. -]*?[\d.]+)/i&&print$1' parse xx
Soft Parse %: 99.64
Execute to Parse %: 57.07
a5:/tmp 113 >

you can allways turn this into a script :)

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Steve Steel
Honored Contributor

Re: script functionality

Hi

awk is fun but anyway here is another silly one


cat file|while read line
do
f1=$(echo $line|cut -f1 -d":")
f2=$(echo $line|cut -f2 -d":")
f2a=$(echo $f2|cut -f1 -d" ")
f2b=$(echo $f2|cut -f2 -d" ")
f3=$(echo $line|cut -f3 -d":")
echo $f2a $f1
echo $f3 $f2b
done|sort -n


output is

57.07 Execute to Parse %
70.00 Redo
96.68 Buffer Hit %
99.64 Soft
99.83 In-memory
99.89 Library Hit %
99.99 Latch
100.00 Buffer Nowait %

It is endless

Steve steel
If you want truly to understand something, try to change it. (Kurt Lewin)
H.Merijn Brand (procura
Honored Contributor

Re: script functionality

Since Opera ruined my newlines anyway, here's the script approach:

a5:/tmp 117 > cat xx
Buffer Nowait %: 100.00 Redo NoWait %: 70.00
Buffer Hit %: 96.68 In-memory Sort %: 99.83
Library Hit %: 99.89 Soft Parse %: 99.64
Execute to Parse %: 57.07 Latch Hit %: 99.99
a5:/tmp 118 > cat xx.pl
#!/usr/bin/perl -l
$pat = shift;
while (<>) {
/\s*([a-z_. -]*?$pat[\w%:. -]*?[\d.]+)/i && print $1;
}
a5:/tmp 119 > xx.pl memory xx
In-memory Sort %: 99.83
a5:/tmp 120 > xx.pl parse xx
Soft Parse %: 99.64
Execute to Parse %: 57.07
a5:/tmp 121 > xx.pl soft xx
Soft Parse %: 99.64
a5:/tmp 122 >

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Robin Wakefield
Honored Contributor

Re: script functionality

Hi Dave,

Here's another awk solution:

Create an awk script:

=======================
$0 ~ var{
p=index($0,var)
for (i=p+length(var)+4;i{printf substr($0,i,1)}
}END{print}
=======================

then run using:

# awk -f awkscript var="Soft Parse" yourfile
99.64

# awk -f awkscript var="Buffer Nowait" yourfile
100.00

rgds, Robin