[英]How to change some symbols (e.g. “space”) to other symbol in Bash/shell
I have some output from 我有一些输出
ps -ef | grep apache
I need to change all spaces in that output to '@' symbol Is it possible to use some bash script for this? 我需要将输出中的所有空格都更改为'@'符号。是否可以为此使用一些bash脚本? Thanks 谢谢
使用tr
:
ps -ef | grep apache | tr ' ' @
Basic sed command: 基本sed命令:
ps -ef | grep apache | sed 's/ /@/g'
sed 's/text/new text/g'
looks for "text" and replaces it with "new text". sed 's/text/new text/g'
查找“ text”,并将其替换为“ new text”。
In case you want to replace more characters, for example replace all spaces and _
with @
: (thanks Adrian Frühwirth ): 如果要替换更多字符,例如用@
:替换所有空格和_
(感谢AdrianFrühwirth ):
ps -ef | grep apache | sed 's/[_ ]/@/g'
如果使用awk
则可以跳过多余的grep
:
ps -ef | awk '/apache/{gsub(/ /,"@");print}'
If you want multiple space characters to be replaced with only one @
symbol, you can use -s
flag with tr
: 如果要将多个空格字符仅替换为一个@
符号,则可以将-s
标志与tr
:
ps -ef | grep apache | tr -s ' ' '@'
or this sed
solution: 或此sed
解决方案:
ps -ef | grep apache | sed -r 's/ +/@/g'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.