LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-02-2004, 06:53 AM   #1
jmanjohn61
Member
 
Registered: Dec 2003
Distribution: FC2
Posts: 38

Rep: Reputation: 15
Find variable in template file replace w/date+ and save as


I need to examine a file/template containing specific variables and replacing those variables with current date for 1/10 of the variable and tomorrows date date for another 1/10 of the variable then the day after tomorrow for the next 1/10 of the variable and so on until all 10 variables in the file have been changed. Then save the file with a new name and location.

An example file would be:

On (variable%) there will be no school. Following that day on (variable2%) you will be rsponsible for cleanup. Then on (variable3%) each student needs to place chairs in line.

(variable%) needs todays date
(variable2%) needs tomorrows date
(variable3%) needs the day after tomorrows date and so on

Does anyone have any app or cli that will allow me to read a template file looking for a variable/s and replace the variables with curernt+ dates?

Thanks for your help!

John
 
Old 12-02-2004, 06:59 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Why would you want to write that. A more generic script would be much more usefull. Anyway, the answer is to use date to compute the dates, and sed to do the replacements.

Yves.
 
Old 12-02-2004, 09:12 AM   #3
jmanjohn61
Member
 
Registered: Dec 2003
Distribution: FC2
Posts: 38

Original Poster
Rep: Reputation: 15
I don't understand what you mean by "Why would you want to write that.".
I want something to automate date insertion at pre-determined intervals. Maybe using cron to execute...
I am looking for either an application or script.
I did look at the man pages for both date and sed and I see the potential but not the format.
Would you have something I could work with?

John
 
Old 12-02-2004, 10:45 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Here's the program:
Code:
#!/bin/bash

declare -a MONTHS=(31 28 31 30 31 30 31 31 30 31 30 31)
DATEFORMAT=+%A-%D

function getDate() {
	local year=$(date +%Y)
	local month=$(( $(date +%m) - 1 ))
	local day=$(( $(date +%d) + $1 ))
	while [ $day -lt 1 ] || [ $day -gt ${MONTHS[$month]} ]; do
		if [ $day -lt 1 ]; then
			month=$(( $month - 1 ))
			day=$(( ${MONTHS[$month]} + $day ))
			if [ $month -lt 0 ]; then
				month=11
				year=$(( $year - 1 ))
			fi
		elif [ $day -gt ${MONTHS[$month]} ]; then
			day=$(( $day - ${MONTHS[$month]} ))
			month=$(( $month + 1 ))
			if [ $month -gt 11 ]; then
				month=0
				year=$(( $year + 1 ))
			fi
		fi
	done
	echo "$(date --date=${year}-$(( ${month} + 1 ))-${day} ${DATEFORMAT})"
}

eval echo '"'"$(sed -e 's/\([$"]\)/\\\1/g' -e 's/(DATE\([+-][0-9][0-9]*\)%)/"$(getDate \1)"/g')"'"'
And here's an example:
Code:
$ cat dates.txt | ./tdate.sh
Hello, on Thursday-12/02/04, you will go to "St John Street" and pay your book with $dolars.
Two '2' days later (it will be Saturday-12/04/04), bring the book to me.
        Bye!
with dates.txt being:
Code:
Hello, on (DATE+0%), you will go to "St John Street" and pay your book with $dolars.
Two '2' days later (it will be (DATE+2%)), bring the book to me.
        Bye!
Yves.
 
Old 12-02-2004, 12:56 PM   #5
jmanjohn61
Member
 
Registered: Dec 2003
Distribution: FC2
Posts: 38

Original Poster
Rep: Reputation: 15
WOW!
What about a different date format 2004-12-31? Leap Year?
I appreciate your time!

John
 
Old 12-02-2004, 04:46 PM   #6
jmanjohn61
Member
 
Registered: Dec 2003
Distribution: FC2
Posts: 38

Original Poster
Rep: Reputation: 15
Never mind. I did need an example and was in a rush earlier. Your solution is all that is needed. I 'man'ned each command and I can see now. Thank you Mr. Yves. Again, thank you.

R/S

John
 
Old 12-03-2004, 09:30 AM   #7
jmanjohn61
Member
 
Registered: Dec 2003
Distribution: FC2
Posts: 38

Original Poster
Rep: Reputation: 15
I have one last problem. I taylored this script to meet my needs as best I know. There are somethings in the file I apply tdate.sh that create a return upon executing =

./tdate.sh: line 1: program: command not found

I have my new date format and read through the sed command via man (what a tool). I don't know how sed reads my file to prevent this. Can you help resolve this YinYeti?
 
Old 12-03-2004, 09:41 AM   #8
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
I would need more details to help you. Please prepare those details over the week-end. I won't be able to help you until Monday. Bye,

Yves.
 
Old 12-07-2004, 07:31 AM   #9
jmanjohn61
Member
 
Registered: Dec 2003
Distribution: FC2
Posts: 38

Original Poster
Rep: Reputation: 15
Details.
This file is a sql insert with 'program' (table) prepending each line. The script runs successfully. The purpose of the database is to have a calendar/scheduler table auto-populate. I am making use of an existing application that would require much more change to affect what I need. I have to go a couple steps further by automatically removing old and inserting new rows created by the tdate.sh all by date. Any direction there is also appreciated. I have browsed a number of perl pages to locate what I need, no luck yet.

John
 
Old 12-07-2004, 07:50 AM   #10
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
I won't be able to help you in such a general way. It would take too much time. Please post a specific example: the exact thing that is run, the wanted result, the real result.
 
Old 12-09-2004, 08:39 PM   #11
jmanjohn61
Member
 
Registered: Dec 2003
Distribution: FC2
Posts: 38

Original Poster
Rep: Reputation: 15
Thanks for your help. I have more work with this in the form of a lesson.
The script you wrote only worked for a couple days as it was written for a time period. Any direction on where/what I can get to swim in script to write one that does not blowup after 5 days?

I understand only a little of sed and less of the date definitions and functions. Is there some place I can go to have someone create a script for me? Or a tool that builds?

John
 
Old 12-10-2004, 01:45 AM   #12
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
In truth, the way I handled the date shift is a quick hack, because the important part for your problem, as I see it, is the "eval ..." line, even though it is only one line long. AFAIK, perl or php should give you higher-level functions for shifting the date. Good luck.
 
Old 12-10-2004, 09:20 AM   #13
jmanjohn61
Member
 
Registered: Dec 2003
Distribution: FC2
Posts: 38

Original Poster
Rep: Reputation: 15
Thanks. Why then originally offer date/sed as the solution???
 
Old 12-13-2004, 02:42 AM   #14
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Good question, and easy answer
bash scripting is the art of combining the right tools for each task:
- "date" is very good at giving the individual components of current date.
- "sed" is very good at replacing regular expressions with something else.

As for date-shifting, I thought at first that "date" could handle that, or maybe "cal". And when I saw that it could not, I decided that I'd quickly write a function, just for being able to run an example. Because the way I see it, the eval... line is the line you'd have had a hard time coming up with... wouldn't it?

But Linux is all about sharing knowledge I'm just now in the process of learning PHP (that how I saw, that something could probably be done with dates using PHP), and I don't know Perl. If you solve the date-shifting part, be sure to share

Yves.
 
Old 12-13-2004, 06:49 AM   #15
jmanjohn61
Member
 
Registered: Dec 2003
Distribution: FC2
Posts: 38

Original Poster
Rep: Reputation: 15
Oh I will share. One thing for sure, the sharing did open my eyes. I've started looking into perl and realize everyday, more and more, how much I don't know. I hope to have this perl script up in a couple learning curve weeks (who knows what that is in dog years).

Happy Holidays.

John
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 02:31 AM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
Suse 9.3 Korganizer Custom Schedule, or Save Entire Day as Template -swimmer- Linux - Newbie 0 08-31-2005 09:26 PM
how to cat a text file and save it as a variable mrobertson Programming 37 07-05-2005 08:20 AM
bash script to replace char in variable gmartin Programming 5 11-08-2004 01:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:12 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