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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to do a substitute in a value of a variable?

Status
Not open for further replies.

jtripper6

Technical User
Oct 18, 2008
4
US
I have a directory where a majority of filenames are in the format of basename.ext.new. What I want to do is rename them basename.ext using a perl script. I'm not sure, however, if it's possible to do a substitution on the value of a variable.
Code:
#!/usr/bin/perl
  2 @fnames = qx{ls};
  3 $i = 1;
  4 $elements = @fnames;
  5 print "\@fnames has $elements elements.\n";
  6 foreach $element (@fnames) {
  7   if ($element =~ /.new$/) {
  8      print "$i: $element";
  9      $i++;
 10   }
 11 }
If the file ends with .new I want to remove it. I can do it in a shell using the following command:
Code:
ls | awk '{print $1}' | perl -pe 's/(.new)//'
I've been unable to incorporate the second part where the substitution occurs. Any ideas?
Thanks,
jt
 
Are you wanting to rename actual files?

Code:
[url=http://perldoc.perl.org/functions/opendir.html][black][b]opendir[/b][/black][/url][red]([/red]DIR, [red]'[/red][purple]path/to/directory[/purple][red]'[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@files[/blue] = [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red][red]/[/red][purple][purple][b]\.[/b][/purple]new$[/purple][red]/[/red][red]}[/red] [url=http://perldoc.perl.org/functions/readdir.html][black][b]readdir[/b][/black][/url] DIR[red];[/red]
[url=http://perldoc.perl.org/functions/closedir.html][black][b]closedir[/b][/black][/url] DIR[red];[/red]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$file[/blue] [red]([/red][blue]@files[/blue][red])[/red] [red]{[/red]
   [black][b]my[/b][/black] [blue]$new[/blue] = [blue]$file[/blue][red];[/red]
   [blue]$new[/blue] =~ [red]s/[/red][purple][purple][b]\.[/b][/purple]new$[/purple][red]/[/red][purple][/purple][red]/[/red][red];[/red] 
   [url=http://perldoc.perl.org/functions/rename.html][black][b]rename[/b][/black][/url] [red]([/red][red]"[/red][purple]path/to/[blue]$file[/blue][/purple][red]"[/red], [red]"[/red][purple]path/to/[blue]$new[/blue][/purple][red]"[/red][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
[red]}[/red]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Do you really need to use Perl? With bash, you could just say:
[tt]
for F in *.new; do mv $F $(sed 's/\.new$//' <<< $F); done
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top