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

后端 未结 4 381
独厮守ぢ
独厮守ぢ 2021-01-30 17:34

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

相关标签:
4条回答
  • 2021-01-30 18:17

    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'/"
    
    0 讨论(0)
  • 2021-01-30 18:23

    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"
    
    0 讨论(0)
  • 2021-01-30 18:27

    Using sed:

    sed -i "s/^.*$/'&'/g" filename
    
    0 讨论(0)
  • 2021-01-30 18:31

    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
    
    0 讨论(0)
提交回复
热议问题