Operating System - HP-UX
1833465 Members
2876 Online
110052 Solutions
New Discussion

scripting - read vars from a file

 
SOLVED
Go to solution
Kim Kendall
Regular Advisor

scripting - read vars from a file

Trying to read data from a file. First part of the script outputs ps info to a file:

UNIX95= ps -e -o ruser,pid,pcpu,vsz,stime,time,args | grep oracleprod | grep -v grep | sort -rnk3 >> ./ps.out

Now I want to do a "for i in `cat ./ps.out`" , BUT, every field is on it's own line in the output file. If I cat the file, or even vi the ps.out file, it appears to be fine, as I would expect it to be.

So I either need to find a way to read 7 lines at a time and assign them to different variables, or find a way to fix the formatting of the ps command so it writes the lines correctly.

Any ideas?
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: scripting - read vars from a file

Hi Kim:

To read one line at a time from your file, simply do:

while read LINE
do
your_thing
done < ps.out

...If you want to read the seven fields comprising each line, do:

while read RUSER PID PCPU VSZ STIME TIME ARGS
do
your_thing
done < ps.out

Regards!

...JRF...
Kim Kendall
Regular Advisor

Re: scripting - read vars from a file

It's 7 fields repeated over and over
Mark Greene_1
Honored Contributor
Solution

Re: scripting - read vars from a file

You have several choices (but then it's unix, so you already knew that). Unless you need to retain the results in a file you set UNIX95 seperately from the command (export UNIX95=1, I think will work) and route the output of the ps query to a variable like this:

PS_OUT=`ps -e -o ruser,pid,pcpu,vsz,stime,time,args | grep oracleprod | grep -v grep | sort -rnk3`

(watch the line wrap, all that goes on 1 line)

Next, change the IFS variable to cooperate:

OLD_IFS=$IFS
IFS="^J" (that's a control-J, a linefeed only)

Now this construction will give you an entire line at a time:

for LINE in `echo PS_OUT`; do
[do your stuff with the data here]
done
IFS=$OLD_IFS

The other option you have is leave the ps capture the way you have it and to read from the ps.out file like this:

exec 4<./ps.out
while read -u4 LINE; do
[do your stuff with the data here]
done

Depending on the actual format of the file, you may need to alter the IFS variable as above.

HTH
mark
the future will be a lot like now, only later
Steven E. Protter
Exalted Contributor

Re: scripting - read vars from a file

A helpful hint with UNIX95 variable.

Don't set it to 1 in /etc/profile or root profile

Havint it set when you sh HP depots can cause crc errors. Not really, but you won't be able to use the patch.

Steve
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Oleg Zieaev_1
Regular Advisor

Re: scripting - read vars from a file

Hello.
If you have predefined fields in your file do:

while read -r RUSER PID PCPU VSZ STIME TIME AGRS
do
.... use your $RUSER etc variables here as you want.
done < ./ps.out

Hope this helps,
0leg
Professionals will prevail ...
Fred Martin_1
Valued Contributor

Re: scripting - read vars from a file

You might use awk, but I suspect if you knew the awk language you would have thought of that already. awk reads files one line at a time and uses white space as a field seperator.

For example, the output from a 'ps' command might look like this:

gbc 18685 1 0 14:56:03 pts/ta 0:00 /usr/bin/newmail -i90

If you pipe that to awk, within awk each field on the line is assigned a parameter variable:

$0 = the entire line
$1 = gbc
$2 = 18685
$3 = 1
$4 = 0
$5 = 14:56:03
$6 = pts/ta
$7 = 0:00
$8 = /usr/bin/newmail
$9 = -i90

Anyway I can't give you an awk tutorial here but if you already know some shell scripting stuff, you can handle awk. The best book about the language is "The Awk Programming Language" by Aho, Kernighan and Weinberger.
fmartin@applicatorssales.com
Fred Martin_1
Valued Contributor

Re: scripting - read vars from a file

...but I also like the "while read line" solution above....
Fred
fmartin@applicatorssales.com
Wodisch_1
Honored Contributor

Re: scripting - read vars from a file

Hi,

looks like your more after the part inside the loop?
How about using the POSIX features of "ps", and suppressing the headline:

UNIX95=. ps -e -C your,processes,go,here -o ruser=,pid=,pcpu=,vsz=,stime=,time=,args= |
while read ruser pid pcpu vsz stime time args
do case "$ruser" in
process#1) do-something-here ;;
process#2) do-something-else ;;
esac
done

FWIW,
Wodisch
Kim Kendall
Regular Advisor

Re: scripting - read vars from a file

Ended up using Mark Greene's idea...

exec 4<$psout2
while read -u4 LINE
do
_usr=`echo $LINE | awk '{print $1}'`
_pid=`echo $LINE | awk '{print $2}'`
_cpu=`echo $LINE | awk '{print $3}'`
_vsz=`echo $LINE | awk '{print $4}'`
_sti=`echo $LINE | awk '{print $5}'`
_tim=`echo $LINE | awk '{print $6}'`
_arg=`echo $LINE | awk '{print $7}'`


done


Not exceptionally elegant, but it works! ;)