我有一个包含空格的目录列表。
我需要用“'来包围它们,以确保我的批处理脚本能够正常工作。
如何用一个'和一个'(引号)来包围每一个新行。
例如
文件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'
使用sed?
sed -e "s/\(.*\)/'\1'/"
或者,如下所述,如果目录可能包含撇号(如果它们是噩梦),则使用此替代
sed -e "s/'/'\\\\''/g;s/\(.*\)/'\1'/"
使用sed:
sed -i "s/^.*$/'&'/g" filename
您可以使用sed(1)在文件中每行的开始和结尾处插入单引号,如下所示:
sed -i~ -e "s/^/'/;s/$/'/" the_file
非常简单的逻辑,你只需要在前面和后面回显引号。
while read -r line do echo "'$line'" # do something done < "file"