简体   繁体   中英

Bash Script to Print to CSV File for specified number of lines

I want to be able to print a number of variables to a csv file. The variable differ depending on the file type. And it should also be possible to specific certain variables as options. For example i envisage it running like following:

./pickafile.sh format1 date time(optional) duration(optional)

example given:

./pickafile.sh watchtv 21112014 150000 10

This would do following

#!/bin/sh

var1=date (format DDMMYYYY)
time=time (format HHMMSS)
var2=duration (in seconds)
var3='Watchtv'
cheese='Cheddar'
num=20 #number times to print

case $file_type in
    watch_tv)
        cheese='stilton'
for i in 1..$num
do

## print to a csv file each row adding the 
echo $var1,$time+$var2,$var2,$var3,$cheese > $watch_tv.csv
done
        ;;
    watch_ppl)
        echo 'watch ppl??'
        ;;
    watch_animals)
        echo $'wahtc animals?'
        exit 1
        ;;
    *)
        echo "Unknown file- sure it isn't toxic?"
esac

done
exit

so the output of file would look add 60 seconds to each time period for 20 times

21112014,150000,10,Watchtv,stilton
21112014,150010,10,Watchtv,stilton
21112014,150020,10,Watchtv,stilton
21112014,150030,10,Watchtv,stilton
21112014,150040,10,Watchtv,stilton
21112014,150050,10,Watchtv,stilton
21112014,150100,10,Watchtv,stilton
etc

I transformed your pseudo code into real code, corrected a few paste errrors. I am sure you are able to do the rest of the work.

#!/bin/sh

# $ date; date --date='now +10 Seconds'
# Fri Nov 21 17:28:33 CET 2014
# Fri Nov 21 17:28:43 CET 2014

var1=$(date '+%d%m%Y') # date (format DDMMYYYY)
time=$(date '+%H%M%S') # time (format HHMMSS)
var2=$4 # duration (in seconds)
if [ "" = "$var" ]; then
    var2=10 # default value
fi
# var3='Watchtv'
cheese='Cheddar' # default cheese
num=20 #number times to print

case $file_type in
    watch_tv)
        cheese='stilton'
        ;;
    watch_ppl)
        echo 'watch ppl??'
        ;;
    watch_animals)
        echo $'wahtc animals?'
        exit 1
        ;;
    *)
        echo "Unknown file- sure it isn't toxic?";;
esac

for (( i=1; i<=$num; i++)); do
    offset=$((var2*i)) # calculate the offset
    ## print to a csv file each row adding the
    #echo $var1,$time+$var2,$var2,$var3,$cheese > $watch_tv.csv
    echo "$(date '+%d%m%Y' --date="now +$offset Seconds"),$(date '+%H%M%S' --date="now +$offset Seconds"),$var2,$var3,$cheese"
done

output:

$ bash script.bash watchtv 21112014 150000 10

Unknown file- sure it isn't toxic?
21112014,174412,10,,Cheddar
21112014,174422,10,,Cheddar
21112014,174432,10,,Cheddar
21112014,174442,10,,Cheddar
21112014,174452,10,,Cheddar
21112014,174502,10,,Cheddar
21112014,174512,10,,Cheddar
21112014,174522,10,,Cheddar
21112014,174532,10,,Cheddar
21112014,174542,10,,Cheddar
21112014,174552,10,,Cheddar
21112014,174602,10,,Cheddar
21112014,174612,10,,Cheddar
21112014,174622,10,,Cheddar
21112014,174633,10,,Cheddar
21112014,174643,10,,Cheddar
21112014,174653,10,,Cheddar
21112014,174703,10,,Cheddar
21112014,174713,10,,Cheddar
21112014,174723,10,,Cheddar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM