Operating System - HP-UX
1830234 Members
2516 Online
109999 Solutions
New Discussion

Script for file name change

 
SOLVED
Go to solution
SKR_1
Trusted Contributor

Script for file name change

Hi All,
I am seeking a URGENT help for changing the filenames ( almost 45000+)
I need a script which will change ( basically reduce) the filenames. My filnaming convention basically like the following
_.key
example of the filenames are following..

AQULEMPONesusyrloseK50SDS_AUSTRIAEnglish.key
AQUARECOnfjesseK50SDS_FINLANDFinnish.key
AMBELOndijsjd_THE_NETHERLANDSCzech.key
BLANOSERefinedCMC7H4XSDS_BULGARIAItalian.key

if you notice the second part of the file name have countryname and language name together. This is the area where we are trying to reduce the characters. Each country and name has to be shorten as per the given standard. some example given below.

Country substitutes:
AUSTRIA AT
BELGIUM BE
BULGARIA BG
CZECH CZ
DENMARK DK

Languages substitutes:
English E
German D
Italian I
Spanish S
Finnish U
so what I am expecting is the replace ment like in the bewlow example

Original file:
AMBERGUMTEMPROES_THE_NETHERLANDSEnglish.key
New file name:
AMBERGUMTEMPROES_THE_NLE.key

Can anyone please help me on this?

SKR
12 REPLIES 12
Tim Nelson
Honored Contributor

Re: Script for file name change

Why opening another one ???

Patience young grasshopper..

Here is one example of a way. Use a case statement for each lang, then instead of echoing use the mv command to rename.

ls /tmp/file*|while read file
do
case $file in
*nether*) NEW=`echo $file|sed -e s/nether_eng/NED_ENG/g'
echo $NEW;;
*) echo "no match"
esac
done


James R. Ferguson
Acclaimed Contributor

Re: Script for file name change

Hi:

You could do this with 'sed'. For example:

# F=AQULEMPONesusyrloseK50SDS_AUSTRIAEnglish.key

# echo ${F}|sed -e 's/_AUSTRIA/_AT/;s/English.key/E.key/'
AQULEMPONesusyrloseK50SDS_ATE.key

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Script for file name change

Hi (again):

Of course, you don't say whether or not these filenames (all 45,000) are actually *files* you want to rename or are *filenames* in some file. Knowing that distinction would make the solution suggested easier.

Regards!

...JRF...
SKR_1
Trusted Contributor

Re: Script for file name change

Hi.. Thanks to all for the quick response.
They all are individual files stored under one directory. I want to rename these file names
SKR_1
Trusted Contributor

Re: Script for file name change

Hi
One more update
When we replace the country, sometime language will also get change.
for ex: if I want to change CZECHCzech.key, both will get replaced. What I need is CZECH( country name) should be replaced with CZ and Czech ( Language) with C
Note: All country names are in uppercase. while langaguages first character is uppercase and remining will be lower case.
ex.
CZECH - country name
Czech - language
Tim Nelson
Honored Contributor

Re: Script for file name change

How bout run it the first time to fix CZECH and then another time with mods to fix Czech ? ( too much brain power for a Friday to fix all options in one sweep)



James R. Ferguson
Acclaimed Contributor

Re: Script for file name change

Hi (again):

> When we replace the country, sometime language will also get change.
for ex: if I want to change CZECHCzech.key, both will get replaced. What I need is CZECH( country name) should be replaced with CZ and Czech ( Language) with C

Consider:

# F=AQULEMPONesusyrloseK50SDS_CZECHCzech.key
# echo $F|sed -e 's/_CZECH/_CZ/;s/Czech.key/C.key/'
AQULEMPONesusyrloseK50SDS_CZC.key

Now, since you are acutally renaming files, Tim's initial approach is fine insofar as the driving code. Simply add a 'mv' of the original filename to the new filename.

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor
Solution

Re: Script for file name change

Basically as Tim and JRF have said, you can use sed(1) to change the names. (You may want to put some separator between the country and language so you could change them back.)

If you only have less than 100 changes, you can just use one long sed command or script file.
NEW=$(echo ${F} | sed -e 's/AUSTRIA/AT/' \
-e 's/BELGIUM/BE/' \
...
-e 's/English/_E/' \
...
)
(I inserted a "_" before the language.)

Then just:
mv $(F) $(NEW)

You may want to echo the above mv so you can check whether your script is correct.

Also, a meta question is: How do I write a script to write that sed script?
Give your above input in a file "map", you would write an awk script to turn it into the sed script file:
Country substitutes:
AUSTRIA AT
...

Languages substitutes:
English E
...

awk '
NF == 2 && $2 != "substitutes" {
print "s/" $1 "/" $2 "/"
}' map > sed_script_file

Then use "sed -f sed_script_file".
Hein van den Heuvel
Honored Contributor

Re: Script for file name change

I would do this with a perl script (some reader will not be surprised :-)...

For example:

---------- translate.pl ---------
use strict;
use warnings;
my %countries = qw(
AUSTRIA AT
BELGIUM BE
BULGARIA BG
CZECH CZ
DENMARK DK
NETHERLANDS NL
);

my %languages = qw(
English E
German D
Italian I
Spanish S
Finnish U
);

while (<>) {
chomp;
my $old = $_;
if (/_([A-Z]+)([A-Z][a-z]+)\./) {
my $country = $countries{$1};
my $language = $languages{$2};
if (!defined($country)) {
print STDERR "Unknown Country $1 from $old \n";
next
}
if (!defined($language)) {
print STDERR "Unknown Language $2 from $old \n";
next
}
my $new = $` . '_' . $country . $language . '.' . $';
print "rename $old, $new;\n";
}
}
----------------------

Using the files you list runs as:

$ ls tmp/*.key | perl translate.pl | more
Unknown Language Czech from tmp/AMBELOndijsjd_THE_NETHERLANDSCzech.key
Unknown Country FINLAND from tmp/AQUARECOnfjesseK50SDS_FINLANDFinnish.key
rename tmp/AQULEMPONesusyrloseK50SDS_AUSTRIAEnglish.key, tmp/AQULEMPONesusyrloseK50SDS_ATE.key;
rename tmp/BLANOSERefinedCMC7H4XSDS_BULGARIAItalian.key, tmp/BLANOSERefinedCMC7H4XSDS_BGI.key;


When you like what you see, change that last print statement into the real rename:
rename $old, $new;

Enjoy!
Hein.

Hein van den Heuvel
Honored Contributor

Re: Script for file name change

I meant to add a generic unrecognide file warning.

Change
rename $old, $new;
}

To
rename $old, $new;
} else {
print STDERR "Unrecognized file: $old\n"
}

fwiw... I thought it was a nice twist to be able to retain the suggested tables verbatim for inclusing in loading the translation array.


Hein.

SKR_1
Trusted Contributor

Re: Script for file name change

Thanks to all of you for helping me.

Thanks

SKR
Dennis Handly
Acclaimed Contributor

Re: Script for file name change

>Thanks to all of you for helping me.

If you have gotten the answers you wanted, please read the following about assigning points and reopening threads:
http://forums.itrc.hp.com/service/forums/helptips.do?#33
http://forums.itrc.hp.com/service/forums/helptips.do?#41