Find variable in template file replace w/date+ and save as
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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?
#!/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!
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.
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?
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.
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.
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?
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.
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
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.