views:

147

answers:

4

I've got a list of directories that contain spaces.

I need to surround them with ' ' to ensure that my batch scripts will work.

How can one surround each new line with a ' and a ' (quotes).

e.g.

File1:

/home/user/some type of file with spaces
/home/user/another type of file with spaces

To

File2:

'/home/user/some type of file with spaces'
'/home/user/another type of file with spaces'
+2  A: 

Using sed:

sed -i "s/^.*$/'&'/g" filename
Adam Bard
Doesn't work for me Adam - get various errors depending on the particular filename - maybe need -e in there?
martin clayton
Pretty sure I popped exactly that into cygwin, and it did fine. Could have made a type though.
Adam Bard
+2  A: 

Use sed?

sed -e "s/\(.*\)/'\1'/"

Or, as commented below, if the directories might contain apostrophes (nightmare if they do) use this alternate

sed -e "s/'/'\\\\''/g;s/\(.*\)/'\1'/"
martin clayton
Command worked perfectly - obviously if anyone was to use this on a file the would append it at the end of the command.
what if the filename contains an apostrophe?
asveikau
I would modify this to sed -e "s/'/'\\\\''/g;s/\(.*\)/'\1'/"
asveikau
A: 

You can use sed(1) to insert single quotes at the beginning and end of each line in a file as so:

sed -i~ -e "s/^/'/;s/$/'/" the_file
Adam Rosenfield
A: 

very simple logic, you just need to echo the quotes in front and behind.

while read -r line
do
  echo "'$line'"
  # do something
done < "file"
ghostdog74