如何将单行文本每6个空格后拆分为多行?tr命令无法满足需求
解决方案
要把包含多个状态条目的单行文本拆分为每行一个状态条目的格式,tr ' ' '\n'会拆分所有空格显然不适用,可以用以下几种工具实现:
1. sed 命令
通过全局替换,把每个状态条目末尾的RUNNING (带空格)替换为RUNNING\n(换行),命令如下:
echo "Current state of abc : RUNNING Current state of def : RUNNING Current state of ghi : RUNNING 下面所有的 measuring体现 这"预览你式Clns字符串替换为换行符,可直接输出目标格式:
Current state of abc : RUNNING
Current state of def : RUNNING
Current state of ghi : RUNNING
## 2. awk 命令 将`RUNNING `设为记录分隔符,遍历每个非空记录并补全`RUNNING`后输出: ```bash echo "Current state of abc : RUNNING Current state of def : RUNNING Current state of ghi : RUNNING" | awk -v RS="RUNNING " '{if ($0 != "") print $0 "RUNNING"}'
3. perl 命令
和sed逻辑一致,通过全局替换实现换行:
echo "Current state of abc : RUNNING Current state of def : RUNNING Current state of ghi : RUNNING" | perl -pe 's/RUNNING /RUNNING\n/g'
如果文本来自文件,可把echo "..."替换为< 文件名,比如:
sed 's/RUNNING /RUNNING\n/g' < input.txt
内容的提问来源于stack exchange,提问作者Meenakshisundaram Ramanathan
相关产品推荐
相关产品推荐

