Find the answer to your Linux question:
Results 1 to 5 of 5
Dear all, I've a folder with some files inside. Synthax: field.field.field.ext But I would like to replace the . by _ or space. We give me this command: sed -e ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    22

    Replace char with sed


    Dear all,

    I've a folder with some files inside. Synthax: field.field.field.ext

    But I would like to replace the . by _ or space.
    We give me this command: sed -e 's/./_/g'

    But I'm not an expert in script. But this will replace the point of my extensions too...

    How can I do to replace only the point in the name and not the point of the extension in all files in one times?

    Mouglou

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    477
    let "A" be the line "field.field.field.ext"
    then

    rev A | sed 's/\./#/' | sed 's/\./_/g' | sed 's/#/\./' |rev >B

    should leave "B" as "field_field_field.ext"
    the sun is new every day (heraclitus)

  3. #3
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    627
    Dear all,

    I've a folder with some files inside. Syntax: field.field.field.ext

    But I would like to replace the . by _ or space.
    We give me this command: sed -e 's/./_/g'

    But I'm not an expert in script. But this will replace the point of my extensions too...

    How can I do to replace only the point in the name and not the point of the extension in all files in one times?

    Mouglou
    I beleive that you are asking how to rename files within a directory and not names in a file?? If that is the case, then use the script:
    Code:
    #!/bin/bash
    
    for fullfilename in "$@"; do
        ext="${fullfilename##*\.}"
        name="${fullfilename%\.${ext}}"
        mv "${fullfilename}" "${name//./_}.${ext}"
    done
    Which is run as:
    Code:
    cd directorylocation
    scriptname *
    Be sure to chmod the script to 755 before attempting to run the script.

  4. #4
    Just Joined!
    Join Date
    Aug 2010
    Posts
    15
    I tried and got a similar style to tpl. Its like that ,

    sed 's/\./_/g' inputfile | rev | sed 's/\_/\./' | rev > outputfile

    Sed is really great.

  5. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    627
    Doing renaming of files within a directory ....
    Code:
    bash$ function testing
    > {
    >     for fullfilename in "$@"; do
    >         ext="${fullfilename##*\.}"
    >         name="${fullfilename%\.${ext}}"
    >         echo mv "${fullfilename}" "${name//./_}.${ext}"
    >     done
    > }
    bash$ testing field.field.field.ext name.segment.here.with.an.ext
    mv field.field.field.ext field_field_field.ext
    mv name.segment.here.with.an.ext name_segment_here_with_an.ext
    bash$
    I like to use a function like this to test a script. As you can see that the "mv" (remame) is just being echoed.

Posting Permissions

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