Double quote filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Double quote filenames
# 1  
Old 11-06-2012
Double quote filenames

Hi, I need to double quotes filenames in the following string:
Code:
      /tmp/*file1                                 /tmp/*file2                                 /tmp/*file3                                 /tmp/*file4

I tried to do this using sed, but it double quotes every character Smilie
Code:
$ echo "$files"|sed 's/^ */"/;s/ */" "/g;s/$/"/'
" """ "/" "t" "m" "p" "/" "*" "f" "i" "l" "e" "1" "/" "t" "m" "p" "/" "*" "f" "i" "l" "e" "2" "/" "t" "m" "p" "/" "*" "f" "i" "l" "e" "3" "/" "t" "m" "p" "/" "*" "f" "i" "l" "e" "4" ""

Any idea how to do this?
# 2  
Old 11-06-2012
Does your input really look like that (no newlines)?

Code:
$ sed 's/[^ ][^ ]*/"&"/g' file1
      "/tmp/*file1"                                 "/tmp/*file2"                                 "/tmp/*file3"                                 "/tmp/*file4"

# 3  
Old 11-06-2012
Add one more space in s/ */" "/g to make it s/ */" "/g.
# 4  
Old 11-06-2012
Gnome

Thanks for the help guys, both solutions work. But it doesn't solve my initial problem.

The variable I have containing the filenames needs to be used in a for loop:

Code:
for f in $files ; do
    do stuff
done

The problem is that the for loop evaluates the wildcard instead of parsing it.

This works, but not when these filenames are in a variable:
Code:
for f in "/tmp/*file1" "/tmp/*file2" "/tmp/*file3" "/tmp/*file4" ; do
    do stuff
done

# 5  
Old 11-06-2012
Hmm. You did say it was a string, not a file!

Is it necessary to quote the filenames anyway? If the filenames have spaces in them then any given solution so far will fail anyway.

Code:
STR=$(echo $YourSrting | sed ...)

Code:
for FILE in $STR; do
  ...
done

# 6  
Old 11-06-2012
One way:
Code:
files='      /tmp/*file1                                 /tmp/*file2                                 /tmp/*file3                                 /tmp/*file4'
set -f
a=($files)
set +f
for i in "${a[@]}"
do
 echo "$i"
done

producing
Code:
/tmp/*file1
/tmp/*file2
/tmp/*file3
/tmp/*file4

And yes, I did create some files in /tmp matching a couple of those globs.

Last edited by elixir_sinari; 11-06-2012 at 06:59 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 11-06-2012
Quote:
Originally Posted by Scott
Hmm. You did say it was a string, not a file!

Is it necessary to quote the filenames anyway?

Code:
STR=$(echo $YourSrting | sed ...)

Code:
for FILE in $STR; do
  ...
done

It's kind of difficult to explain...
Let's say I have these files:
Code:
 
$ ls -1
1file1
1file2
2file1
2file2

My variable:
Code:
 
files="/tmp/test/*file1 /tmp/test/*file2 /tmp/test/*file3 /tmp/test/*file4"

If I loop through this using a for loop I get this:
Code:
 
$ for f in $files ; do echo $f ; done
/tmp/test/1file1
/tmp/test/2file1
/tmp/test/1file2
/tmp/test/2file2
/tmp/test/*file3
/tmp/test/*file4

Instead, I want this:
Code:
 
$ for f in $files ; do echo $f ; done
/tmp/test/*file1
/tmp/test/*file2
/tmp/test/*file3
/tmp/test/*file4

---------- Post updated at 06:02 AM ---------- Previous update was at 05:58 AM ----------

elixir_sinari, your solution works perfectly in bash but not in ksh unfortunately.
Is there any alternative which I can use with ksh?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

2. Shell Programming and Scripting

Replacing Double Quote in Double Quote incsv file

Hi All , We have source data file as csv file and since data could contain commas ,each attribute is quoted into double quotes.However problem is that some of the attributa data also contain double quotes which is converted to double double quote while creating csv file XLs data : ... (2 Replies)
Discussion started by: Shalini Badal
2 Replies

3. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

4. Shell Programming and Scripting

getting value between double quote

Can somebody supply me with a simple way to get a value between two double quotes? Example: input = ADR base is "/u01/app/oracle" output = /u01/app/oracle Thanks to all who answer (4 Replies)
Discussion started by: BeefStu
4 Replies

5. Shell Programming and Scripting

double quote as the delimiter

I'm trying to extract a column from a csv file with either cut or awk but some of the fields contain comma with them: "Field1","Field2, additional info","Field3",...,"Field17",... If I want to extract column 3 and use comma as the delimiter, I'll actually get the additional info bit but not... (4 Replies)
Discussion started by: ivpz
4 Replies

6. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

7. Shell Programming and Scripting

double-quote inside double-quote

hey all, i made a simple .sh like this: echo "<style media="screen" type="text/css">@import url("main.css");</style>" but the output is: <style media=screen type=text/css>@import url(main.css);</style> i want to keep double-quotes, can anyone help me? thanks (3 Replies)
Discussion started by: indraf
3 Replies

8. Shell Programming and Scripting

How to get rid of double quote in sed.

Hi, i am using sed command to grep just a valuable data for my report generating. Thanks to the person who assists me on before thread. the problem that i encounter now is when i executed below command The output will give me like below output in between the data, there is a double quote. How... (6 Replies)
Discussion started by: anakiar
6 Replies

9. Shell Programming and Scripting

single or double quote in SED

i m trying the following command but its not working: sed 's/find/\'replace\'/g' myFile but the sed enters into new line # sed 's/find/re\'place/g' myFile > I havn't any idea how to put single quote in my replace string. Your early help woud be appreciated. Thanx (2 Replies)
Discussion started by: asami
2 Replies
Login or Register to Ask a Question