The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-09-2008
msb65 msb65 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 89
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
###########################################################################
###########################################################################
  #2 (permalink)  
Old 12-09-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,391
you could have done this script in much more easy way..
anyways you want to read it to excel so you have to creat a .CSV file..
first type this in the command line or you can take care inside the script while running it first time by checking the file exists or not...
Code:
echo "Mean Systolic,Mean Diastolic,Mean Heartrate" > formatedfile.csv
later inside script replace your last 3 echo statments by one echo statement
Code:
echo "$MEAN_SYSTOLIC,$MEAN_DIASTOLIC,$MEAN_HEARTRATE" >>formatedfile.csv
  #3 (permalink)  
Old 12-09-2008
msb65 msb65 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 89
Hi vidyadhar85,

Thanks for the reply! How could I simplify the script?

Mike
  #4 (permalink)  
Old 12-09-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,391
Quote:
Originally Posted by msb65 View Post
Hi vidyadhar85,

Thanks for the reply! How could I simplify the script?

Mike
you can before that i wanna clarify some things
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??
  #5 (permalink)  
Old 12-09-2008
msb65 msb65 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 89
yes, i will reinput them every time i run the script. I won't specify them as command line parameters.
  #6 (permalink)  
Old 12-09-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,391
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
but since you are entering the values manually i don't think you need to check for the five elements entered or not right??
if you avoid that it became 10 line code
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0