用引号('something')环绕文本文件中的所有行

use*_*960 24 unix linux directory grep text

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

我需要用''来包围它们,以确保我的批处理脚本能够正常工作.

如何用'和'(引号)围绕每个新行.

例如

文件1:

/home/user/some type of file with spaces
/home/user/another type of file with spaces
Run Code Online (Sandbox Code Playgroud)

文件2:

'/home/user/some type of file with spaces'
'/home/user/another type of file with spaces'
Run Code Online (Sandbox Code Playgroud)

mar*_*ton 34

用sed?

sed -e "s/\(.*\)/'\1'/"
Run Code Online (Sandbox Code Playgroud)

或者,如下所述,如果目录可能包含撇号(如果他们这样做,那就是噩梦)使用此替代

sed -e "s/'/'\\\\''/g;s/\(.*\)/'\1'/"
Run Code Online (Sandbox Code Playgroud)

  • 如果文件名包含撇号怎么办? (2认同)

Ada*_*ard 6

使用sed:

sed -i "s/^.*$/'&'/g" filename
Run Code Online (Sandbox Code Playgroud)


小智 5

我更喜欢 awk (它比 bash 更快并且很容易扩展):

awk '{print "\'" $0 "\'"}'
Run Code Online (Sandbox Code Playgroud)