- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Script for file name change
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2008 09:33 AM
08-22-2008 09:33 AM
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
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2008 09:39 AM
08-22-2008 09:39 AM
Re: Script for file name change
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2008 09:47 AM
08-22-2008 09:47 AM
Re: Script for file name change
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2008 09:50 AM
08-22-2008 09:50 AM
Re: Script for file name change
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2008 09:54 AM
08-22-2008 09:54 AM
Re: Script for file name change
They all are individual files stored under one directory. I want to rename these file names
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2008 09:59 AM
08-22-2008 09:59 AM
Re: Script for file name change
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2008 10:12 AM
08-22-2008 10:12 AM
Re: Script for file name change
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2008 10:45 AM
08-22-2008 10:45 AM
Re: Script for file name change
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2008 06:58 PM
08-22-2008 06:58 PM
SolutionIf 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".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2008 07:40 PM
08-22-2008 07:40 PM
Re: Script for file name change
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2008 08:53 PM
08-22-2008 08:53 PM
Re: Script for file name change
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2008 01:13 AM
08-25-2008 01:13 AM
Re: Script for file name change
Thanks
SKR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2008 02:16 PM
08-25-2008 02:16 PM
Re: Script for file name change
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