Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

sed - removing/renaming file extensions.

Options
  • 30-06-2005 5:26pm
    #1
    Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,690 Mod ✭✭✭✭


    No response in OS. so posting it here..

    I want to copy files into a folder, but if a file of the name already exists I want to rename one of them so I get Filename_X.EXT where X is the next number.

    I can remove file extensions by
    dir | sed s\...$\\ but I need to use the extension later on too.

    Should I be able to use sed h.....$ to store the extension and then append it later ?


    I could use sed to reverse the string, then use FOR (Windows command line) to remove everything up to the first dot and then use sed to reverse the three characters, but that would just be stupid if there is an easier way.

    I could also just process one file type at a time, but would rather a generic way.

    sed scripts I've looked at
    http://sed.sourceforge.net/sed1line.txt
    http://www.student.northpark.edu/pemente/sed/sedfaq.html


Comments

  • Registered Users Posts: 304 ✭✭PhantomBeaker


    Um, my way is probably needlessly complex, but I have a script for ripping cds - cdparanoia which names tracks "track[0-9][0-9].mp3" which I then want to rename to [0-9][0-9].mp3

    There are a coupla other things I use. So what I do is I surround the bit that I want to match, but keep in escaped brackets

    i.e. s/\(basename\)\(.ext\)$/\1\2 \2/g would turn out

    basename.ext .ext

    The \1 and \2 refer to the atoms you surrounded with escaped parentheses. \1 to the first one, \2 to the second... I'm sure you get the picture. So that way you can keep it.

    Hope that helps you with what you wanna do


  • Registered Users Posts: 432 ✭✭Catch_22


    if i had to do this i would use awk, but then again i wouldnt be stuck using windows :P
    so id be able to wtite a regular little shell script as below, but you can get shells for
    windows so this might help
    ( i just typed this in a quick rush so i assume there is typos etc...)

    for file in <all files>
    do
    name=`echo $file |awk -F'.'. '{print $1}'
    ext=`echo $file |awk -F'.'. '{print $2}'

    if [ ! -f $dest/$name.$ext ]; then
    cp $file $dest/$name.$ext
    else
    a=0
    done=0
    while [ $done -eq 0 ]; do
    if [ ! -f $name_$a.$ext ]; then
    cp $file $dest/$name_$a.$ext
    done=1
    else
    a=`expr $a + 1`
    fi
    done
    fi
    done


    Edit: grrr thats alll nicely indented in the bloody edit window, ah well .....


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,690 Mod ✭✭✭✭Capt'n Midnight


    Catch_22 wrote:
    [php]
    for file in <all files>
    do
    name=`echo $file |awk -F'.'. '{print $1}'
    ext=`echo $file |awk -F'.'. '{print $2}'

    if [ ! -f $dest/$name.$ext ]; then
    cp $file $dest/$name.$ext
    else
    a=0
    done=0
    while [ $done -eq 0 ]; do
    if [ ! -f $name_$a.$ext ]; then
    cp $file $dest/$name_$a.$ext
    done=1
    else
    a=`expr $a + 1`
    fi
    done
    fi
    done
    [/php]
    . Just added [ php ] tags - it looks better now , thanks.

    http://gnuwin32.sourceforge.net/packages/gawk.htm


Advertisement