Search code examples
linuxunixtextdirectorygrep

Surround all lines in a text file with quotes ('something')


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'

Solution

  • 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'/"