renaming files using split with a delimiter

I have a directory of files that I need to rename by splitting the first and second halves of the filenames using the delimiter "-O" and then renaming with the second half first, followed by two underscores and then the first half. For example, natfinal1995annvol1_14.pdf -O filenum-20639 will be renamed to: filenum-20639__natfinal1995annvol1_14.pdf. There is always a -O in the filenames. I'm a newbie to Perl, but it looks like the way to go.

Thanks.

Hi,

Try this,

#!/usr/bin/perl

$filename='natfinal1995annvol1_14.pdf -O filenum-20639';
if ( $filename =~ /(.+?)\s-O\s(.*)/ ) { print $2,"__",$1,"\n"; }
1 Like

That's great. Thank you.