|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I have a bunch of files that need name changes (about 1000). Each file
looks like this: ABC-2008.03.15.19.53.36-2008.03.15.19.54.37.tpd.gz I need to have the names look like this: ABC-2008.03.14.19.53.36-2008.03.14.19.54.37.tpd.gz So, I need to have the day of the month subtracted by 1. The days and times are all different from March 2 - March 19. Is there some ksh or perl script that would make this easy to do? Thanks. Allyson |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 2008-03-19, eskgwin@gmail.com <eskgwin@gmail.com> wrote:
> > > I have a bunch of files that need name changes (about 1000). Each file > looks like this: > > ABC-2008.03.15.19.53.36-2008.03.15.19.54.37.tpd.gz > > I need to have the names look like this: > > ABC-2008.03.14.19.53.36-2008.03.14.19.54.37.tpd.gz > > So, I need to have the day of the month subtracted by 1. The days and > times are all different from March 2 - March 19. > > Is there some ksh or perl script that would make this easy to do? > Thanks. > for oldname in * ; do newname=$(echo "$oldname" | awk 'BEGIN{OFS=FS="."} $2==3 && $3>1{$8=$3=$3-1} {print}') echo "$newname" #uncomment the following line if the filenames look OK #mv "$oldname" "$newname" done |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
eskgwin@gmail.com wrote:
> I have a bunch of files that need name changes (about 1000). Each file > looks like this: > > ABC-2008.03.15.19.53.36-2008.03.15.19.54.37.tpd.gz > > I need to have the names look like this: > > ABC-2008.03.14.19.53.36-2008.03.14.19.54.37.tpd.gz > > So, I need to have the day of the month subtracted by 1. The days and > times are all different from March 2 - March 19. No March 1; that's simplifying things. > Is there some ksh or perl script that would make this easy to do? I suppose including awk wouldn't be bad either? awk -F. -v OFS=. '$3--&&$8--' ....will change a filename given through stdin as requested. for f in ABC-*.tpd.gz do ff=$( echo "$f" | awk -F. -v OFS=. '$3--&&$8--' ) mv -i "$f" "$ff" done You can do that also faster in pure shell separating the components #!/bin/ksh echo "$f" | IFS=. read -r f1 f2 f3 f4 f5 f6 f7 f8 fRest echo "$f1.$f2.$((f3-1)).$f4.$f5.$f6.$f7.$((f8-1)).$fRest" and embed that in a loop like done above. Janis > Thanks. > > Allyson |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
eskgwin@gmail.com wrote:
> I have a bunch of files that need name changes (about 1000). Each file > looks like this: > > ABC-2008.03.15.19.53.36-2008.03.15.19.54.37.tpd.gz > > I need to have the names look like this: > > ABC-2008.03.14.19.53.36-2008.03.14.19.54.37.tpd.gz > > So, I need to have the day of the month subtracted by 1. The days and > times are all different from March 2 - March 19. > > Is there some ksh or perl script that would make this easy to do? One way could be this (assuming no spaces in filenames and that day of month is always the same in first and second occurrence): cd /dir/wth/the/files ls | awk -F'.' 'BEGIN {OFS="."} {o=$0;$3=$8=sprintf("%02d",--$3) rint"mv "o" "$0}'if you are fine with the results, then just pipe the output to sh. -- 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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Janis Papanagnou wrote:
> eskgwin@gmail.com wrote: >> I have a bunch of files that need name changes (about 1000). Each file >> looks like this: >> >> ABC-2008.03.15.19.53.36-2008.03.15.19.54.37.tpd.gz >> >> I need to have the names look like this: >> >> ABC-2008.03.14.19.53.36-2008.03.14.19.54.37.tpd.gz >> >> So, I need to have the day of the month subtracted by 1. The days and >> times are all different from March 2 - March 19. > > No March 1; that's simplifying things. > >> Is there some ksh or perl script that would make this easy to do? > > I suppose including awk wouldn't be bad either? > > awk -F. -v OFS=. '$3--&&$8--' [...] or (if 1 instead of 01 is not a problem): awk -F. '$3=--$8' OFS=. Regards Dimitre |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Janis Papanagnou wrote:
> I suppose including awk wouldn't be bad either? > > awk -F. -v OFS=. '$3--&&$8--' > > ...will change a filename given through stdin as requested. But when a number becomes < 10, it will use only a single digit in the resulting file name, which might or might not be what the OP wants. (my guess is that he would like to preserve alignment, since I see some "03"s in the file name, but I may of course be wrong). -- 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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Mar 19, 7:24am, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote: > eskg...@gmail.com wrote: > > I have a bunch of files that need name changes (about 1000). Each file > > looks like this: > > > ABC-2008.03.15.19.53.36-2008.03.15.19.54.37.tpd.gz > > > I need to have the names look like this: > > > ABC-2008.03.14.19.53.36-2008.03.14.19.54.37.tpd.gz > > > So, I need to have the day of the month subtracted by 1. The days and > > times are all different from March 2 - March 19. > > No March 1; that's simplifying things. > > > Is there some ksh or perl script that would make this easy to do? > > I suppose including awk wouldn't be bad either? > > awk -F. -v OFS=. '$3--&&$8--' > > ...will change a filename given through stdin as requested. > > for f in ABC-*.tpd.gz > do > ff=$( echo "$f" | awk -F. -v OFS=. '$3--&&$8--' ) > mv -i "$f" "$ff" > done > > You can do that also faster in pure shell separating the components > > #!/bin/ksh > > echo "$f" | IFS=. read -r f1 f2 f3 f4 f5 f6 f7 f8 fRest > echo "$f1.$f2.$((f3-1)).$f4.$f5.$f6.$f7.$((f8-1)).$fRest" > > and embed that in a loop like done above. > > Janis > > > > > Thanks. > > > Allyson- Hide quoted text - > > - Show quoted text - I like this solution because to me it seems the simplest. 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. Thanks for all the . Allyson |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|