#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    7
    Rep Power
    0

    Edit Multiple filenames in folder ksh


    I have a folder with a large amount of .mdl and .att files. There are also some with extensions after that .att or .mdl. An example of each:

    810i41VicsFinBenT.att
    ordoxmlRedmanS.mdl.orig
    850i41OwenCorningS.mdl
    ordrespRedman.att
    ordrespRedmanT.mdl

    For the .mdl files, I need to add an 'EG' in front of either the capital 'S' or 'T' that appears before .mdl.

    For the .att files, I need to add an 'EG' simply in right before the .att.

    How can I write a single script to accomplish this that will work for all the different file names I have? I am on a unix machine using ksh. Thanks.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2007
    Posts
    27
    Rep Power
    0
    With ksh93 (/usr/dt/bin/dtksh on Solaris):

    Code:
    for f in *mdl* *att*;do
    	case $f in
    		*S.mdl*) mv "$f" "${f/S.mdl/EGS.mdl}" ;;
    		*T.mdl*) mv "$f" "${f/T.mdl/EGT.mdl}" ;;
    		 *.att*) mv "$f" "${f/.att/EG.att}" ;;
    	esac
    done
    With zsh:

    Code:
    autoload -U zmv
    zmv  '(*)([ST].mdl*|.att*)' '$1EG$2'
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    7
    Rep Power
    0
    Ok, so I guess I am a beginner still at scripts because I can't get the script to work. Do I need to make the script a certain type of executable? Should it be a .sh or .exe file?

    Do I need to insert the #! /bin/ksh at the top?

    Do I need to specify the path of the folder to be edited in the script somewhere?

    How do I run the script on the cmd line?

    Please let me know, Thanks.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2007
    Posts
    27
    Rep Power
    0
    Originally Posted by lspvvq
    [...]
    How do I run the script on the cmd line?
    [...]
    Code:
    $ touch 810i41VicsFinBenT.att \
    > ordoxmlRedmanS.mdl.orig \
    > 850i41OwenCorningS.mdl \
    > ordrespRedman.att \
    > ordrespRedmanT.mdl 
    $ ls
    810i41VicsFinBenT.att    850i41OwenCorningS.mdl   ordoxmlRedmanS.mdl.orig  ordrespRedman.att        ordrespRedmanT.mdl
    $ echo '#!/usr/dt/bin/dtksh
    > for f in *mdl* *att*;do
    > case $f in
    > *S.mdl*) mv "$f" "${f/S.mdl/EGS.mdl}" ;;
    > *T.mdl*) mv "$f" "${f/T.mdl/EGT.mdl}" ;;
    >  *.att*) mv "$f" "${f/.att/EG.att}" ;;
    > esac
    > done
    > ' > script.ksh && chmod 700 script.ksh
    $ ./script.ksh 
    $ ls
    810i41VicsFinBenTEG.att    ordoxmlRedmanEGS.mdl.orig  ordrespRedmanEGT.mdl
    850i41OwenCorningEGS.mdl   ordrespRedmanEG.att        script.ksh

    Addjust the first line (#!/usr/dt/bin/dtksh) for your system;
    by the way, what's your system ?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    7
    Rep Power
    0
    #!/bin/ksh

    for f in *mdl* *att*;do
    case $f in
    *S.mdl*) mv "$f" "${f/S.mdl/EGS.mdl}" ;;
    T.mdl*) mv "$f" "${f/T.mdl/EGT.mdl}" ;;
    *.att*) mv "$f" "${f/.att/EG.att}" ;;
    esac
    done

    The above is exactly what I have saved under edit.ksh. It resides in the folder where the files are to be edited.
    I did: chmod 700 edit.ksh and then ran the file doing: ./edit.ksh

    I get a Bus Error (Core Dump) when I run the file. What am I doing wrong? I am on a Unix machine running ksh. Thanks again.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2007
    Posts
    27
    Rep Power
    0
    Originally Posted by lspvvq
    [...]
    I get a Bus Error (Core Dump) when I run the file. What am I doing wrong? I am on a Unix machine running ksh.
    [...]

    Could you post the output from the following commands:

    Code:
    uname -a
    Code:
    set|fgrep -i ver
    P.S. It's *T.mdl*) , not T.mdl*) !
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    7
    Rep Power
    0

    Command results


    $uname -a
    HP-UX dbsaux05 B.11.23 U 9000/800 186424626 unlimited-user license

    $set|fgrep -i ver
    CLASSPATH=/entgw/webMethods65/IntegrationServer/lib/client.jar
    PATH=/entgw/ai50gw:/entgw/ai50gw/bin:/usr/bin:/opt/ansic/bin:/usr/ccs/bin:/usr/contrib/bin:/usr/contrib/Q4/bin:/opt/perl/bin:/opt/ipf/bin:/opt/hparray/bin:/opt/nettladm/bin:/opt/fcms/bin:/usr/bin/X11:/opt/resmon/bin:/opt/perf/bin:/usr/contrib/kwdb/bin:/opt/wbem/bin:/opt/wbem/sbin:/opt/graphics/common/bin:/opt/prm/bin:/usr/sbin/diag/contrib:/opt/upgrade/bin:/opt/sec_mgmt/bastille/bin:/opt/dsau/bin:/opt/dsau/sbin:/opt/gnome/bin:/opt/mozilla:/opt/sec_mgmt/spc/bin:/opt/ssh/bin:/opt/hpsmh/bin:/opt/gwlm/bin:/opt/ignite/bin:/usr/contrib/bin/X11:/opt/langtools/bin:/opt/imake/bin:/opt/omni/bin:/opt/drd/bin:/usr/local/bin:/opt/java1.4/bin/:/entgw/webMethods65/IntegrationServer/lib:/entgw/webMethods65/IntegrationServer/bin:/entgw/bin:/entgw/scripts:.
    SHLIB_PATH=/entgw/webMethods65/IntegrationServer/lib
    classpath=/entgw/webMethods65/IntegrationServer/lib/client.jar
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2007
    Posts
    27
    Rep Power
    0
    You have ksh88 and therefore this script won't work.
    In order to make it work on your system you have to use bash (/bin/bash).
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    7
    Rep Power
    0

    /bin/bash not found


    Originally Posted by radoulov
    You have ksh88 and therefore this script won't work.
    In order to make it work on your system you have to use bash (/bin/bash).
    I looked in my bin and usr directories but could not find bash. Is there a certain directory it would be in or is it possible I don't have that shell? How can I search for it more efficiently?

    If I don't have that shell is it impossible to run the script?

    Thanks.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2007
    Posts
    27
    Rep Power
    0
    Not elegant and not tested.
    Test it first (use echo instead of mv)!

    Code:
    for f in *mdl* *att*;do
    	case $f in
    		*S.mdl*) mv "$f" "${f%S.mdl*}"EGS.mdl"${f#*S.mdl}";;
    		*T.mdl*) mv "$f" "${f%T.mdl*}"EGT.mdl"${f#*T.mdl}";;
    		 *.att*) mv "$f" "${f%.att*}"EG.att"${f#*.att}";;
    	esac
    done
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    7
    Rep Power
    0

    Bus Error (Core Dump)


    Originally Posted by radoulov
    Not elegant and not tested.
    Test it first (use echo instead of mv)!

    Code:
    for f in *mdl* *att*;do
    	case $f in
    		*S.mdl*) mv "$f" "${f%S.mdl*}"EGS.mdl"${f#*S.mdl}";;
    		*T.mdl*) mv "$f" "${f%T.mdl*}"EGT.mdl"${f#*T.mdl}";;
    		 *.att*) mv "$f" "${f%.att*}"EG.att"${f#*.att}";;
    	esac
    done
    Ok, so I tried the fix with echo in place of the mv's and I got a Bus Error (Core Dump) error again. Any ideas? Appreciate your help.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2007
    Posts
    27
    Rep Power
    0
    I see, what's your current shell (ps -p $$)?
    And how many files are in the directory (ls|wc -l)?
    And could you try the script with /bin/sh?
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    7
    Rep Power
    0
    Originally Posted by radoulov
    I see, what's your current shell (ps -p $$)?
    And how many files are in the directory (ls|wc -l)?
    And could you try the script with /bin/sh?
    I tried changing ksh to sh but got the error:

    $./edit.ksh
    ./edit.ksh[6]: "${f/T.mdl/EGT.mdl}": The specified substitution is not valid for this command.

    $ps - p $$
    PID TTY TIME COMMAND
    18420 pts/2 0:00 ps
    18195 pts/2 0:00 ksh

    $ls|wc -l
    87
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2007
    Posts
    27
    Rep Power
    0
    Originally Posted by lspvvq
    I tried changing ksh to sh but got the error:

    $./edit.ksh
    ./edit.ksh[6]: "${f/T.mdl/EGT.mdl}": The specified substitution is not valid for this command
    [...]
    Use the modified version:

    Code:
    ... mv "$f" "${f%S.mdl*}"EGS.mdl"${f#*S.mdl}" ...

IMN logo majestic logo threadwatch logo seochat tools logo