LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-07-2005, 12:58 PM   #1
petenyce
Member
 
Registered: Sep 2005
Posts: 39

Rep: Reputation: 15
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
 
Old 10-07-2005, 01:39 PM   #2
pippo
Member
 
Registered: Aug 2005
Distribution: Gentoo
Posts: 38

Rep: Reputation: 15
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
 
Old 10-07-2005, 01:52 PM   #3
petenyce
Member
 
Registered: Sep 2005
Posts: 39

Original Poster
Rep: Reputation: 15
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

Last edited by petenyce; 10-07-2005 at 01:57 PM.
 
Old 10-07-2005, 02:31 PM   #4
pippo
Member
 
Registered: Aug 2005
Distribution: Gentoo
Posts: 38

Rep: Reputation: 15
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.
 
Old 10-07-2005, 02:36 PM   #5
petenyce
Member
 
Registered: Sep 2005
Posts: 39

Original Poster
Rep: Reputation: 15
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
 
Old 10-07-2005, 02:45 PM   #6
pippo
Member
 
Registered: Aug 2005
Distribution: Gentoo
Posts: 38

Rep: Reputation: 15
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.
 
Old 10-07-2005, 03:16 PM   #7
petenyce
Member
 
Registered: Sep 2005
Posts: 39

Original Poster
Rep: Reputation: 15
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
 
Old 10-11-2005, 04:14 AM   #8
Lintroll
LQ Newbie
 
Registered: Oct 2005
Posts: 2

Rep: Reputation: 0
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

 
Old 10-12-2005, 02:36 PM   #9
pippo
Member
 
Registered: Aug 2005
Distribution: Gentoo
Posts: 38

Rep: Reputation: 15
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 :-)
 
Old 10-13-2005, 12:16 PM   #10
Lintroll
LQ Newbie
 
Registered: Oct 2005
Posts: 2

Rep: Reputation: 0
Thumbs up

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.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Timestamp of file/directories Ephracis Linux - Software 1 12-16-2004 09:21 AM
adding date timestamp to filename markraem Solaris / OpenSolaris 2 11-17-2004 08:32 AM
Converting the date (not timestamp) mpgram Programming 2 04-18-2004 03:56 PM
Get the date on PHP and insert it into SQL poeta_boy Programming 4 11-26-2003 03:25 AM
add timestamp to name of file how? supafly Linux - General 3 11-14-2001 11:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration