Is the date on the last line of the file? If so, we have a nice easy job identifying the line... We could use sed to do the replacement. Here's a little shell script which would do it, assuming the date is on the last line, is the first date on that line, and is on YYYY-MM-DD format (my favorite date format - no confusion between British and American "day-or-month-first" silliness, plus dates in this format sort in dictionary sort order, which is super-useful!):
Code:
#!/bin/bash
file=$HOME/Desktop/file_to_work_on
today=$(date +%Y-%m-%d)
sed -r -i "$ s/[0-9]{4}-[0-9]{2}-[0-9]{2}/$today/" "$file"
Some explanation:
The file= line should be obvious - we're going to use the filename several times - we might as well put the name in a variable, and then if we need to change it, there's only one place to modify.
The today= line calls the date command with a format string to get YYYY-MM-DD format, and assigns the result to the "today" variable. In general, if you want to take the output of a command, and use it as part of another command, that's how you do it - using the $(command) syntax. You might also see `backticks` used for this purpose, but I prefer $() because it's less easily confused (people always confuse ` with ' - they're very different to the shell! Also $() is nestable.
Finally, the hard bit. sed.
Sed is the
stream
editor. It's a pretty simple program, but is phenomenally useful. It reads input, does some operations on it a line at a time, and prints the output (or when the -i option is used, as we have, saves the changes to the input file).
Firstly, before sed it actually run, the shell looks at the arguments to sed,
Code:
-r
-i
"$ s/[0-9]{4}-[0-9]{2}-[0-9]{2}/$today/"
"$file"
and expands anything it sees which needs expanding. This includes variables with the $ symbol before them, so $today takes the value of the variable (which we set in the previous line), and file as well. Quotes are stripped, but the shell has already separated the arguments, so spaces in the arguments stay in the arguments - they do not become delimiters (that was the purpose of quoting them in the first place)
So the arguments become:
Code:
-r
-i
$ s/[0-9]{4}-[0-9]{2}-[0-9]{2}/2007-01-15/
/home/username/Desktop/file_to_work_on
Sed's syntax (which you can read about the in the sed manual page) is such that first argument after any options is treated as a sed command to perform on the input, which is read from files named in any subsequent arguments.
The argument beginning "$ s/" means "on the last line of the input file, substitute the first occurrence of four digits, followed by a hyphen, followed by two digits, followed by a hyphen followed by two digits, with 2007-01-15". This may seem cryptic. If you want to understand it (and it IS worth it), google for "regular expressions", and "sed tutorial".
So the question is - does this fit your needs?