Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printf awk sed to retrieve data

Status
Not open for further replies.

Triton46

Programmer
Jan 9, 2002
33
US
I have a file that looks like:

procs memory page disk faults cpu
r b w swap free re mf pi po fr de sr m0 m1 m3 m4 in sy cs us sy id
0 0 0 1888 776 4 109 65 6 2277 0 3771 0 0 0 0 625 2371 1162 16 12 72

It was built from vmstat for Oracle. Out of this file I want to grab only certain parts to be inserted into a table. I have tried the following:

while read RUNQUEPAGE_IN PAGE_OUT USER_CPU SYSTEM_CPUIDLE_CPU WAIT_CPU
do
cat /tmp/msg$$|sed 1,4d | awk '{ printf("%s %s %s %s %s %s %s\n", $1, $8, $9, $14, $15, $16, $17)}'

Do I need a '%s' for every column in the file? Is there a simple way to retrieve certain columns from this file?
 
Are you looking for on output of something like

0
14
67
where 0=$1, 114=$7, 67=$8

also, I noticed that you don't have a "done" statement at the end of the do loop.
 
vgersh99,
the command reproduces a line of data, but I want to put parts of that line ($1, $8, $9, $14, $15, $16, $17) into variable for inserting (echoing in my example) into a table. I am anticipating having the data from 'column' 1, 'column' 8, 'column' 9, etc in variables name1, name2, name3, etc.
read below

linmatty,
Yes that is exactly the output I want. I do have a done statement, just don't have it in the example.
 
Ok, I just noticed that the file had tmp/msg*

msg* being the key here. It was reading all the msg files in tmp (which there were a few).

I reran:
while read name1 name2 name3
do
echo 'start second loop'
sed -e '1,4d' -e 's/^[ ]*//g' /tmp/msg18145 | nawk -F '[ ][ ]*' ' {printf("%d %d %d\n", $1, $8, $9)}
'
echo $name1
echo $name2
echo $name3
done < /tmp/msg18145


and got this:
2
0
0 10045496 58000 8 295 40 15 9064 0 13388 1 0 1 0 1073 6088 2254 60 33 7

Apparently I need the (sed1,4d) command other wise I get:
0 67 0
0
0 10045496 58000 8 295 40 15 9064 0 13388 1 0 1 0 1073 6088 2254 60 33 7

How can I keep all the data from piling into that last variable?
 
something like that?

#!/bin/ksh

sed -e '1,2d' -e 's/^[ ]*//g' /tmp/sample.txt | nawk -F '[ ][ ]*' ' {printf(&quot;%d %d %d %d %d %d %d\n&quot;, $1, $8, $9, $14, $15, $16, $17)}' | while read RUNQUE PAGE_IN PAGE_OUT USER_CPU SYSTEM_CPU IDLE_CPU WAIT_CPU garbage
do

echo ${RUNQUE}
echo ${PAGE_IN}
echo ${PAGE_OUT}
echo ${USER_CPU}
echo ${SYSTEM_CPU}
echo ${IDLE_CPU}
echo ${WAIT_CPU}
done
 


you're 'looping/reading' incorrectly - look at my previous post.

vlad
 
I'll bet this works, not at a unix prompt right now though.

#!/bin/ksh
sed -e '1,4d' -e 's/^[ ]*//g' /tmp/msg18145 | nawk -F '[ ][ ]*' ' {printf(&quot;%d %d %d\n&quot;, $1, $8, $9)} |
while read LINE
do
FIELD1=`echo $LINE | awk '{print $1}'`
FIELD8=`echo $LINE | awk '{print $8}'`
FIELD9=`echo $LINE | awk '{print $9}'`

print $FIELD1
print $FIELD8
print $FIELD9
done

 
syntax error on my last post...forgot a '(single quote)...
should be

sed -e '1,4d' -e 's/^[ ]*//g' /tmp/msg18145 | nawk -F '[ ][ ]*' ' {printf(&quot;%d %d %d\n&quot;, $1, $8, $9)}' |


 
vgersh99,

I tried the read at the end when I first put the program together and it would not read. Moving it to the beginning got it to work correctly.

I'll copy and paste your script into unix and try again.

linmatty,

I'll try yours too.

 
another syntax error I noticed...

ksh might not like the &quot;echo&quot; statements in the variable assignments. If thats the case, replace with &quot;print&quot;.
 
yet another...

FIELD1=`echo $LINE | awk '{print $1}'`
FIELD8=`echo $LINE | awk '{print $8}'`
FIELD9=`echo $LINE | awk '{print $9}'`

should be

FIELD1=`echo $LINE | awk '{print $1}'`
FIELD8=`echo $LINE | awk '{print $2}'`
FIELD9=`echo $LINE | awk '{print $3}'`



 
This is it...I swear!!

#!/bin/ksh
sed -e '1,4d' -e 's/^[ ]*//g' /tmp/msg18145 | nawk -F '[ ][ ]*' ' {printf(&quot;%d %d %d\n&quot;, $1, $8, $9)}' |
while read LINE
do
FIELD1=`print $LINE | nawk '{print $1}'`
FIELD8=`print $LINE | nawk '{print $2}'`
FIELD9=`print $LINE | nawk '{print $3}'`

print $FIELD1
print $FIELD8
print $FIELD9
done

 
Test1:

#!/bin/ksh

sed -e '1,2d' -e 's/^[ ]*//g' /tmp/sample.txt | nawk -F '[ ][ ]*' ' {printf(&quot;%d %d %d %d %d %d %d\n&quot;, $1, $8, $9, $14, $15, $16, $17)}' | while read RUNQUE PAGE_IN PAGE_OUT USER_CPU SYSTEM_CPU IDLE_CPU WAIT_CPU garbage
do

echo ${RUNQUE}
echo ${PAGE_IN}
echo ${PAGE_OUT}
echo ${USER_CPU}
echo ${SYSTEM_CPU}
echo ${IDLE_CPU}
echo ${WAIT_CPU}
done

This worked! I will try the other one just for my own edification. I wonder why it would not work before. Did anyone see anything on that Oracle Tips website I posted up above that would cause the read not to work?
 
Final problem. These scripts work for a static file, but for some reason the script returns different values when sending vmstats to a file. Here is the entire script with output depending on what is commented out:

#!/bin/ksh

ORACLE_HOME=/tpsw/app/oracle/product/8.1.7
SERVER_NAME=`uname -a|awk '{print $2}'`
SAMPLE_TIME=30

*while true
*do
echo 'build msg file'
vmstat ${SAMPLE_TIME} 2 > /tmp/msg$$

sed -e '1,2d' -e 's/^[ ]*//g' /tmp/msg.txt | nawk -F '[ ][ ]*' ' {printf(&quot;%d %d %d %d %d %d %d\n&quot;, $1,
$8, $9, $14, $15, $16, $17)}' | while read RUNQUE PAGE_IN PAGE_OUT USER_CPU SYSTEM_CPU IDLE_CPU WAIT_CPU garbage
do

echo ${RUNQUE}
echo ${PAGE_IN}
echo ${PAGE_OUT}
echo ${USER_CPU}
echo ${SYSTEM_CPU}
echo ${IDLE_CPU}
echo ${WAIT_CPU}
done
*done
rm /tmp/msg.txt

build msg file
0
64
7
0
0
0
638


#!/bin/ksh

ORACLE_HOME=/tpsw/app/oracle/product/8.1.7
SERVER_NAME=`uname -a|awk '{print $2}'`
SAMPLE_TIME=30

while true
do
echo 'build msg file'
vmstat ${SAMPLE_TIME} 2 > /tmp/msg$$

sed -e '1,2d' -e 's/^[ ]*//g' /tmp/msg$$ | nawk -F '[ ][ ]*' ' {printf(&quot;%d %d %d %d %d %d %d\n&quot;, $1,
$8, $9, $14, $15, $16, $17)}' | while read RUNQUE PAGE_IN PAGE_OUT USER_CPU SYSTEM_CPU IDLE_CPU WAIT_CPU garbage
do

echo ${RUNQUE}
echo ${PAGE_IN}
echo ${PAGE_OUT}
echo ${USER_CPU}
echo ${SYSTEM_CPU}
echo ${IDLE_CPU}
echo ${WAIT_CPU}
done
done
rm /tmp/msg$$

build msg file
0
64
7
0
0
0
638
0
15
0
0
0
0
359


I looked for 359 in my file but it does not exist? The controlling factor seems to be:

vmstat ${SAMPLE_TIME} 2 > /tmp/msg$$

I changed the 2 to 1 but nothing came out.

 
linmatty,

It does print a header, but that header exists in the static file as well. I only get double the output from the second script (note the sections that were commented have been uncommented and instead of looking at msg.txt it is reading msg$$ which is created by vmstats).
 
Hmmmm...try this,


I added a for statement to run through all command line arguments. also replaced /tmp/mesg1845.txt on the 4th line with $i. so to execute this script, type
./scriptname /tmp/msg*

#!/bin/ksh
for i in $* # expands all command line arguments
do
sed -e '1,4d' -e 's/^[ ]*//g' $i | nawk -F '[ ][ ]'{
printf (&quot;%d %d %d\n&quot;, $1, $8, $9)}' |
while read LINE
do
FIELD1=`print $LINE | nawk '{print $1}'`
FIELD8=`print $LINE | nawk '{print $2}'`
FIELD9=`print $LINE | nawk '{print $3}'`

print $FIELD1
print $FIELD8
print $FIELD9
done
done


good luck, have a good weekend!
 
linmatty/all

Thanks! I kinda figured out what is going on. Sometime after the creation of the msg$$ file there is an additional line of data put into it from vmstat. If I stop the program at a certain point I can see two lines of numbers. I'm not sure if that is normal for vmstat or if it is indicative of dual processes running.

At any rate, the script is functioning correctly. Thanks guys! Much appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top