bash - 用长度替换模式(Bash 的便携式解决方案)
问题描述
有没有办法用 Sed/Awk/Perl 用它的长度替换一个模式?我正在寻找可以在 Bash 脚本中使用的小型便携式命令。
我有所有以特定字符开头的字符串(比如说x
),我想x
用它们的长度替换这些重复。
所以
xxxx rest of the line
xxx again
xx again and again
xxxxx you got my point
会成为
4 rest of the line
3 again
2 again and again
5 you got my point
Sed 可能不是一个好的候选人。我知道 Perl 有一个e
允许在替换字符串中执行代码的选项,但我不确定在这里如何使用它:perl -pe 's/^(x+)/length($1)/e'
?
解决方案
用它的长度替换第一个模式:
awk '$1=length($1)' file
输出:
其余 4 条线路 3再 2一次又一次 5 你明白我的意思
推荐阅读
- android - 无法在卡片小部件中圆角图像
- imagemagick - 如何在 ImageMagick 中使用输入文件名?
- python - 方面并根据方面进行一些过滤
- javascript - 在 ngx-slick 轮播中从本地 JSON 加载图像的问题 - Angular 7
- python - 如何以相同的顺序将一个列表的元素插入另一个列表?
- wxpython - wxPython 2.8 到 wxPython 4 - 默认大小差异
- java - Java 错误 在命令窗口中运行简单程序时发生 JNI 错误
- elasticsearch - Kibana 无法启动
- node.js - 对一个 git 存储库中的脚本进行单元测试,该脚本调用其他 git 存储库中的函数
- scala - Scala - 在不执行的情况下获取具有阶段和任务的 DAG