how to read a txt file using shell script

November 9, 2010 at 04:26:32
Specs: AIX and Solares
I have a txt file stored under root that has the measurements for all the filesystem and i want to read it using a script. This txt file is an output of the command
# filemon -v -o test.txt -O lv


The file looks like this:

------------------------------------------------------------------------
util #rblk #wblk KB/s volume description
------------------------------------------------------------------------
0.01 0 368 2.5 /dev/hd8 jfs2log
0.00 0 528 3.6 /dev/WAS_XD /WAS_XD
0.00 8 120 0.9 /dev/hd2 /usr
0.00 0 72 0.5 /dev/hd4 /
0.00 0 96 0.7 /dev/hd10opt /opt
0.00 0 32 0.2 /dev/hd9var /var
0.00 0 32 0.2 /dev/hd3 /tmp
#

Now i want to read the contents under the following field and print it on the screen (#rblk #wblk KB/s volume)

Also i want to calculate the average of the values under the field kb/s and output it as
all filesystem io = 3.4 kbps

Here the script does two things:

1. Displays the output in the desired format
2. Displays the average of kbps for all filesystem


See More: how to read a txt file using shell script

Report •

#1
November 9, 2010 at 11:21:58
I will give you the pieces to get you started. This korn shell stub reads each line of the datafile and prints out the 3rd field. I also included a function to determine whether the field is numeric or not:

#!/bin/ksh

isnum()
{
    case $1 in
    ( ?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) )
        return 0;;
    ( ?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])) )
        return 0;;
    *)  return 1;;
    esac
}

while read f1 f2 f3 f4
do

   isnum $f3 && echo "numeric" || echo "NOT numeric"

done < mydata

Also, only the ksh93 version of the shell allows decimal point arithmetic, but the bc calculator command works. Here is an example:

var1="2.5"
var2="3.6"
tot=$(echo "$var1 + $var2"|bc)
echo $tot

Let me know if you have any specific questions.


Report •

#2
June 30, 2011 at 00:41:38
(1)Implement a  Mode calculator(any 4 operations)using Shell Script for two input data.
(2) The data and operation information for the calculator is input through a text file
(3) The task of your script is to fetch the data & operation, execute using the calculator script, store the result in a separate text file.

Send the Steps to follow


Report •
Related Solutions


Ask Question