Results 1 to 6 of 6

Thread: space character seperates string in container

  1. #1
    Join Date
    Aug 2011
    Beans
    2

    Cool space character seperates string in container

    Hi everyone,

    I try to use the terminal for multiple renaming. For this I got a script from someone and it goes like this:

    for F in $(find *.pdf); do mv ${F} $(echo ${F} | sed s/pdf/txt/); done

    it searches for all pdf files and changes them to txt files.
    However, it does not work for files containing a space character. Here the container F (is this called a container btw.??) separates the input from the find command into single words. The foldername "hello world" would be separated in "hello" and "world".
    My question is: how can this be inhibit?

    Thanks in advance
    Jakob

  2. #2
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    7,700

    Re: space character seperates string in container

    I think putting this command in front should work:
    Code:
    export IFS=$'\n'
    It says the inter-field-separator is newline, rather than the normal newline-or-tab-or-space.

  3. #3
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,573
    Distro
    Ubuntu

    Re: space character seperates string in container

    Also, always quote your variables.

    http://mywiki.wooledge.org/BashPitfa...file_.24target

  4. #4
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: space character seperates string in container

    Check out the first (probably the most common pitfall) from the link posted by nothingspecial.

    Oh, and http://mywiki.wooledge.org/BashFAQ/020

    Also, instead of echo text | sed whatever you can use parameter expansion.

    Code:
    while IFS= read -d '' file
    do
        mv -b -- "$file" "${file%.pdf}.txt"
    done < <(find ./ \*.pdf -print0)

  5. #5
    Join Date
    Aug 2011
    Beans
    2

    Re: space character seperates string in container

    hey, thanks a lot for your replies, I made it with your help

    however, its still a bit fishing in the dark with those variables. Sometimes it has to be in " ", sometimes the spaces have to be preceded with a /, sometimes not. And some more stuff which confuses me - I think I need to follow an introduction to shell programming. I found this site http://www.freeos.com/guides/lsst/ or does anyone have a tip?

    greets
    Jakob

  6. #6
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: space character seperates string in container

    That's a very outdated guide. Check out the BashGuide, BashFAQ and BashPitfalls at http://mywiki.wooledge.org/

Tags for this Thread

Bookmarks

Posting Permissions

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