You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将单行文本每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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 04:50:22