Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何将Bash/shell中的某些符号(例如“空格”)更改为其他符号_Linux_Bash_Shell_Text - Fatal编程技术网

Linux 如何将Bash/shell中的某些符号(例如“空格”)更改为其他符号

Linux 如何将Bash/shell中的某些符号(例如“空格”)更改为其他符号,linux,bash,shell,text,Linux,Bash,Shell,Text,我有一些来自 ps -ef | grep apache 我需要将该输出中的所有空格更改为“@”符号 是否可以为此使用一些bash脚本? 感谢基本sed命令: ps -ef | grep apache | sed 's/ /@/g' sed's/text/new text/g'查找“text”并将其替换为“new text” 如果您想替换更多字符,例如,将所有空格和替换为@:(谢谢): 使用: 使用tr: $ echo 'foo bar baz' | tr ' ' '@' foo@bar@ba

我有一些来自

ps -ef | grep apache
我需要将该输出中的所有空格更改为“@”符号 是否可以为此使用一些bash脚本? 感谢基本sed命令:

ps -ef | grep apache | sed 's/ /@/g'
sed's/text/new text/g'
查找“text”并将其替换为“new text”

如果您想替换更多字符,例如,将所有空格和
替换为
@
:(谢谢):

使用:

使用
tr

$ echo 'foo bar baz' | tr ' ' '@'
foo@bar@baz
ps -ef | grep apache | tr -s ' ' '@'

()

如果使用
awk
,您可以跳过额外的
grep

ps -ef | awk '/apache/{gsub(/ /,"@");print}'

如果希望多个空格字符仅替换为一个
@
符号,则可以将
-s
标志与
tr
一起使用:

$ echo 'foo bar baz' | tr ' ' '@'
foo@bar@baz
ps -ef | grep apache | tr -s ' ' '@'
或者此
sed
解决方案:

ps -ef | grep apache | sed -r 's/ +/@/g'

为什么不使用字符类,例如sed's/[[u]/@/g'?@AdrianFrühwirth谢谢,我不知道我们可以做这样的事情。为什么?我并不是有意轻率;只是您似乎希望将结果传递给另一个命令,在该命令中空格将导致问题,而且可能有一种比首先使用
ps
的输出更好的方法。