Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip characters out of a filename.

Status
Not open for further replies.

pcutler

Technical User
Jan 18, 2002
59
CA
Hi,

I tried to search for an answer to this in the forum, but the search feature is down.

I need a script that will shorten “filename_20070405.txt” to “filename_070405.txt”

In other words, I need to strip out the 11th and 12th characters of the filename.

I’m very new to PERL, so thanks for your help.

PC
 
Untested
There are more compact ways but this shows the steps
Code:
$file="filename_20070405.txt”

my $start=substr($file,0,9);
my $end=substr($file,11,(length($file)-11));
my $new_filename=$start.$end;

Keith
 
or:

Code:
[blue]$filename[/blue] = [red]'[/red][purple]filename_20070405.txt[/purple][red]'[/red][red];[/red]
[blue]$filename[/blue] =~ [red]s/[/red][purple]_[purple][b]\d[/b][/purple][purple][b]\d[/b][/purple][/purple][red]/[/red][purple]_[/purple][red]/[/red][red];[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$filename[/blue][red];[/red]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top