- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Awk guru wanted: Rename filenames in a directo...
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
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
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
тАО07-31-2002 07:15 AM
тАО07-31-2002 07:15 AM
Awk guru wanted: Rename filenames in a directory.
DBI=INTERFACE, 1= ID (1,2), F = Indicator (S,C,M,E), 1= Order (1,2,3,4), _ = underscore and YYMMDD = Year, Month and day.
Order 1=P , 2=E, 3=R and 4=D
Field 6 on original filename (1,2,3,4)and field 2 on renamed file (P,E,R,D)
I need a script to rename the filenames in a directory from DBI1F1_YYMMDD to FPMMDDYY.txt
Thanks ..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2002 07:51 AM
тАО07-31-2002 07:51 AM
Re: Awk guru wanted: Rename filenames in a directory.
opendir(DIR,".");
%mapord={1,P,2,E,3,R,4,D};
while($fn=readdir(DIR)) {
if ($fn=~/DBI(\d)(\b)(\d)_(......)/) {
$id=$1; $indicator=$2; $order=$3; $ymd=$4;
$newfil=$indicator . $mapord{$order} . $ymd;
rename("$fn","$newfil") unless -f $newfil
}
}
If the new name generated already exists, then it will leave the file alone.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2002 07:59 AM
тАО07-31-2002 07:59 AM
Re: Awk guru wanted: Rename filenames in a directory.
eg using files
fil1F1_020728
fil1F1_020729
fil1F1_020730
fil1F1_020731
for file in `ls fil*`
do
NEW=`echo $file | awk '{printf("%s%s%s%s%s\n",substr($0,5,1),"P",substr($0,12,2),substr($0,10,2),substr($0,8,2))}'`
echo $NEW
cp -p $f $NEW.txt
done
I've used cp rather than mv at the end for safety - you can then check that the right files have the right names
the resultant files are called
FP280702.txt
FP290702.txt
FP300702.txt
FP310702.txt
I couldn't work out the exact requirements for the P from your description, so I've just coded it in a "P" in the awk command. If it's a positional character in the filename, you just need to add another substr section,
substr($0,X,Y)
where X is the starting character, and Y is the number of characters required. $0 is the filename passed to awk from the loop.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2002 09:37 AM
тАО07-31-2002 09:37 AM
Re: Awk guru wanted: Rename filenames in a directory.
Here is a "sed" solution.
for x in $(ls DBI*)
do
cp $x $(echo $x | sed 's/^....\(..\).\(....\)\(..\)/\1\3\2.txt/')
done
My test using a 13 character file name produced the following command.
cp 1234567890abc 56bc890a.txt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-01-2002 04:03 PM
тАО08-01-2002 04:03 PM
Re: Awk guru wanted: Rename filenames in a directory.
DBI1= Constant
F= Indicator (valuesF,M,C & E)
1= Order (values 1,2,3 & 4)
Where 1=P, 2=E, 3=R & 4=D
Rename filename:
DBI1F1_YYMMDD.dat=FPMMDDYY.txt
DBI1F2_YYMMDD.dat=FEMMDDYY.txt
DBI1C4_YYMMDD.dat=CDMMDDYY.txt
DBI1M3_YYMMDD.dat=MRMMDDYY.txt
Hope this can help..
Thanks ..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-01-2002 06:47 PM
тАО08-01-2002 06:47 PM
Re: Awk guru wanted: Rename filenames in a directory.
for FILE in `ls DBI*`
do
cp $FILE $(echo $FILE | cut -c 5)$(echo $FILE | cut -c 6 | tr '1234' 'PERD')$(echo $FILE | sed 's/^.......\\(....\\)\\(..\\)....
/\\2\\1.txt/')
done
Regards,
Kenneth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-01-2002 06:48 PM
тАО08-01-2002 06:48 PM
Re: Awk guru wanted: Rename filenames in a directory.
for FILE in `ls DBI*`
do
cp $FILE $(echo $FILE | cut -c 5)$(echo $FILE | cut -c 6 | tr '1234' 'PERD')$(echo $FILE | sed 's/^.......\(....\)\(..\)....
/\2\1.txt/')
done
Regards,
Kenneth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-01-2002 11:22 PM
тАО08-01-2002 11:22 PM
Re: Awk guru wanted: Rename filenames in a directory.
But I think in Rodney's code snippet are some minute flaws.
First his %map_order hash shouldn't work the way he put it because the curly braces yield a hashref.
Instead it should be round parentheses.
Then the literals in the hash need to be quoted.
You can avoid quoting at least of the hash's keys by using the => syntax.
The rename() would only work if you did the readdir() in the current directory (or cd'ed in it).
Otherwise you should have to prepend the full path to the file names, which is always safer (see "perldoc -f readdir").
I attached yet another Perl snippet which only concentrates on the mapping of the filenames (influenced by Schwartz'ian transforms ;-).
Thus I only read some filenames from a _data_ block.
Just keep the opendir(), readdir(), rename(), closedir() stuff from Rodney (but use full pathnames in rename).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2002 12:33 AM
тАО08-02-2002 12:33 AM
Re: Awk guru wanted: Rename filenames in a directory.
please find as attachement a shell script that will do the job.
start it without params. It will ask for needed input.
Filenames to be renamed can be like examples below :
* as a wildcard for all files
AAAA* as a prefix and a wildcard This will rename all files starting with AAAA regardless of their actual extension
*.dat this will rename all files having a extension of dat
When the script is asking for the new extension enter it WITHOUT the dot (.)
There is also a test option that will show on screen what will be the output if you run it not in test mode.
Hope it is what you are looking for
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2002 12:39 AM
тАО08-02-2002 12:39 AM
Re: Awk guru wanted: Rename filenames in a directory.
I forgot one very important thing
I 'm using this script since approx. 3 years.
But it was not me who wrote the script originally . I just translated the messages into englsh (was originally in german).
the author of this script was the sysadm of my hp-ux box at that time.
his name is Ladislaus Markuly.
just to make clear that it's not my own idea how to handle such a case. ( don't want to be seen as creator of stuff that's not really mine)
Reinhard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2002 06:15 AM
тАО08-02-2002 06:15 AM
Re: Awk guru wanted: Rename filenames in a directory.
Thanks for pointing out the issue about the hashref in my code. I had intended the user to have cd'd to the directory before processing.
-- Rod Hills