Re: Adding a leading zero
Re: Adding a leading zero
- Subject: Re: Adding a leading zero
- From: "Mark J. Reed" <email@hidden>
- Date: Thu, 5 Apr 2007 11:14:07 -0400
Let me apologize publicly to Gunno for the tone of my reply. I wasn't in the best of moods, having just returned from a long trip to a funeral, but that's no excuse to be rude. As I said in my private reply to him, I'm not really an arrogant jerk; I just play one on the Internet. :\
I also wanted to add some clarifications to the actual content; this is all off-topic for an AS list, so feel free to skip, but when doing tricks with do shell script it may come in handy.
On 4/4/07, Mark J. Reed <email@hidden> wrote:
If you're on Tiger, use the stat command.
stat -f %Sm -t "%b%d%Y" /Users/gunno/Library/Preferences/Flexklockan.plist
The stat command, named after the system call stat(2), provides access to similar pieces of information to what's in the "Get Info" dialog in the Finder. The argument to -f tells it which pieces of info you want, along with how to print those pieces in the output. In general, %(stuff)m means the file modification time; the particular (stuff) tells it how to return it. The (stuff) in this case is a capital S, means "format the time using strftime(3)". That means you need a format string to pass to strftime, and it is specified as the argument to -t.
In strftime formats, %b is replaced by the abbreviated month name, %d by the day of the month, and %Y by the full year number.
Unfortunately, the /usr/bin/stat command is not available on Panther and earlier. In an earlier message I mentioned the GNU coreutils and fileutils packages, which provide the stat command on Linux, but I couldn't find a version in Fink or MacPorts for OS X <=
10.3. Compiling it yourself from source is of course an option.
If you're not on Tiger, your best bet would be to move up from awk to one of the next generation of scripting languages, the usual suspects being Perl, Python, Ruby, Tcl. As covered in the above earlier threads, all provide an easy way to get at the modification time of a file and format it however you want.
Relatively easy, anyway. Here are examples for all four of the above langs:
perl -MPOSIX -MFile::stat -le 'print strftime("%b%d%Y", localtime(stat("/path/to/file")->mtime))'
python -c 'import os; import time; print time.strftime("%b%d%Y", time.localtime(os.stat("/path/to/file").st_mtime))'
ruby -e 'puts File.stat("/path/to/file").mtime.strftime("%b%d%Y")'
echo 'puts [clock format [file mtime {/path/to/file} ] -format {%b%d%Y} ]' | tclsh
For future reference, however, awk has printf, which means it's trivial to get any number of leading zeroes:
...once you know how...
ls -l /Users/gunno/Library/Preferences/Flexklockan.plist | awk '{printf "%ss%s\n", $6, $7, $9}'
In printf, "%s" is a placeholder that means "put a string here"; the particular string to put there is the next one in the list after the format string, which is processed in order. The printf function comes from the C language, in which the values are just undifferentiated bits, so the specifier has to indicate the type of the data; but in awk, everything can be treated as a string.
%s = string goes here
%2s = string goes here, padded to two places with leading space if needed
s = string goes here, padded to two places with leading zeroes if needed
And \n means newline/carriage return/linefeed/whatever you call it.
The values are taken in order from the arguments after the format string - the first %blah is replaced by the first value, second %blah by the second value, etc.
So printf("%ss%s\n", $6, $7, $9) prints out $6 as-is, followed by $7 padded to two spaces with zeroes as needed, followed by $9, followed by a return. (In awk, regular print automatically includes the return at the end, but printf does not).
--
Mark J. Reed <
email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden