filename with white space


 
Thread Tools Search this Thread
# 1  
filename with white space

our user creates a text file with a white space on the filename. this same file is transfered to unix via automation tool. i have a korn shell script that reads these files on a input directory and connects to oracle database to run the oracle procedures which will load the data from each of the files. it appears to be that my code does not recognize a file that has a white space on it's filename. i am using a simple for loop with a combination of ls -1 *.* commands on my code. i think i might be missing something.

below is the sample of snippet of the code that i use to simulate the result.
Code:
#!/usr/bin/ksh
cd /u02/app/bdms/dev/in
echo "simple ls -latr results in:\n"
ls -latr
echo "\n"
echo "when using for loop:"
for filename in $(ls -1 *.*)
do
 echo "files: $filename"
done
cd $HOME/apps

when the above code is run below is the results.
Code:
$ sh sample_dirlist.sh
simple ls -latr results in:

total 16
drwxr-xr-x    3 oracle   oracle          256 Aug 20 2009  ..
-rw-rw----    1 bdmsftdv staff          1174 Jan 06 16:43 Test 1 LFD.csv
drwxrwx---    2 bdmsftdv bdmsgrp         256 Jan 06 17:07 .
-rw-rw----    1 bdmsftdv staff          1204 Jan 09 13:59 Test 2 LFD.csv


when using for loop:
files: Test
files: 1
files: LFD.csv
files: Test
files: 2
files: LFD.csv

please help. thank you.
# 2  
*.* is a DOS thing, to specify all files in UNIX only * is necessary.

That's a useless use of ls * and useless use of backticks besides.

Do it without the ls and it works.

Code:
for FILE in *
do
        echo "File is $FILE"
done

Be careful to always quote the filename when you use it. mv $FILE destination/ will mess up when $FILE contains spaces, mv "$FILE" destination/ won't.
This User Gave Thanks to Corona688 For This Post:
# 3  
thanks so much that works
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Computers
Difficulty: Easy
Pointers were not used in the original C programming language; they were added later on in C++.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Preserve leading white space

I have about 350 programs in which I have to add 2 lines; one before and one after a specfic line. The following script does the job except that I lose the indentation. #!/usr/bin/bash while read line ... (8 Replies)
Discussion started by: jgt
8 Replies

2. Shell Programming and Scripting

Add white space

hi guys how can i add spacein file name with sed if strings have no space around dash input 19-20 ( 18-19 ) ABC-EFG output after add white space 19 - 20 (18 - 19 ) ABC - EFG thx in advance (2 Replies)
Discussion started by: mhs
2 Replies

3. Shell Programming and Scripting

AWK - Ignoring White Space with FS

I have an AWK script that uses multiple delimiters in the FS variable. FS="+" My awk script takes a file name such as this: 12345_smith_bubba_12345_20120215_4_0.pdf and parses it out based on the under score. Each parsed field then has some code for data validation etc. This script has... (12 Replies)
Discussion started by: reno4me
12 Replies

4. Shell Programming and Scripting

Replacing white spaces in filename

Hi; In following code find LOG_DIR -type f | while read filename; do echo $filename; done I want to precede each white space encountered in filename with \ so that when i use $filename for running some commands in do...done,it wont give me an error. will appreciate ur help in this.... (1 Reply)
Discussion started by: ajaypadvi
1 Replies

5. Shell Programming and Scripting

sed + white space

Hi, What sed command (if sed is the right command) can remove ALL white space from my file. I have a csv, except I want to remove all white space between commas and characters. My idea (without testing) sed 's/ //g' Is there a better way? (18 Replies)
Discussion started by: mcclunyboy
18 Replies

6. Shell Programming and Scripting

Matching white space through Grep

Hello All, I am trying to match white space in patterns through - Grep I tried ] & ] but none of them worked. Then I tried Perl extension '\s' and it worked. So just wanted to know if ] & ] are still supported or have they become deprecated. However they have been mentioned in the... (3 Replies)
Discussion started by: paragkalra
3 Replies

7. UNIX for Dummies Questions & Answers

SED with White Space

Dear Members, Suppose i have a variable test which stores a string as below: test='John drives+++++++++a+++++car' now i want to use sed on the above variable and replace + with a white space, so that i get echo $test should give me 'john drives a car' Between... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

8. Shell Programming and Scripting

how to protect white space in for loop

Hi All, I know there's a really simple answer to this but I just can't think of it :) I'm processing a file which has lines containing white space i.e. And I want to perform some awk on each line but when I do the following: for US in $( cat /tmp/unique-strings.tmp | sed 's/\/\\]/g'... (6 Replies)
Discussion started by: pondlife
6 Replies

9. Shell Programming and Scripting

stripping white space...

Hi All; Having a problem with a file.. the file contains the following data... (a snapshot) 1331F9E9DB7C2BB80EAEDE3A8F043B94,AL7 1DZ,M,50 186FDF93E1303DBA217279EC3671EA91,NG5 1JU,M,24 3783FFAF602015056A8CD21104B1AAAF,CH42 4NQ,M,17 It has 3 columns sepreated by a , the second column... (7 Replies)
Discussion started by: Zak
7 Replies

10. Shell Programming and Scripting

How to keep white space is being deleted using read

I am using Posix shell to write a script. The problem I am having is that when I use the read command to go through a file I lose the tabs. How can I keep this from happening? (1 Reply)
Discussion started by: keelba
1 Replies

Featured Tech Videos