File Name with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File Name with spaces
# 1  
Old 09-24-2010
File Name with spaces

I need to read pdf files copied in a unix directory.I tried using the for loop with find command but the file names is cutting off at the spaces.Below is the code I tried.
Code:
for FILENAME in `find $DIR_FILE/ -name "*.pdf" -mindepth 1 -maxdepth 1 -mmin +60 ` ##Needs to be changed later +60 to -60
do
  cat "$filename"
  logmsg "filename1 ${FILENAME}"
  AGTCODEFILE=`basename $FILENAME`
  echo $AGTCODEFILE
  logmsg "filename ${AGTCODEFILE}"
done

Can anybody please help me on this?

Last edited by Franklin52; 09-24-2010 at 12:50 PM.. Reason: Please indent your code and use code tags
# 2  
Old 09-24-2010
just use different flow control:

Code:
find $DIR_FILE/ -name "*.pdf" -mindepth 1 -maxdepth 1 -mmin +60 |
while read filename ; do

cat "$filename"
logmsg "filename1 ${FILENAME}"
AGTCODEFILE=`basename $FILENAME`
echo $AGTCODEFILE
logmsg "filename ${AGTCODEFILE}"

done

# 3  
Old 09-24-2010
I tried it ,but didn't work.I am trying something like below
Code:
 
for FILENAME in `find $DIR_FILE/-name *.pdf | awk 'gsub(/ /,"_")' -mindepth 1 -maxdepth 1 -mmin +60` ##Needs to be changed later +60 to -60
#cat "$filename"
logmsg "filename1 ${FILENAME}"
AGTCODEFILE=`basename $FILENAME`
echo $AGTCODEFILE

done

But ,getting error.Can somebody pls help?

Last edited by jim mcnamara; 09-24-2010 at 04:37 PM.. Reason: code tags
# 4  
Old 09-24-2010
Bar a minor typo the post from quirkasauus cures the problem reported in post #1 (filenames with spaces).
Also the variable name $filename is case-sensitive. It is not the same as $FILENAME.

With minor correction:

Code:
find "${DIR_FILE}"/ -name "*.pdf" -mindepth 1 -maxdepth 1 -mmin +60 | while read FILENAME
do
    cat "${FILENAME}"
    logmsg "filename1 ${FILENAME}"
    AGTCODEFILE=`basename "${FILENAME}"`
    echo "${AGTCODEFILE}"
    logmsg "filename ${AGTCODEFILE}"
done



The construct of "for filename in list" is never ever useful for an open-ended list or where there may be filenames contining spaces. I have absolutely no idea where the construct came from because I have never seen it in a unix book and never seen it in a commercial environment.


Btw: If you are "getting error" please copy/paste the command and the matching error message.

Last edited by methyl; 09-24-2010 at 05:33 PM.. Reason: layout issues which made it worse & case sensitivity. More bugs in the original script than immediately obvious. More quotes!
# 5  
Old 09-24-2010
A version that is safe for all legal filename characters (including newline ... as abhorrent as it may be in such a context Smilie):
Code:
find "$DIR_FILE" -name \*.pdf -mindepth 1 -maxdepth 1 -mmin +60 -exec sh -c '
    for FILENAME; do
        cat "$FILENAME"
        logmsg "filename1 ${FILENAME}"
        AGTCODEFILE=`basename $FILENAME`
        echo $AGTCODEFILE
        logmsg "filename ${AGTCODEFILE}"
    done    
' sh {} +

Like methyl, I too assumed that $filename is a typo of $FILENAME. If it is not, if what you shared is a fragment of a script and $filename is a different variable, then it could trivially be passed into the shell script as the first positional parameter (in the sample code above, between "sh" and "{}").

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove spaces from the file

Hi All, The output file contains data as below. "20141023","CUSTOMER" ,"COMPANY" ,"IN0515461" ,"" ,"JOSHUA" There are spaces in between the ending " and ,. The number of spaces is random. How can I remove that from the file so that the final output is:... (4 Replies)
Discussion started by: aarsh.dave
4 Replies

2. UNIX for Dummies Questions & Answers

remove spaces in between file

hey, I have this file: ATOM 2510 HG12 VAL 160 8.462 15.861 1.637 ATOM 2511 HG13 VAL 160 9.152 14.510 0.725 ATOM 2512 CG2 VAL 160 6.506 16.579 -0.088 ATOM 2513 HG21 VAL 160 5.499 16.421 -0.478 ATOM 2514 HG22 VAL 160 6.417 16.984 ... (4 Replies)
Discussion started by: kanikasharma
4 Replies

3. Shell Programming and Scripting

Conversion of spaces Text file into CSV format file

Input file (each line is separaed by spaces )given below: Name Domain Contact Phone Email Location ----------------------- ------------------------------------------------ ------- -----... (18 Replies)
Discussion started by: sreenath1037
18 Replies

4. UNIX for Dummies Questions & Answers

cleaning up spaces from fixed width file while converting to csv file

Open to a sed/awk/or perl alternative so that i can stick command into my bash script. This is a problem I resolve using a combination of cut commands - but that is getting convoluted. So would really appreciate it if someone could provide a better solution which basically replaces all... (3 Replies)
Discussion started by: svn
3 Replies

5. Emergency UNIX and Linux Support

Spaces in File Name issue

Hi, I have a small issue while using variables for a file name in UNIX. I have file name like "abc def 123.txt" Folder will be in /zzz/xxx/yyy I created 2 variables Folder = '/zzz/xxx/yyy' file = 'abc def 123.txt' While using this in grep command i am... (6 Replies)
Discussion started by: pssandeep
6 Replies

6. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

7. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

8. Shell Programming and Scripting

need help in replacing spaces in a file

hi all this is the part i am facing a problem eg data: filename : tr1 + T 40 this is a sample record in that file ... the value of T can be anything, but will be a single character. i need to cut from field two, and i am using this command cut -d " " -f2 tr1 >tr3 and the o/p is ... (7 Replies)
Discussion started by: sais
7 Replies

9. UNIX for Dummies Questions & Answers

create file with 96 spaces

hi, strange requirement but what to do:-(.... i need to create a file with a record length of 96 and entirely with spaces... i thought of using sed (like sed G etc...) but no success... wud be glad if some one help:-)... Regards, Bhups (3 Replies)
Discussion started by: Bhups
3 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question