AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.


#Power
 View Only
  • 1.  [NEWBIE Q] Howto mv files according to regular expression

    Posted Wed April 18, 2007 09:56 AM

    Originally posted by: SystemAdmin


    Is there an aix equivalent to the non-posix (but common) rename(1) command?
    Where?

    <desired>
    rename(1) substitute filenames that match regular expressions

    Summary:
    rename 'match-regular-expression' 'replace-string' file http://...
    </desired>

    <example>
    ksh> ls
    abc.cpp vm.h foo.c bar.cpp x.C

    ksh> rename '.cpp' '.C' *.cpp

    ksh> ls
    abc.C vm.h foo.c bar.C x.C
    </example>

    <example>
    ksh> ls received
    pic1.png pic2.png pic3.png

    ksh> for f in received/*.png; do convert "$f" "$f".gif; done
    ksh> ls received
    pic1.png pic.png.gif pic2.png pic2.png.gif pic3.png pic3.png.gif

    ksh> rename png.gif gif received/*.png.gif
    ksh> ls received
    pic1.png pic.gif pic2.png pic2.gif pic3.png pic3.gif

    ksh> rm received/*.png

    ksh> ls received
    pic.gif pic2.gif pic3.gif
    </example>

    We are currently using a 20-line perl script which has hacked from a
    2-line perl snippet.
    Before spending time to identify the correct gnu package
    and porting rename(1),
    I am asking if there is already an aix equivalent extension (e.g. mv option)

    TIA,
    #AIX-Forum


  • 2.  Re: [NEWBIE Q] Howto mv files according to regular expression

    Posted Wed April 18, 2007 10:37 AM

    Originally posted by: SystemAdmin


    I do not know of such a command. I would suggest grabbing the GNU package and compiling it and then installing it on your machine.

    You'll want to check out the Linux Toolbox for AIX: http://www-03.ibm.com/servers/aix/products/aixos/linux/download.html
    which has a version of GCC and make that can be installed on your machine. That should make the task of compiling the rename package easier as you should not have to 'port' it, just download, configure, compile, and install the package.
    #AIX-Forum


  • 3.  Re: [NEWBIE Q] Howto mv files according to regular expression

    Posted Wed April 18, 2007 11:34 AM

    Originally posted by: SystemAdmin


    On the other hand,
    something like this simple Korn shell script might do the job. Note, this comes wil no guarantees or warrantees.

    Here's a sample run:
    $ ls
    file.txt
    $ myrename .txt .TXT *
    $ ls
    file.TXT
    $

    Note, the mv command will need to be uncommented after you verify this will work.

    #!/bin/ksh

    1. $1 is the expression to search for
    2. $2 is the replacement string

    1. for all files matching third argument, provided by the shell
    for file in $3
    do
    # check if each filename matches the req expression
    exists=`echo $file | grep -c $1`
    if $exists != 0 # If file matches
    then
    # Use sed to replace matched reqular expression in
    # the filename
    newfile=`echo $file | sed -e "s|$1|$2|"`
    echo "old name >$file< new name >$newfile<"
    #mv $file $newfile
    fi
    done
    #AIX-Forum


  • 4.  Re: [NEWBIE Q] Howto mv files according to regular expression

    Posted Wed April 18, 2007 04:45 PM

    Originally posted by: SystemAdmin


    Thank you for the sh implementation.

    We started with the following perl snippet for string match and replace.

    perl -e '($m,$r,@f)=@ARGV;$foreach $f(@f){$t=$f;$t=~s:$m:$r:;rename "$f","$t";}'

    As our files have spaces, punctuation, and unprintable characters,
    we now have a ~20-line perl script that handles our current set,
    but we are seeking a low-maintenance alternative to our "growing" :) script.

    Within the next few days, as time permits, I hope to identify and download
    the correct rpm (not coreutils) from the toolbox.

    Thank you,
    #AIX-Forum


  • 5.  Re: [NEWBIE Q] Howto mv files according to regular expression

    Posted Thu April 19, 2007 07:11 AM

    Originally posted by: SystemAdmin


    It's dead easy in Perl

    #!/usr/bin/perl
    $op = shift or die;
    chomp(@ARGV = <STDIN>) unless @ARGV;
    for (@ARGV) {
    $was = $_;
    eval $op;
    rename($was,$_) unless $was eq $_;
    }

    This is from one of the O'Reilly books on Perl.
    http://people.scs.fsu.edu/~burkardt/pl_src/rename/rename.html

    HTH

    Jim Lane
    #AIX-Forum


  • 6.  [Solution] Re: [NEWBIE Q] Howto mv files according to regular expression

    Posted Fri April 20, 2007 10:20 AM

    Originally posted by: SystemAdmin


    Jim Lane,

    > It's dead easy in Perl
    > ...
    > This is from one of the O'Reilly books on Perl.
    > http://people.scs.fsu.edu/~burkardt/pl_src/rename/rename.html

    This is what we needed.

    We've named it mvViaPerlsub to avoid usage confusion with rename(1),
    and I plan to add perldoc for our users.

    Thank you,

    #AIX-Forum