![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| awk to find a formated o/p | aaysa123 | Shell Programming and Scripting | 5 | 10-17-2008 09:01 AM |
| Create a file with aleatory text | agasamapetilon | Shell Programming and Scripting | 5 | 09-08-2008 04:57 PM |
| create a text file in a script | n8575 | UNIX for Dummies Questions & Answers | 2 | 02-07-2006 12:38 PM |
| 1.44mb disk formated = 1.38mb left?? | G-wizz | Windows & DOS: Issues & Discussions | 4 | 01-13-2004 02:27 PM |
| Disk Formated with NTFS | bache_gowda | SuSE | 4 | 11-19-2003 05:05 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Create a formated text file
Hi,
I have written a BASH shell script (included below) which will allow me to monitor my blood pressure. The script computes the mean of 5 user input systolic, diastolic, and heart rate values. I would like the script to then append these three values to their respective columns in a text file. How do I do this? I would like the text file to be formated so that it can be read into EXCEL. Thanks a lot. Mike Code:
#!/bin/bash
###########################################################################
###########################################################################
SYSTOLIC=(120 130 125 124 122)
DIASTOLIC=(90 80 75 89 73)
HEARTRATE=(60 70 80 85 75)
###########################################################################
###########################################################################
###########################################################################
###########################################################################
#Perform an initial check:
echo; echo; echo 'Performing initial check:'
#Check that SYSTOLIC has 5 elements:
if [ ${#SYSTOLIC[@]} -eq 5 ]; then
echo 'SYSTOLIC has 5 elements:' ${SYSTOLIC[@]}
else
echo; echo; echo 'ERROR: SYSTOLIC does not have 5 elements'
exit 1
fi
#Check that DIASTOLIC has 5 elements:
if [ ${#DIASTOLIC[@]} -eq 5 ]; then
echo 'DIASTOLIC has 5 elements:' ${DIASTOLIC[@]}
else
echo; echo; echo 'ERROR: DIASTOLIC does not have 5 elements'
exit 1
fi
#Check that HEARTRATE has 5 elements:
if [ ${#HEARTRATE[@]} -eq 5 ]; then
echo 'HEARTRATE has 5 elements:' ${HEARTRATE[@]}
else
echo; echo; echo 'ERROR: HEARTRATE does not have 5 elements'
exit 1
fi
echo 'Initial check complete'
###########################################################################
###########################################################################
###########################################################################
###########################################################################
#Compute the mean blood pressure and heart rate values:
echo; echo; echo 'Computing mean blood pressure and heart rate values:'
let MEAN_SYSTOLIC=(${SYSTOLIC[0]} + ${SYSTOLIC[1]} + ${SYSTOLIC[2]} + ${SYSTOLIC[3]} + ${SYSTOLIC[4]})/5
let MEAN_DIASTOLIC=(${DIASTOLIC[0]} + ${DIASTOLIC[1]} + ${DIASTOLIC[2]} + ${DIASTOLIC[3]} + ${DIASTOLIC[4]})/5
let MEAN_HEARTRATE=(${HEARTRATE[0]} + ${HEARTRATE[1]} + ${HEARTRATE[2]} + ${HEARTRATE[3]} + ${HEARTRATE[4]})/5
echo 'Mean Systolic:' $MEAN_SYSTOLIC
echo 'Mean Diastolic:' $MEAN_DIASTOLIC
echo 'Mean Heartrate:' $MEAN_HEARTRATE
###########################################################################
###########################################################################
|
|
|||||
|
Quote:
are you gonna hard core your input values??as you did in the above script i mean to array?? or you will provide them as argument to script?? |
|
|||||
|
it can be done some thing like this
Code:
SYSTOLIC="120 130 125 124 122"
DIASTOLIC="90 80 75 89 73"
HEARTRATE="60 70 80 85 75"
len=`echo $SYSTOLIC|awk 'BEGIN{RS=" "}END{print NR}'`
if [ $len -eq 5 ] ; then
MEAN_SYSTOLIC=`echo $SYSTOLIC|awk 'BEGIN{RS=" "}{sum += $0}END{print sum/5}'`
else
echo 'ERROR: SYSTOLIC does not have 5 elements'
exit 1
fi
len=`echo $DIASTOLIC|awk 'BEGIN{RS=" "}END{print NR}'`
if [ $len -eq 5 ] ; then
MEAN_DIASTOLIC=`echo $DIASTOLIC|awk 'BEGIN{RS=" "}{sum += $0}END{print sum/5}'`
else
echo 'ERROR: DIASTOLIC does not have 5 elements'
exit 1
fi
len=`echo $HEARTRATE|awk 'BEGIN{RS=" "}END{print NR}'`
if [ $len -eq 5 ] ; then
MEAN_HEARTRATE=`echo $HEARTRATE|awk 'BEGIN{RS=" "}{sum += $0}END{print sum/5}'`
else
echo 'ERROR: HEARTRATE does not have 5 elements'
exit 1
fi
echo "$MEAN_SYSTOLIC,$MEAN_DIASTOLIC,$MEAN_HEARTRATE" >> file.csv
if you avoid that it became 10 line code ![]() |
![]() |
| Bookmarks |
| Tags |
| shell script, shell scripting, unix scripting, unix scripting basics |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|