By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,704 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,704 IT Pros & Developers. It's quick & easy.

Space in file name problem

P: 30
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
Aug 22 '08 #1
Share this Question
Share on Google+
9 Replies

Nepomuk
Expert 2.5K+
P: 3,112
>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:
Expand|Select|Wrap|Line Numbers
  1. >cat checkfile
  2. file1
  3. file2
  4. new\ file
  5. file\ no
Note the backslashes - I'm escaping the space. I think, this way it should work fine.

Greetings,
Nepomuk
Aug 22 '08 #2

P: 30
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
Aug 23 '08 #3

Nepomuk
Expert 2.5K+
P: 3,112
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
Aug 23 '08 #4

P: 30
Error Msg i have posted at first place.
Aug 23 '08 #5

P: 30
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
Aug 23 '08 #6

Nepomuk
Expert 2.5K+
P: 3,112
Error Msg i have posted at first place.
OK, so it's still the same error message. Exactly the same?
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
Aug 23 '08 #7

Expert 100+
P: 511
don't use for loop with cat. Use while read loop instead to iterate a file
Expand|Select|Wrap|Line Numbers
  1. while read line
  2. do
  3.  # echo $line
  4. done < file
  5.  
Aug 23 '08 #8

P: 30
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
Aug 23 '08 #9

Expert 100+
P: 511
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.
Aug 24 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.