Operating System - HP-UX
1824984 Members
3392 Online
109678 Solutions
New Discussion юеВ

Re: Awk guru wanted: Rename filenames in a directory.

 
Maurinho
Occasional Contributor

Awk guru wanted: Rename filenames in a directory.

The file generated by the application has the following naming conventions: DBI1F1_YYMMDD
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 ..
Mauro
10 REPLIES 10
Rodney Hills
Honored Contributor

Re: Awk guru wanted: Rename filenames in a directory.

The following perl program should do the trick-

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
There be dragons...
Chris Wilshaw
Honored Contributor

Re: Awk guru wanted: Rename filenames in a directory.

I think this may do it.

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.
Rich Wright
Trusted Contributor

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
Maurinho
Occasional Contributor

Re: Awk guru wanted: Rename filenames in a directory.

Great answers ; but I didn't explained correctly. File is generated by application as DBI1F1_YYMMDD.dat where:

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 ..
Mauro
Kenneth_19
Trusted Contributor

Re: Awk guru wanted: Rename filenames in a directory.

Maybe you can try this:

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
Always take care of your dearest before it is too late
Kenneth_19
Trusted Contributor

Re: Awk guru wanted: Rename filenames in a directory.

The script should look like this:

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
Always take care of your dearest before it is too late
Ralph Grothe
Honored Contributor

Re: Awk guru wanted: Rename filenames in a directory.

Like Rodney I'd also favour Perl to have this done.
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).
Madness, thy name is system administration
Reinhard Burger
Frequent Advisor

Re: Awk guru wanted: Rename filenames in a directory.

Dear Mauro
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
keep it simple
Reinhard Burger
Frequent Advisor

Re: Awk guru wanted: Rename filenames in a directory.

Hi once again me
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
keep it simple
Rodney Hills
Honored Contributor

Re: Awk guru wanted: Rename filenames in a directory.

Ralph,

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
There be dragons...