How to Create New file every time with new output

2k views Asked by At

I have shell Script which is currently running in every 5 minutes (i configured in crontab) and sending all the output in same file.

  • I want that in every 5 minute(when cronjob will exexute) the output of the script should be send in new file with "current_date_year_time.csv"

Current Script

#!/bin/sh
ipath=/usr/local/nextone/bin
ifile=/var/tmp/EndpointUsage.csv
"$ipath"/cli iedge list | awk '
BEGIN { print "------------------------------------";
    printf "|%-17s|%-16s|\n","Registration ID", "Ongoing Calls"
  }
/Registration ID/ { id = $3; next }
/Ongoing Calls/ {print "------------------------------------"; printf  "|%-17s|%-16s|\n",id,$3 }
END{
print "------------------------------------";
}'>> "$ifile"

Current output is coming like as below.

EndpointUsage.csv

I want something like below.

EndpointUsage_09:55-Am_09_05_2015.csv
EndpointUsage_10.00-Am_09_05_2015.csv
EndpointUsage_10:05-Am_09_05_2015.csv

Can anyone please help me on this. Thank you in advance.

2

There are 2 answers

0
mattias On

I edited your script to suite your needs, see below:

#!/bin/sh
ipath=/usr/local/nextone/bin
date=$(date +"%Y%m%d%H%M")
ifile=EndpointUsage$date.csv
"$ipath"/cli iedge list | awk '
BEGIN { print "------------------------------------";
    printf "|%-17s|%-16s|\n","Registration ID", "Ongoing Calls"
  }
/Registration ID/ { id = $3; next }
/Ongoing Calls/ {print "------------------------------------"; printf      "|%-17s|%-16s|\n",id,$3 }
END{
print "------------------------------------";
}'>> "$ifile"

The additions I made was

date=$(date +"%Y%m%d%H%M")
ifile=EndpointUsage$date.csv

Format the timestamp according to your needs.

0
RSchulze On

You could change the ifile= line to something like this (use 'date' to format the path/file you want to write the output into)

ifile="$(date '+/var/tmp/EndpointUsage_%I:%M-%p_%d_%m_%Y.csv')"

The man (or info) page for date has a list of all the placeholders you can use. http://linux.die.net/man/1/date