For loop with space in file name

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers For loop with space in file name
# 1  
Old 09-06-2017
For loop with space in file name

Hi All

I have a source file named
Code:
ABC-20150613 to 20150613.zip

. I was trying to execute the below command on this source file, but its telling file is not available in that path and giving me some random file names.

Code:
ls -ltr| for z in ABC-????????*to*????????*.zip
do unzip $z -d
done

I want the file name to check by using full pattern, which makes me to give question mark. Kindly help me on this
# 2  
Old 09-06-2017
would
Code:
ls -ltr| for z in "ABC-????????*to*????????*".zip
do unzip $z -d
done

not do the job?
# 3  
Old 09-06-2017
I think the for loop's arguments should be expanded.
But the command argument must not be expanded again.
Also, the ls output seems pointless (if nothing in the loop reads from stdin).
Code:
for z in ABC-????????*to*????????*.zip
do unzip "$z" -d
done

# 4  
Old 09-06-2017
The pipe feeding the loop does nothing useful because the filenames are picked by matching the pattern you give after in with the content of your current directory.
Arguments for commands and programs are usually seperated by spaces. Your initial attempt fails because unzip treats every part of the filename that is seperated by space as a filename. You have to quote the variable representing the filename to tell unzip, that it is indeed a single filename:
Code:
for z in ABC-????????*to*????????*.zip
do unzip "$z" -d
done

PS.: typing half of the reply, then going to lunch and submitting thereafter is not the best idea Smilie
This User Gave Thanks to cero For This Post:
# 5  
Old 09-06-2017
I agree with MadeInGermany with one caveat: You are being precise with the number of characters in the date segments of the filename, and then blowing it with the spaces. Your pattern would match this:
Code:
ABC-1234567890to0987654321.zip

This would be better:
Code:
for z in ABC-????????\ to\ ????????.zip
do unzip "$z" -d
done

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 6  
Old 09-06-2017
Maybe I'm being thick, but what is the ls -lrt | for? I don't see it having any purpose the way it is coded. You need to quote $z when you use it to preserve the whitespace too.

If you want to get the files matching the pattern in modified date order (oldest first) then would you be better with something more like this?:-
Code:
for z in $(ls -1rt | grep -E "^ABC-.{8,}to.{8,}\.zip$")
do
   unzip "$z" -d
done

Note that I have used a digit 1 not a letter l else you get all sorts of other details you will not want.

To match your original specification, the extended expression for the grep should read:-
  • Starts with ABC-
  • Eight or more other characters (take out the comma in the {8,} to make it exactly 8)
  • Literal to
  • Eight or more other characters
  • And ends with the literal .zip (escaped dot)

If the filename length is fixed as describing something like a date with spaces in the name, then perhaps this would be neater all round:-
Code:
for z in ABC-????????\ to\ ????????.zip
do
   unzip "$z" -d
done

It depends what you will accept as input and whether the modification time of the file or the name of the file should be used for sorting.

Do either of these help?


Robin
These 2 Users Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help on For Loop to pass space separated value as one value

Hi, I am having a file say list1 with a output like below jun 12 18:23 may 20 18:23 Now i want to pass the above two values into for loop,I have written a script like this. #!/bin/bash a=`cat list1` for i in $a do echo "HI $i" done expected output: HI jun 12 18:23 (3 Replies)
Discussion started by: sumanthupar
3 Replies

2. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

3. Shell Programming and Scripting

Unable to read the first space of a record in while loop

I have a loop like while read i do echo "$i" . . . done < tms.txt The tms.txt contians data like 2008-02-03 00:00:00 <space>00:00:00 . . . 2010-02-03 10:54:32 (2 Replies)
Discussion started by: machomaddy
2 Replies

4. Shell Programming and Scripting

How to loop through space separated values?

How do I loop thru space separated values in a variable? I hate to use very complicated counter increment logic for this kind of simple problem. Expected result(using ksh) $>echo "aaa bbbb cccc" | <looping code here> var=aaa var=bbbb var=cccc $>echo "aaa bbbb cccc" | while IFS=" "... (12 Replies)
Discussion started by: kchinnam
12 Replies

5. Shell Programming and Scripting

escape space characters in loop from file

Hi Everyone! I want to build sql inserts from a list of countries/regions saved in a file. The list looks like this: United Kingdom Czech Republic ... The script I run is: while read i; do var=`expr $var + 1`; echo "INSERT INTO calltypes VALUES($var, '$i','$i');" >>... (5 Replies)
Discussion started by: linuca
5 Replies

6. Shell Programming and Scripting

Escape space in for loop

I have a file with the following contents # more hello.txt man hello man whereru The shell script i have tries to echo the contents of the file hello.txt for i in `cat hello.txt` do echo $i done but the output i am getting is taking the space as a new line.. #... (3 Replies)
Discussion started by: Tuxidow
3 Replies

7. Shell Programming and Scripting

Problem if parameter has space in it using loop

for cmdopts in $*;do case $cmdopts in -mode) mode="$2";shift 2 ;; -server) server="$2";shift 2 ;; -Id) Id="$2";shift 2 ;; -passwd) passwd="$2";shift 2 ;; -rmtDir) rmtDir="$2";shift 2 ;; -lcDir) ... (9 Replies)
Discussion started by: pinnacle
9 Replies

8. UNIX for Dummies Questions & Answers

Use of For loop with Space

Hi, I am trying to query the database to get the list of portfolio and for each portfolio, I am using the for loop, but the problem is some of the portfolio is having the spaces. The Code PORT=`${EFG_ISQL} -b <<-! set nocount on use ${EFG_DB} go select portId from PORTFOLIO go... (3 Replies)
Discussion started by: peter35james
3 Replies

9. Shell Programming and Scripting

Moving files with space, in for loop

Hi All I need to put a bunch of specific files in a directory (with loads of other files), into a tar archive. The best way I thought of doing this was putting the filenames into a file, reading them line by line in a for loop, and then adding them to a tar acrhive. However the filenames have... (6 Replies)
Discussion started by: saabir
6 Replies

10. 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
Login or Register to Ask a Question