bash - 用 \n+numbers+\n 替换所有数字
问题描述
我想用换行符+数字+换行符替换字符串中的所有数字。
更改字符串
1xxx2yyy3zzz
进入
1
xxx
2
yyy
3
zzz
他们俩都不能出招。
echo "1xxx2yyy3zzz" |tr '0-9' '\n0-9\n'
echo "1xxx2yyy3zzz" |tr '[0-9]' '\n[0-9]\n'
echo "1xxx2yyy3zzz" |tr [:digit:] \n[:digit:]\n
解决方案
考虑到您的 Input_file 与显示的示例相同,那么以下内容可能会对您有所帮助。
sed -E 's/[a-zA-Z]+/\n&\n/g;s/\n$//' Input_file
说明:现在也为上述代码添加说明。它仅用于解释目的。
sed -E ' ##Starting sed here and -E option is for extended regex enabling it.
s ##s is for substitution.
/[a-zA-Z]+/ ##look for regex all small of capital letter alphabets and substitute them with following regex.
\n&\n/ ##Substitute above match with a NEW line matched value and a NEW line here.
g; ##g means perform this action to all matched patterns on current line.
s/ ##Starting a new substitution here.
\n$ ##Substituting NEW LINE which is coming in last of the line with following.
// ##Substituting above with NULL.
' Input_file ##Mentioning Input_file name here.
推荐阅读
- python - 从 qtconsole 之外的 python 运行时调用 %history
- c# - 在 C# 中的异步函数中填充字符串数组
- javascript - 使用 React TypeScript 进行 GraphQL 身份验证
- r - 如何使我的 for 循环随着时间的推移正确计算平均值?
- architecture - 游戏引擎设计中的 GUI 系统和渲染系统有什么区别?
- php - 如何在php中从包含特定单词的数组中删除文件
- arrays - VBA 添加到 ReDim 值
- mysql - 尝试使用 while 循环将多条记录添加到由字段定义的 MYSQL 表中,以防它们不存在
- linux - aws cli 输出自动发送到 vi
- linux - 如何正确管理 g_dbus_interface_get_object() 返回的引用