Space in file name problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashokd001
    New Member
    • Aug 2008
    • 30

    Space in file name problem

    Hi

    I am faceing a problem with a space in file name.
    I want to write a script which will open a file that file contain others file name, among them few file name contain space.
    >cat checkfile
    file1
    file2
    new file
    file no

    Here the file 'new file' and 'file no' contain space.When i trying to print contain of the file 'new file' from for loop it is giving error like,
    cat: new: No such file or directory
    cat: file: No such file or directory
    cat: file: No such file or directory
    cat: no: No such file or directory


    Please give suggestion how do i resolve.Thanks in advance.

    Thanks,
    -ASK
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by ashokd001
    >cat checkfile
    file1
    file2
    new file
    file no

    Here the file 'new file' and 'file no' contain space.When i trying to print contain of the file 'new file' from for loop it is giving error like,
    cat: new: No such file or directory
    cat: file: No such file or directory
    cat: file: No such file or directory
    cat: no: No such file or directory
    Try this:
    Code:
    >cat checkfile
    file1
    file2
    new\ file
    file\ no
    Note the backslashes - I'm escaping the space. I think, this way it should work fine.

    Greetings,
    Nepomuk

    Comment

    • ashokd001
      New Member
      • Aug 2008
      • 30

      #3
      No It is not wroking.

      As i told you i am getting problem in for loop.

      This chechfile is the input in for loop,
      As the for loop iterates based on space so \, ", ' anything is not wroking.

      Pls, try like this open that checkfile in for loop and then trying to print the containt of each file from checkfile.

      How do i resolve .Thank in advance.

      -Ashok

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by ashokd001
        No It is not wroking.

        As i told you i am getting problem in for loop.

        This chechfile is the input in for loop,
        As the for loop iterates based on space so \, ", ' anything is not wroking.

        Pls, try like this open that checkfile in for loop and then trying to print the containt of each file from checkfile.

        How do i resolve .Thank in advance.

        -Ashok
        Well, it's difficult to find a solution without having the code. Could you post the relevant parts of this shell script?

        Also, is the error message still the same?

        Greetings,
        Nepomuk

        Comment

        • ashokd001
          New Member
          • Aug 2008
          • 30

          #5
          Error Msg i have posted at first place.

          Comment

          • ashokd001
            New Member
            • Aug 2008
            • 30

            #6
            Just a for loop, input is the checkfile(which contain few file names )
            Inside using cat for each file.
            Thats it. You try, I am sure u will also face the same problem.

            There must be some way to use files and folders which contain space in script.

            Best Regards,
            Ashok

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by ashokd001
              Error Msg i have posted at first place.
              OK, so it's still the same error message. Exactly the same?
              Originally posted by ashokd001
              Just a for loop, input is the checkfile(which contain few file names )
              Inside using cat for each file.
              Thats it. You try, I am sure u will also face the same problem.
              That's not the point. We want to make your code work, so please post your code! I'm sure, we'll be able to solve the problem then.

              Greetings,
              Nepomuk

              Comment

              • ghostdog74
                Recognized Expert Contributor
                • Apr 2006
                • 511

                #8
                don't use for loop with cat. Use while read loop instead to iterate a file
                Code:
                while read line
                do
                 # echo $line
                done < file

                Comment

                • ashokd001
                  New Member
                  • Aug 2008
                  • 30

                  #9
                  What is the difference by reading with for to while loop.
                  Can you please elaborate a little.

                  Thanks for your suggestion.


                  I found out the above solution first convert the space before for loop to some other rearly used character. then inside for loop do the opposite by sed command.

                  Thanks for your time.
                  -Ask

                  Comment

                  • ghostdog74
                    Recognized Expert Contributor
                    • Apr 2006
                    • 511

                    #10
                    functions wise, there is no difference. They are just loops. Syntactically, they are different. PLease look through the bash reference manual for more information. I am just only advising on the caveats you will encounter when using a for cat loop to iterate a file. Special care need to be taken to ensure white spaces is escaped properly.

                    Comment

                    Working...