Create a formated text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create a formated text file
# 1  
Old 12-09-2008
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  
Old 12-09-2008
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  
Old 12-09-2008
Hi vidyadhar85,

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

Mike
# 4  
Old 12-09-2008
Quote:
Originally Posted by msb65
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  
Old 12-09-2008
yes, i will reinput them every time i run the script. I won't specify them as command line parameters.
# 6  
Old 12-09-2008
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
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create a text file and a pdf file from Linux command results.

Hello. The task : Using multiple commands like : gdisk -l $SOME_DISK >> $SOME_FILEI generate some text file. For readiness I must insert page break. When the program is finished I want to convert the final text file to a pdf file. When finished, I got two files : One text file and One pdf... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Splitting a text file into smaller files with awk, how to create a different name for each new file

Hello, I have some large text files that look like, putrescine Mrv1583 01041713302D 6 5 0 0 0 0 999 V2000 2.0928 -0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 5.6650 0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 3.5217 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

3. Shell Programming and Scripting

Create csv from text file

Gents, I am trying to create a csv file using the file attached. I have a problem to get all information required because the rows are not continues. Here is my code till now. awk ' /"ffid"/{if(s){print s;s=$NF}else{s=$NF}} /"LineNumber"/{s=s $NF} /"PointNumber"/{s=s $NF}... (4 Replies)
Discussion started by: jiam912
4 Replies

4. Shell Programming and Scripting

Copy text and create new file

I have a directory with ~500 files that look like below. I need to copy each unique entry up until the second_ only once and put a .txt in place of the _. I am not quite sure how but have the below as a startThank you :). cp -u file1.txt "$(cat output.txt)" file1.txt ... (4 Replies)
Discussion started by: cmccabe
4 Replies

5. Shell Programming and Scripting

Script to create a text file whose content is the text of another files

Hello everyone, I work under Ubuntu 11.10 (c-shell) I need a script to create a new text file whose content is the text of another text files that are in the directory $DIRMAIL at this moment. I will show you an example: - On the one hand, there is a directory $DIRMAIL where there are... (1 Reply)
Discussion started by: tenteyu
1 Replies

6. Red Hat

create pdf of text file help

Can someone please tell me why this is not working? I have created numerous pdf's from text files by following these instructions and this time it is not working. Convert jpeg files to PDF under Linux | bitPrison.net convert /home/liveuser/Documents/hw7 /home/liveuser/Documents/hw7.pdf... (5 Replies)
Discussion started by: cokedude
5 Replies

7. UNIX for Dummies Questions & Answers

Script to create text file.

Hi all Below this is my script..I want to write the command to create a text file in my script below. If anyone know how to do...show me the result.I also want to do this script run automatically without type in terminal. Thanks. #!/usr/bin/sh... (6 Replies)
Discussion started by: mastercar
6 Replies

8. Shell Programming and Scripting

Create multiple text file from a single text file on AIX

Hi I need to create multiple text files from onc text file on AIX. The data of text files is as below: ********************************************** ********************************************** DBVERIFY: Release 10.2.0.4.0 - Production on Tue Nov 10 13:45:42 2009 Copyright (c) 1982,... (11 Replies)
Discussion started by: lodhi1978
11 Replies

9. Shell Programming and Scripting

Create a file with aleatory text

Can someone guide me on how to write a small script to create a text file with non-sense text, i.e. lorem ipsum... I would like it with about 5 paragraphs but I have no idea on how to perfrom that. Any help will be much appreciated. Thanks, (5 Replies)
Discussion started by: agasamapetilon
5 Replies

10. UNIX for Dummies Questions & Answers

create a text file in a script

Hi, i want to create a text file (init${x}.ora) and write information to it via a korn shell script. Is it right to do it as shown below (the file doesnt exist yet)? x=$1 file="$ORC/dbs/init${x}.ora" echo "info here..." >> $file will this file get created? (2 Replies)
Discussion started by: n8575
2 Replies
Login or Register to Ask a Question