LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Insert date and timestamp Into File name (https://www.linuxquestions.org/questions/linux-newbie-8/insert-date-and-timestamp-into-file-name-370691/)

petenyce 10-07-2005 12:58 PM

Insert date and timestamp Into File name
 
I have a script that produces a txt file. but i want to be able to name the file automatically using the system date and time :

Referraldate&timestamp.txt


Select * into outfile '/tmp/referral.txt' fileds terminated by ';' lines terminated by '\n' ehpadmin.Referral_Authorization



Now instead of the file being named referral.txt

i want it to show referraldate&timestamp.txt

or just the date

thanks

pippo 10-07-2005 01:39 PM

Hi!

The simplest thing would be a shell script that creates a SQL script with the file name you need.

echo "select ... into outfile/tmp"$(whoami)$(date +%d%m%y)".txt ..." >> myscript.sql

You can save this script in a file named file.sh and launch it with

sh ./file.sh

Good luck!

Pippo

petenyce 10-07-2005 01:52 PM

ok
 
Well i have test.sh calling my refferal.sql

here is my code in refferal.sql that i tried

SELECT * INTO OUTFILE '/tmp/"$(whoami)$(date+d%m%y)".txt'
FIELDS TERMINATED BY ';' LINES TERMINATED BY '\N' FROM ehpadmin.Referral_Authorization


this script worked before calline the file referral.txt i added your suggestion.... I want the file name to read system date.txt

pippo 10-07-2005 02:31 PM

I am not sure I understand your question. So here is what I suggested.

You can put in the file script1.sh

echo "select... /tmp"$(whoami)$(date +%d%m%y)".txt ..." >> script2.sql

Running it with

sh ./script1.sh

will create the script script2.sql which is your original script with the file name made of the user name and the date .txt
Then you can just run MySQL with it.

In short, my solution consists in a script that generates your initial script with the output file name you need.

petenyce 10-07-2005 02:36 PM

ok
 
I just wnat my file name to be the date? thats it. right now my file name is referral.txt i just want it to be todays date&timestamp.txt

pippo 10-07-2005 02:45 PM

The solution is to put today's_date.txt as your file name in your sql script. To do that, you can generate this script with another command.

echo "select ... /tmp"$(date +%d%m%y)".txt ... >> script.sql

This will create your script with the file name being date.txt

Each time you do an sql request, you will have to regenerate your script with that command.

In turn, to make this more convenient, you can put this command in a script.

So running my command will generate you script that you will use like before but it will each time contain the right file name.

petenyce 10-07-2005 03:16 PM

Ok Heres the confusion
 
Below is my test.sh and my referral.sql now i want the .txt file to be named
the date.txt


test.sh
#!/bin/bash
/usr/bin/mysql -u root ehpadmin </usr/etc/referral.sql


referral.sql
SELECT * INTO OUTFILE '/tmp/referral.txt'
FIELDS TERMINATED BY ';' LINES TERMINATED BY '\N' FROM Referral_Authorization

Try to incorporate what you said before into my scripts thats why im confused with echo statment??? my test.sh calls my referral.sql
thanks for help

Lintroll 10-11-2005 04:14 AM

I think you're seriously overcomplicating things, Pippo :)

Adding the date to the filename is as easy as adding the following string to the filename: `date +%d_%m_%y, thus:
/usr/bin/mysql -u root ehpadmin > /usr/etc/referral_`date +%d_%m_%y`.sql

:)

pippo 10-12-2005 02:36 PM

I must admit that I did not suggest the simplest solution. This is the least I can say. Yours is much more simple.

As a matter of fact, I took it the wrong way. I asked myself 'How can I modify the preexisting script to insert the date into the file name'. And, when the question was stated in these terms, the first thing that popped into my mind is another script.

But I do that kind of things with everything :-)

Lintroll 10-13-2005 12:16 PM

Quote:

Originally posted by pippo
I must admit that I did not suggest the simplest solution. This is the least I can say. Yours is much more simple.

As a matter of fact, I took it the wrong way. I asked myself 'How can I modify the preexisting script to insert the date into the file name'. And, when the question was stated in these terms, the first thing that popped into my mind is another script.

But I do that kind of things with everything :-)

Yes, That is what I thought. And I don't really think it's wrong, just another way of doing it. To each his own and all that. :)


All times are GMT -5. The time now is 10:25 PM.