How do I represent "the file previous to this one" in Korn Shell?
November 22, 2005 5:09 PM Subscribe
Korn Shell Filter: I have a list of time ordered files, with Julian day of year as part of the filename. There is not a file for every Julian day. If I grep on a directory listing for file *{DOY}*, and that file does not exist, I would like my script to be smart enough to use the file immediately preceding it in the listing, even if the preceding file is not *{DOY-1}*. My Google-Fu has failed me. Any suggestions?
Best answer: Here's a suggestion. Do two things: if the file XXX{DOY}XXX exists then you're fine, just go ahead and use the file. If the file XXX{DOY}XXX does not exist, then touch XXX{DOY}XXX, then look for the file preceding it in the directory listing, remove XXX{DOY}XXX and then then use the file you found preceding it.
posted by rdr at 5:45 PM on November 22, 2005
posted by rdr at 5:45 PM on November 22, 2005
Best answer: Use
Also, you may have to play with the
posted by rjt at 5:56 PM on November 22, 2005
sed
for the string handling:
f=`ls "$dir" | sed "/$doy/!d;q"` if [ "$f" = "" ] ; then f=`ls "$dir" | sed "/$doy/,$d" | tail -1` fi if [ "$f" = "" ] ; then echo "no file on or before $doy" >&2 exit 1 fiYou should also be able to combine that
tail
with the preceding sed
but I couldn't get that working in the few minutes I had.Also, you may have to play with the
/$doy/
pattern if you want to make sure that if you're checking for, say, the 10th day of the year that you don't accidentally match the 100th (or 101st, 102nd, …)posted by rjt at 5:56 PM on November 22, 2005
Best answer: Haven't tried this, but what about something like this (assuming DOY is already set and $file points to the filename):
posted by aberrant at 5:57 PM on November 22, 2005
while ([ -e $file ]); do
(( DOY=$DOY-1 ))
file="whatever*\{$DOY\}*whatever"
done
echo "file found: $file"
posted by aberrant at 5:57 PM on November 22, 2005
If the file XXX{DOY}XXX does not exist, touch XXX{DOY}XXX, then look for the file preceding it in the directory listing
I like rdr's answer best. To easily get the preceding file in that case, at least if you have gnu grep, just "ls files | grep -B 1 XXX | head -1"
posted by sfenders at 6:58 PM on November 22, 2005
I like rdr's answer best. To easily get the preceding file in that case, at least if you have gnu grep, just "ls files | grep -B 1 XXX | head -1"
posted by sfenders at 6:58 PM on November 22, 2005
Any reason you're not using a regexp in ksh to solve this problem?
posted by Ethereal Bligh at 2:13 AM on November 23, 2005
posted by Ethereal Bligh at 2:13 AM on November 23, 2005
Response by poster: Sorry it took me a while to get back to this post. Thanks everybody for your help. Lots of good advice in this thread.
posted by Fat Guy at 12:06 PM on December 1, 2005
posted by Fat Guy at 12:06 PM on December 1, 2005
Response by poster: Also the reason I am using ksh is that is what we have at work. I assumed that was the only shell installed on our system. All of our scripts are written in ksh.
posted by Fat Guy at 12:12 PM on December 1, 2005
posted by Fat Guy at 12:12 PM on December 1, 2005
This thread is closed to new comments.
Personally I'd probably use a more advanced scripting language.
posted by RustyBrooks at 5:40 PM on November 22, 2005