PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Renaming files with substitution in names
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Renaming files with substitution in names

Réponse
 
LinkBack Outils de la discussion
Vieux 19/03/2008, 14h29   #1
eskgwin@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Renaming files with substitution in names

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
  Réponse avec citation
Vieux 19/03/2008, 15h01   #2
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Renaming files with substitution in names

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
  Réponse avec citation
Vieux 19/03/2008, 15h24   #3
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Renaming files with substitution in names

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

  Réponse avec citation
Vieux 19/03/2008, 15h30   #4
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Renaming files with substitution in names

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.
  Réponse avec citation
Vieux 19/03/2008, 15h33   #5
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Renaming files with substitution in names

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
  Réponse avec citation
Vieux 19/03/2008, 15h46   #6
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Renaming files with substitution in names

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.
  Réponse avec citation
Vieux 19/03/2008, 15h57   #7
eskgwin@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Renaming files with substitution in names

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
  Réponse avec citation
Vieux 19/03/2008, 16h34   #8
eskgwin@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 09h52.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15951 seconds with 16 queries