Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2003
    Posts
    23

    Unanswered: insert character in name file

    Hello

    We have x files in a directory, never they have the same name, and the length name is also different

    ie.

    pedi04011999999999.txt
    salidft2001dfa100104011999999999.txt

    I would like change the name insert a character "." , for every eight characters

    ie

    pedi0401.19999999.99.txt
    salidft2.001dfa10.01040119.99999999.txt



    hope someone can help.

    thanks

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    This assumes that the filenames will not currently contain more than one '.' (and that each has a file extension).

    for fileName in *
    do

    newFileName=$(echo ${fileName%.*} | sed 's/.\{8\}/&\./g').${fileName#*.}
    echo mv ${fileName} ${newFileName} #remove 'echo' when you're happy

    done
    Last edited by Damian Ibbotson; 01-19-04 at 13:21.

  3. #3
    Join Date
    Sep 2003
    Posts
    23

    Smile

    I happy now, thats work fine.

    Thanks Damian
    Regards

  4. #4
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hi,
    there is a little mistake in Damians solution.
    If the filename is 8,16,... long then you have two points between the
    filename and the extension. Like this :
    salidft2001dfa100104011999999999.txt ==> salidft2.001dfa10.01040119.99999999..txt
    12345678.txt ==> 12345678..txt

    I have another solution for you. It is a little bit larger but it works how you want it.

    The same as Damian said :
    This assumes that the filenames will not currently contain more than one '.' (and that each has a file extension).

    for oldFile in `ls *.txt`
    do
    newFile=`echo $oldFile | awk ' BEGIN { FS="."}
    {
    lang=length($1)
    x=int(lang / 8)
    y=lang / 8
    newFile=""
    i=0
    while (i < x)
    {
    newFile=newFile""substr($1,i*8+1,8)"."
    i=i+1
    }
    if ( x == y )
    {
    newFile=newFile""substr($1,i*8+1)""$2
    }
    else
    {
    newFile=newFile""substr($1,i*8+1)"."$2
    }
    print newFile
    }'`
    #The same as Damian said :
    echo mv $oldFile $newFile #remove 'echo' when you're happy
    done
    Last edited by fla5do; 01-20-04 at 19:51.
    Greetings from germany
    Peter F.

  5. #5
    Join Date
    Jul 2003
    Location
    Edinburgh
    Posts
    35
    Good point Peter.

    Piping the command through sed 's/\.$//' will also fix the double periods.

  6. #6
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Just for the hell of it, you could do the substitution all in one sed command...

    echo $fileName | sed '{
    :insDot
    /[^.]\{9\}/ {
    s/\([^.]\{8\}\)\([^.]\)/\1.\2/
    b insDot
    }
    }'

    This basically says: if the fileName contains a 9 character string that does not contain a '.', place a '.' after the 8th character. Repeat until the 9 character string is not found.

    It's not perfect as for example, the string "123.1234567812345678" would be transformed to "123.12345678.12345678". Sant's original requirements suggest that this should really be "123.1234.56781234.5678".

    Damian

  7. #7
    Join Date
    Jul 2003
    Location
    Edinburgh
    Posts
    35
    I know gotos are considered harmful, but wow! There's a lot more to sed than I first reckoned ...

  8. #8
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hi Damian,
    i am brim over with enthusiasm about your knowledge.
    It is realy great.

    Hi periods Chillies ;-),
    excuse my bad englisch. Now I know that the point is a dot or a period.
    Thanks for adjust me.
    Greetings from germany
    Peter F.

  9. #9
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Sed is horrible! It does the job *really* quick but even when you write the stuff yourself, it can be a nightmare to figure out what you were trying to do.

    Take a look at this (aaagh!)...

    http://www.dbnet.ece.ntua.gr/~george/sed/OLD/dc.sed.txt

  10. #10
    Join Date
    Jul 2003
    Location
    Edinburgh
    Posts
    35
    Ow! ow! ow!

    I think Greg had too much time on his hands, waiting for processes to finish.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •