环绕在一个文本文件中的所有行加上引号(“东西”)(Surround all lines in a t

2019-08-31 22:12发布

我有一个包含空格的目录列表。

我需要围绕他们',以确保我的批处理脚本会工作。

一个人怎么能围绕一个“和”(引号)每个新行。

文件1:

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

文件2:

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

Answer 1:

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


Answer 2:

使用SED:

sed -i "s/^.*$/'&'/g" filename


Answer 3:

您可以使用SED(1)插入开头,并在文件所以每行尾单引号:

sed -i~ -e "s/^/'/;s/$/'/" the_file


Answer 4:

很简单的逻辑,你只需要回声引号前面和后面。

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


文章来源: Surround all lines in a text file with quotes ('something')