如何在linux cat命令中同时处理单引号/撇号和空格?
我在 test's dir
目录中有一个名为 test's file.txt
的文件。 因此文件路径变成test's dir/test's file.txt
。 我想捕获文件的内容,但由于该文件包含撇号和空格,因此我很难实现这一点。 我尝试了几个命令,包括
sh -c "cat 'test's dir/test's file.txt'"
sh -c 'cat "test's dir/test's file.txt"'
sh -c "cat '"'"'test's dir/test's file.txt'"'"'"
sh -c 'cat "test\'s\ dir/test\'s\ file.txt"'
还有更多... 但它们都不起作用。
I have a file named test's file.txt
inside test's dir
directory.
So the file-path becomes test's dir/test's file.txt
.
I want to cat the content of the file but since the file contains an apostrophe and a space it is giving me hard time achieving that.
I have tried several commands including
sh -c "cat 'test's dir/test's file.txt'"
sh -c 'cat "test's dir/test's file.txt"'
sh -c "cat '"'"'test's dir/test's file.txt'"'"'"
sh -c 'cat "test\'s\ dir/test\'s\ file.txt"'
and many more ...
But none of them is working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请您尝试一下:
至于路径名部分,它是以下内容的串联:
如果您想在
python
中执行shell命令,请您尝试:或立即:
作为
子进程。 run()
函数将命令作为列表,不是单个字符串(可以使用
shell=True
选项),我们没有担心命令周围的额外引用。
请注意,Python 3.5 或更高版本支持
subprocess.run()
。Would you please try:
As for the pathname part, it is a concatenation of:
If you want to execute the shell command in
python
, would you please try:or immediately:
As the
subprocess.run()
function takes the command as a list,not a single string (possible with
shell=True
option), we do not haveto worry about the extra quoting around the command.
Please note
subprocess.run()
is supported by Python 3.5 or newer.您可以使用此处-doc:
You can use here-doc:
此选项避免了两级引用的需要:
请参阅如何通过“bash -c”命令使用位置参数?。 (它也适用于
sh -c
。)This option avoids the need for two levels of quoting:
See How to use positional parameters with "bash -c" command?. (It applies to
sh -c
too.)