Re: Renaming files with substitution in names
On Mar 19, 8:36am, pk <p...@pk.pk> wrote:
> eskg...@gmail.com wrote:
> > Unfortunately, the files can have different lengths at the beginning.
> > Some can look like this:
>
> > ABC.DEF-2008.03.15.19.53.36-2008.03.15.19.54.37.tpd.gz
>
> > or DEF-2008.03.03.19.53.36-2008.03.04.19.54.37.tpd.gz
>
> > So the begin date and end date do not have to be the same and the
> > beginning can be an arbitrary length before the 2008. After the first
> > 2008, though, all the lengths and positions of everything is the same
> > in all files.
>
> So:
>
> - arbitrary prefix at the beginning, but from some point (the first "2008")
> regular pattern;
> - first occurrence of day of month can differ from second occurrence of day
> of month, but both cannot be less than 2;
> - no spaces in filenames;
> - you always want numbers in two-digits format (my guess).
>
> If the above is correct, then
>
> ls | awk -F. 'BEGIN {OFS="."}
>
> {o=$0;$(NF-5)=sprintf("%02d",$(NF-5)-1);
> $(NF-10)=sprintf("%02d",$(NF-10)-1);
> print "mv "o" "$0}'
>
> As before, pipe the output to sh if you are fine with it.
>
> --
> All the commands are tested with bash and GNU tools, so they may use
> nonstandard features. I try to mention when something is nonstandard (if
> I'm aware of that), but I may miss something. Corrections are welcome.
I don't understand this one too well. I am trying to put it into my
loop that gets the files names. This is what my code looks like:
#!/bin/ksh
cd /home/agwin/tpd
for iFile in `ls | grep 2008.03`
do
echo "original filename = $iFile"
ls | awk -F. 'BEGIN {OFS="."}
{o=$iFile;$(NF-5)=sprintf("%02d",$(NF-5)-1);
$(NF-10)=sprintf("%02d",$(NF-10)-1);
echo "new filename = $o"}'
done
I get this error:
awk: trying to access field -3
record number 86
I am not sure what the problem is. Can someone me? Thanks.
Sorry for all the questions. I am pretty new to ksh programming.
Allyson
|