简体   繁体   English

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

[英]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 ' ' @

use tr : 使用tr

$ echo 'foo bar baz' | tr ' ' '@'
foo@bar@baz

( documentation ) 文件

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.

相关问题 一个演示cmd.exe和Linux shell(例如bash),定界参数的AC程序? - A C program demonstrating how cmd.exe and a linux shell e.g. bash, delimit parameters? Bash:检查是否给出了参数(例如,是否有参数“-a”?) - Bash: Check if argument is given (e.g. is there the argument "-a" ?) 拆分字符串(例如使用bash)但跳过部分字符串 - split string (e.g. with bash) but skip part of it 在 unix/linux 命令行中定义函数(例如 BASH) - Define function in unix/linux command line (e.g. BASH) 我如何在Linux性能下获得libc6符号(例如_int_malloc)的致电父母? - How do I get call parents for libc6 symbols (e.g. _int_malloc) with linux perf? Linux程序(例如bash或python脚本)如何知道它是如何启动的:从命令行还是交互式GUI? - How can Linux program, e.g. bash or python script, know how it was started: from command line or interactive GUI? 如何在 bash 脚本中的流水线命令之间插入延迟。 例如猫文件| 远程登录 mail.domain.com 25 - How to insert a delay between pipelining commands in a bash script. E.g. cat file | telnet mail.domain.com 25 是否可以防止任何人从ELF二进制文件中剥离调试符号(例如标签)? - Is it possible to prevent anyone from stripping debug symbols (e.g., labels) from an ELF binary? 如何在 Linux 上获得总体 RAM 使用率(例如 57%) - How to get overall RAM usage (e.g. 57%) on Linux 如何计算好CPU百分比,例如在顶部? - How is nice cpu percentage calculated, e.g. in top?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM