Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have the following short perl script rename.pl. How can I translate this script to one liner perl syntax? And how to combine the perl one liner in find command?

rename.pl

#!/usr/bin/perl

$op = shift;
for (@ARGV) {
  $was = $_;
  eval $op;
  die $@ if $@;
  rename($was,$_) unless $was eq $_;
}

My current find syntax:

find /tmp -name "*$NAME*" -exec \
  ./rename.pl \
  's/\Q$ENV{NAME_THAT_WE_WANT_TO_CHANGE}\E/$1$ENV{NEW_NAME}$2/' {} +

The find syntax that I want to build:

find /tmp -name "*$NAME*" -exec \
  < perl one liner line ....... > \
  's/\Q$ENV{NAME_THAT_WE_WANT_TO_CHANGE}\E/$1$ENV{NEW_NAME}$2/' {} +
share|improve this question
    
OTOH, you could use File::Find in the script turning the whole thing into a simple command. I know that seems crazy as you will not get to spend as much time typing and making mistakes, but... $0.02 –  TAFKA 'goldilocks' Mar 13 '13 at 13:23
    
@goldilocks, if he's not familiar with perl (he copy-pasted that perl code), then he's more likely to make mistakes with that, and it's probably going to be less efficient. He could use mmv or zmv that are designed for that kind of task (or even rename, an off the shelf perl script for that). –  Stephane Chazelas Mar 13 '13 at 13:46
    
Good point. I had taken "I have the following short perl script" to mean something like "this is my script, it works" (and now I want to make it a one liner). –  TAFKA 'goldilocks' Mar 13 '13 at 13:54
add comment

1 Answer

up vote 2 down vote accepted

Simply give the content of the script as an argument following -e:

find .... -exec perl -e '
  $op = shift;
  for (@ARGV) {
    $was = $_;
    eval $op;
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
  }' s/a/b/g {} +

You can condense it to:

find ... -exec perl -e '$op=shift;for(@ARGV){$was=$_;eval$op;die$@if$@;rename$was,$_ unless$was eq$_}' s/a/b/g {} +

But that doesn't help legibility.

share|improve this answer
    
not clearly how to set your first example in the find syntax ? , can you give me full example ? –  yael Mar 13 '13 at 13:31
    
WOW - this is great , thanx –  yael Mar 13 '13 at 14:23
    
this is the last update or you need to test your syntax to be sure that its work ? –  yael Mar 13 '13 at 14:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.