linux - 如何在 Linux 中使用 sed 将文本文件拆分为 10+ 个字符的块而不分割单词?
问题描述
我想提出一个 sed 命令,其中每 10 个字符将查找最近的空格并将其替换为“|”
我试过sed -E -e 's/ /|/\( *?[0-9a-zA-Z]*\)\{10,\}' new.file
了,但它显示错误。
示例输入:
Hello there! How are you? I am trying to figure this out.
预期输出:
Hello there!|How are you?|I am trying|to figure this|out.
解决方案
这适用于给定的样本:
$ sed -E 's/(.{10}[^ ]*) /\1|/g' ip.txt
Hello there!|How are you?|I am trying|to figure this|out.
(.{10}[^ ]*)
这匹配 10 个字符,后跟任何非空格字符- 然后匹配一个空格
\1|
放回捕获的部分和一个|
角色
推荐阅读
- python - 我如何与苹果同事分享 kivy 应用程序
- javascript - Javascript split() 字数统计问题
- firebase - Firebase 应用检查功能适用于实时数据库,但不适用于云存储
- unity3d - 有没有办法预测刚体将与墙壁和其他移动的刚体一起去哪里?
- reactjs - 是否可以仅使用传单进行平移和缩放功能?如何使地图不可见?
- python - 获取 numpy 数组的每个元素在排序时将具有的索引
- python - 未在 Kaggle 中导入 feature_engine 包的模块
- javascript - 如何使用循环导入多个js文件并导出所有这些js文件
- java - java背包使用蛮力
- postgresql - 函数调用另一个函数时的Postgers问题